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

Last change on this file since 1134 was 1134, checked in by Peter, 14 years ago

using Index class instead of std::vector<size_t>

  • 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, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Target.h"
27#include "yat/random/random.h"
28#include "yat/utility/Index.h"
29#include "yat/utility/stl_utility.h"
30#include "yat/utility/utility.h"
31
32#include <algorithm>
33#include <cassert>
34#include <iostream>
35#include <map>
36#include <string>
37#include <sstream>
38#include <utility>
39#include <vector>
40
41namespace theplu {
42namespace yat {
43namespace classifier {
44
45  Target::Target(void)
46  {
47  }
48 
49
50  Target::Target(const std::vector<std::string>& label)
51  {
52    init(label);
53  }
54 
55
56  Target::Target(const std::vector<std::string>& label,
57                 const Target& t)
58    : class_map_(t.class_map_)
59  {
60    binary_=t.binary_;
61    labels_ = t.labels_;
62    classes_.reserve(label.size());
63    for (std::vector<std::string>::const_iterator lab=label.begin(); 
64         lab!=label.end(); ++lab) {
65      std::map<std::string,size_t>::iterator i=class_map_.lower_bound(*lab);
66      if (i->first==*lab) // label exists
67        classes_.push_back(i->second);
68      else {
69        binary_.push_back(0);
70        classes_.push_back(labels_.size());
71        class_map_.insert(i, std::make_pair(*lab, labels_.size()));
72        labels_.push_back(*lab);
73      }
74    }
75  }
76
77
78  Target::Target(std::istream& is, char sep) 
79    throw (utility::IO_error,std::exception)
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);
87      else 
88        ok=getline(is, word, sep);
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.