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

Last change on this file since 4050 was 4050, checked in by Peter, 9 months ago

remove white-space

  • 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, 2008 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2012, 2017, 2019 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 <config.h>
26
27#include "Target.h"
28#include "yat/random/random.h"
29#include "yat/utility/Index.h"
30#include "yat/utility/stl_utility.h"
31#include "yat/utility/utility.h"
32
33#include <algorithm>
34#include <cassert>
35#include <iostream>
36#include <map>
37#include <string>
38#include <sstream>
39#include <utility>
40#include <vector>
41
42namespace theplu {
43namespace yat {
44namespace classifier {
45
46  Target::Target(void)
47  {
48  }
49
50
51  Target::Target(const std::vector<std::string>& label)
52  {
53    init(label);
54  }
55
56
57  Target::Target(const std::vector<std::string>& label,
58                 const Target& t)
59    : class_map_(t.class_map_)
60  {
61    binary_=t.binary_;
62    labels_ = t.labels_;
63    classes_.reserve(label.size());
64    for (std::vector<std::string>::const_iterator lab=label.begin();
65         lab!=label.end(); ++lab) {
66      std::map<std::string,size_t>::iterator i=class_map_.lower_bound(*lab);
67      if (i->first==*lab) // label exists
68        classes_.push_back(i->second);
69      else {
70        binary_.push_back(0);
71        classes_.push_back(labels_.size());
72        class_map_.insert(i, std::make_pair(*lab, labels_.size()));
73        labels_.push_back(*lab);
74      }
75    }
76  }
77
78
79  Target::Target(std::istream& is, char sep)
80  {
81    std::vector<std::string> vec;
82    std::string word;
83    bool ok=true;
84    while(ok) {
85      if(sep=='\0')
86        ok=!(is>>word).fail();
87      else
88        ok=!getline(is, word, sep).fail();
89
90      // ignore empty words
91      if (!word.size() || !ok )
92        continue;
93
94      vec.push_back(word);
95    }
96
97    // manipulate the state of the stream to be good
98    is.clear(std::ios::goodbit);
99
100    init(vec);
101  }
102
103
104  Target::Target(const Target& t,
105                 const utility::Index& index)
106    : class_map_(t.class_map_)
107  {
108    binary_=t.binary_;
109    classes_.resize(index.size());
110    for (size_t i=0; i<index.size(); i++) {
111      assert(index[i]<t.size());
112      classes_[i]=t.classes_[index[i]];
113    }
114    labels_ = t.labels_;
115  }
116
117
118  Target::~Target(void)
119  {
120  }
121
122  bool Target::binary(size_t i) const
123  {
124    assert(i<size());
125    return binary_[operator()(i)];
126  }
127
128  const std::map<std::string,size_t>& Target::classes(void) const
129  {
130    return class_map_;
131  }
132
133  void Target::init(const std::vector<std::string>& label)
134  {
135    classes_.clear();
136    classes_.reserve(label.size());
137    class_map_.clear();
138    labels_.clear();
139
140    for (size_t i=0; i<label.size(); i++) {
141      std::map<std::string,size_t>::iterator iter =
142        class_map_.lower_bound(label[i]);
143
144      // label in map
145      if (iter != class_map_.end() &&
146          !(class_map_.key_comp()(label[i],iter->first)) ){
147
148        classes_.push_back(iter->second);
149      }
150      // label not found in map
151      else{
152        classes_.push_back(class_map_.size());
153        class_map_.insert(iter, std::make_pair(label[i],classes_[i]));
154        labels_.push_back(label[i]);
155      }
156    }
157    // setting binary to false for every class
158    binary_=std::vector<char>(nof_classes(),false);
159
160    set_binary(0,true);
161  }
162
163  const std::vector<std::string>& Target::labels(void)
164  {
165    return labels_;
166  }
167
168  size_t Target::nof_classes(void) const
169  {
170    return classes().size();
171  }
172
173  void Target::random_shuffle(void)
174  {
175    random::random_shuffle(classes_.begin(), classes_.end());
176  }
177
178  void Target::set_binary(size_t i, bool b)
179  {
180    assert(i<nof_classes());
181    binary_[i]=b;
182  }
183
184  size_t Target::size(const std::string& label) const
185  {
186    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);
187    if(i==class_map_.end())
188      return 0;
189    assert(i->first==label);
190    return size(i->second);
191  }
192
193  size_t Target::size(void) const
194  {
195    return classes_.size();
196  }
197
198  size_t Target::size(size_t cl) const
199  {
200    return std::count(classes_.begin(),classes_.end(),cl);
201  }
202
203  size_t Target::operator()(size_t sample) const
204  {
205    assert(sample<size());
206    return classes_[sample];
207  }
208
209  size_t Target::operator[](size_t sample) const
210  {
211    return this->operator()(sample);
212  }
213
214  const Target& Target::operator=(const Target& other)
215  {
216    binary_ = other.binary_;
217    classes_ = other.classes_;
218    class_map_ = other.class_map_;
219    labels_ = other.labels_;
220    return *this;
221  }
222
223
224  std::ostream& operator<<(std::ostream& s, const Target& a)
225  {
226    for (size_t j = 0; j < a.size(); ++j) {
227      s << a(j);
228      if ( (j+1)<a.size() )
229        s << " ";
230    }
231
232    return s;
233  }
234
235
236}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.