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

Last change on this file since 1706 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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