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

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

Updating copyright statements.

  • 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://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
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    throw (utility::IO_error,std::exception)
81  {
82    std::vector<std::string> vec;
83    std::string word;
84    bool ok=true;
85    while(ok) {
86      if(sep=='\0')
87        ok=(is>>word);
88      else 
89        ok=getline(is, word, sep);
90
91      // ignore empty words
92      if (!word.size() || !ok )
93        continue;
94     
95      vec.push_back(word);
96    }
97
98    // manipulate the state of the stream to be good
99    is.clear(std::ios::goodbit);
100
101    init(vec);
102  }
103 
104
105  Target::Target(const Target& t, 
106                 const utility::Index& index)
107    : class_map_(t.class_map_)
108  {
109    binary_=t.binary_;
110    classes_.resize(index.size());
111    for (size_t i=0; i<index.size(); i++) {
112      assert(index[i]<t.size());
113      classes_[i]=t.classes_[index[i]];
114    }
115    labels_ = t.labels_;
116  }
117
118
119  Target::~Target(void)
120  {
121  }
122
123  bool Target::binary(size_t i) const
124  {
125    assert(i<size());
126    return binary_[operator()(i)];
127  }
128
129  const std::map<std::string,size_t>& Target::classes(void) const
130  {
131    return class_map_;
132  }
133
134  void Target::init(const std::vector<std::string>& label) 
135  {
136    classes_.clear();
137    classes_.reserve(label.size());
138    class_map_.clear();
139    labels_.clear();
140   
141    for (size_t i=0; i<label.size(); i++) {
142      std::map<std::string,size_t>::iterator iter = 
143        class_map_.lower_bound(label[i]);
144
145      // label in map
146      if (iter != class_map_.end() && 
147          !(class_map_.key_comp()(label[i],iter->first)) ){
148
149        classes_.push_back(iter->second);
150      }
151      // label not found in map
152      else{
153        classes_.push_back(class_map_.size());
154        class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 
155        labels_.push_back(label[i]);
156      }
157    }
158    // setting binary to false for every class
159    binary_=std::vector<char>(nof_classes(),false);
160   
161    set_binary(0,true);
162  }
163
164  const std::vector<std::string>& Target::labels(void)
165  {
166    return labels_;
167  }
168
169  size_t Target::nof_classes(void) const
170  {
171    return classes().size();
172  }
173
174  void Target::random_shuffle(void)
175  {
176    random::random_shuffle(classes_.begin(), classes_.end());
177  }
178
179  void Target::set_binary(size_t i, bool b)
180  {
181    assert(i<nof_classes());
182    binary_[i]=b;
183  }
184
185  size_t Target::size(const std::string& label) const 
186  {
187    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);   
188    if(i==class_map_.end())
189      return 0;
190    assert(i->first==label);
191    return size(i->second);
192  }
193 
194  size_t Target::size(void) const
195  {
196    return classes_.size();
197  }
198
199  size_t Target::size(size_t cl) const
200  {
201    return std::count(classes_.begin(),classes_.end(),cl);
202  }
203
204  size_t Target::operator()(size_t sample) const
205  {
206    assert(sample<size());
207    return classes_[sample];
208  }
209
210  size_t Target::operator[](size_t sample) const
211  {
212    return this->operator()(sample);
213  }
214
215  const Target& Target::operator=(const Target& other)
216  {
217    binary_ = other.binary_;
218    classes_ = other.classes_;
219    class_map_ = other.class_map_;
220    labels_ = other.labels_;
221    return *this;
222  }
223
224
225  std::ostream& operator<<(std::ostream& s, const Target& a)
226  {
227    for (size_t j = 0; j < a.size(); ++j) {
228      s << a(j);
229      if ( (j+1)<a.size() )
230        s << " ";
231    }
232
233    return s;
234  }
235
236
237}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.