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

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.6 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  void Target::init(const std::vector<std::string>& label) 
91  {
92    classes_.clear();
93    classes_.reserve(label.size());
94    class_map_.clear();
95    labels_.clear();
96   
97    for (size_t i=0; i<label.size(); i++) {
98      std::map<std::string,size_t>::iterator iter = 
99        class_map_.lower_bound(label[i]);
100
101      // label in map
102      if (iter != class_map_.end() && 
103          !(class_map_.key_comp()(label[i],iter->first)) ){
104
105        classes_.push_back(iter->second);
106      }
107      // label not found in map
108      else{
109        classes_.push_back(class_map_.size());
110        class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 
111        labels_.push_back(label[i]);
112      }
113    }
114    // setting binary to false for every class
115    binary_=std::vector<char>(nof_classes(),false);
116   
117    set_binary(0,true);
118  }
119
120  void Target::random_shuffle(void)
121  {
122    random::DiscreteUniform d;
123    std::random_shuffle(classes_.begin(), classes_.end(),d);
124  }
125
126  const size_t Target::size(const std::string& label) const 
127  {
128    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);   
129    if(i==class_map_.end())
130      return 0;
131    assert(i->first==label);
132    return size(i->second);
133  }
134 
135  const size_t Target::size(size_t cl) const
136  {
137    return std::count(classes_.begin(),classes_.end(),cl);
138  }
139
140
141  std::ostream& operator<<(std::ostream& s, const Target& a)
142  {
143    for (size_t j = 0; j < a.size(); ++j) {
144      s << a(j);
145      if ( (j+1)<a.size() )
146        s << " ";
147    }
148
149    return s;
150  }
151
152
153  const Target& Target::operator=(const Target& other)
154  {
155    binary_ = other.binary_;
156    classes_ = other.classes_;
157    class_map_ = other.class_map_;
158    labels_ = other.labels_;
159    return *this;
160  }
161
162
163}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.