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

Last change on this file since 3579 was 3579, checked in by Peter, 5 years ago

merge release 0.14 into trunk

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