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

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

Addresses #170..

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