source: trunk/yat/classifier/Target.cc

Last change on this file was 4245, checked in by Peter, 13 days ago

GCC -Wdeprecated-copy complains when some but not all of copy ctor, dtor and operator= are user-implemented. When the implementation is identical with the one generated by the compiler, there is no reason to keep the code. DataWeightProxy? is a different story (see #986)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.7 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, 2021 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  bool Target::binary(size_t i) const
119  {
120    assert(i<size());
121    return binary_[operator()(i)];
122  }
123
124  const std::map<std::string,size_t>& Target::classes(void) const
125  {
126    return class_map_;
127  }
128
129  void Target::init(const std::vector<std::string>& label)
130  {
131    classes_.clear();
132    classes_.reserve(label.size());
133    class_map_.clear();
134    labels_.clear();
135
136    for (size_t i=0; i<label.size(); i++) {
137      std::map<std::string,size_t>::iterator iter =
138        class_map_.lower_bound(label[i]);
139
140      // label in map
141      if (iter != class_map_.end() &&
142          !(class_map_.key_comp()(label[i],iter->first)) ){
143
144        classes_.push_back(iter->second);
145      }
146      // label not found in map
147      else{
148        classes_.push_back(class_map_.size());
149        class_map_.insert(iter, std::make_pair(label[i],classes_[i]));
150        labels_.push_back(label[i]);
151      }
152    }
153    // setting binary to false for every class
154    binary_=std::vector<char>(nof_classes(),false);
155
156    set_binary(0,true);
157  }
158
159  const std::vector<std::string>& Target::labels(void) const
160  {
161    return labels_;
162  }
163
164  size_t Target::nof_classes(void) const
165  {
166    return classes().size();
167  }
168
169  void Target::random_shuffle(void)
170  {
171    random::random_shuffle(classes_.begin(), classes_.end());
172  }
173
174  void Target::set_binary(size_t i, bool b)
175  {
176    assert(i<nof_classes());
177    binary_[i]=b;
178  }
179
180  size_t Target::size(const std::string& label) const
181  {
182    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);
183    if(i==class_map_.end())
184      return 0;
185    assert(i->first==label);
186    return size(i->second);
187  }
188
189  size_t Target::size(void) const
190  {
191    return classes_.size();
192  }
193
194  size_t Target::size(size_t cl) const
195  {
196    return std::count(classes_.begin(),classes_.end(),cl);
197  }
198
199  size_t Target::operator()(size_t sample) const
200  {
201    assert(sample<size());
202    return classes_[sample];
203  }
204
205  size_t Target::operator[](size_t sample) const
206  {
207    return this->operator()(sample);
208  }
209
210
211  std::ostream& operator<<(std::ostream& s, const Target& a)
212  {
213    for (size_t j = 0; j < a.size(); ++j) {
214      s << a(j);
215      if ( (j+1)<a.size() )
216        s << " ";
217    }
218
219    return s;
220  }
221
222
223}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.