source: trunk/yat/classifier/Target.cc @ 831

Last change on this file since 831 was 831, checked in by Peter, 15 years ago

Refs #185.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.3 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005 Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6
7  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Target.h"
26#include "yat/random/random.h"
27#include "yat/utility/stl_utility.h"
28#include "yat/utility/utility.h"
29
30#include <algorithm>
31#include <cassert>
32#include <iostream>
33#include <map>
34#include <string>
35#include <sstream>
36#include <utility>
37#include <vector>
38
39namespace theplu {
40namespace yat {
41namespace classifier {
42
43  Target::Target(const std::vector<std::string>& label)
44  {
45    init(label);
46  }
47 
48  Target::Target(const Target& t, 
49                 const std::vector<size_t>& index)
50    : class_map_(t.class_map_)
51  {
52    binary_=t.binary_;
53    classes_.resize(index.size());
54    for (size_t i=0; i<index.size(); i++) {
55      assert(index[i]<t.size());
56      classes_[i]=t.classes_[index[i]];
57    }
58    labels_ = t.labels_;
59  }
60
61
62  Target::Target(std::istream& is, char sep) 
63    throw (utility::IO_error,std::exception)
64  {
65    std::vector<std::string> vec;
66    std::string word;
67    bool ok=true;
68    while(ok) {
69      if(sep=='\0')
70        ok=(is>>word);
71      else 
72        ok=getline(is, word, sep);
73
74      // ignore empty words
75      if (!word.size() || !ok )
76        continue;
77     
78      vec.push_back(word);
79    }
80
81    // manipulate the state of the stream to be good
82    is.clear(std::ios::goodbit);
83
84    init(vec);
85  }
86 
87  Target::~Target(void)
88  {
89  }
90
91  bool Target::binary(size_t i) const
92  {
93    assert(i<size());
94    return binary_[operator()(i)];
95  }
96
97  const std::map<std::string,size_t>& Target::classes(void) const
98  {
99    return class_map_;
100  }
101
102  void Target::init(const std::vector<std::string>& label) 
103  {
104    classes_.clear();
105    classes_.reserve(label.size());
106    class_map_.clear();
107    labels_.clear();
108   
109    for (size_t i=0; i<label.size(); i++) {
110      std::map<std::string,size_t>::iterator iter = 
111        class_map_.lower_bound(label[i]);
112
113      // label in map
114      if (iter != class_map_.end() && 
115          !(class_map_.key_comp()(label[i],iter->first)) ){
116
117        classes_.push_back(iter->second);
118      }
119      // label not found in map
120      else{
121        classes_.push_back(class_map_.size());
122        class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 
123        labels_.push_back(label[i]);
124      }
125    }
126    // setting binary to false for every class
127    binary_=std::vector<char>(nof_classes(),false);
128   
129    set_binary(0,true);
130  }
131
132  const std::vector<std::string>& Target::labels(void)
133  {
134    return labels_;
135  }
136
137  const size_t Target::nof_classes(void) const
138  {
139    return classes().size();
140  }
141
142  void Target::random_shuffle(void)
143  {
144    random::DiscreteUniform d;
145    std::random_shuffle(classes_.begin(), classes_.end(),d);
146  }
147
148  void Target::set_binary(size_t i, bool b)
149  {
150    assert(i<nof_classes());
151    binary_[i]=b;
152  }
153
154  const size_t Target::size(const std::string& label) const 
155  {
156    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);   
157    if(i==class_map_.end())
158      return 0;
159    assert(i->first==label);
160    return size(i->second);
161  }
162 
163  size_t Target::size(void) const
164  {
165    return classes_.size();
166  }
167
168  const size_t Target::size(size_t cl) const
169  {
170    return std::count(classes_.begin(),classes_.end(),cl);
171  }
172
173  size_t Target::operator()(size_t sample) const
174  {
175    assert(sample<size());
176    return classes_[sample];
177  }
178
179  size_t Target::operator[](size_t sample) const
180  {
181    return this->operator()(sample);
182  }
183
184  const Target& Target::operator=(const Target& other)
185  {
186    binary_ = other.binary_;
187    classes_ = other.classes_;
188    class_map_ = other.class_map_;
189    labels_ = other.labels_;
190    return *this;
191  }
192
193
194  std::ostream& operator<<(std::ostream& s, const Target& a)
195  {
196    for (size_t j = 0; j < a.size(); ++j) {
197      s << a(j);
198      if ( (j+1)<a.size() )
199        s << " ";
200    }
201
202    return s;
203  }
204
205
206}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.