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

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

adding a wrapper around std::random_shuffle that uses the RNG class. Changed all calls to random_shuffle to use the added function. Acctually all calls already used the RNG class so essentially this change is only cosmetic, but by providing a function I think it is easier to avoid using multiple random generators.

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