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

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • 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 "yat/classifier/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 classifier {
40
41  Target::Target(const std::vector<std::string>& label)
42  {
43    init(label);
44  }
45 
46  Target::Target(const Target& t, 
47                 const std::vector<size_t>& index)
48    : class_map_(t.class_map_)
49  {
50    binary_=t.binary_;
51    classes_.resize(index.size());
52    for (size_t i=0; i<index.size(); i++) {
53      assert(index[i]<t.size());
54      classes_[i]=t.classes_[index[i]];
55    }
56    labels_ = t.labels_;
57  }
58
59
60  Target::Target(std::istream& is, char sep) 
61    throw (utility::IO_error,std::exception)
62  {
63    std::vector<std::string> vec;
64    std::string word;
65    bool ok=true;
66    while(ok) {
67      if(sep=='\0')
68        ok=(is>>word);
69      else 
70        ok=getline(is, word, sep);
71
72      // ignore empty words
73      if (!word.size() || !ok )
74        continue;
75     
76      vec.push_back(word);
77    }
78
79    // manipulate the state of the stream to be good
80    is.clear(std::ios::goodbit);
81
82    init(vec);
83  }
84 
85  Target::~Target(void)
86  {
87  }
88
89  void Target::init(const std::vector<std::string>& label) 
90  {
91    classes_.clear();
92    classes_.reserve(label.size());
93    class_map_.clear();
94    labels_.clear();
95   
96    for (size_t i=0; i<label.size(); i++) {
97      std::map<std::string,size_t>::iterator iter = 
98        class_map_.lower_bound(label[i]);
99
100      // label in map
101      if (iter != class_map_.end() && 
102          !(class_map_.key_comp()(label[i],iter->first)) ){
103
104        classes_.push_back(iter->second);
105      }
106      // label not found in map
107      else{
108        classes_.push_back(class_map_.size());
109        class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 
110        labels_.push_back(label[i]);
111      }
112    }
113    // setting binary to false for every class
114    binary_=std::vector<char>(nof_classes(),false);
115   
116    set_binary(0,true);
117  }
118
119  void Target::random_shuffle(void)
120  {
121    random::DiscreteUniform d;
122    std::random_shuffle(classes_.begin(), classes_.end(),d);
123  }
124
125  const size_t Target::size(const std::string& label) const 
126  {
127    std::map<std::string,size_t>::const_iterator i=class_map_.find(label);   
128    if(i==class_map_.end())
129      return 0;
130    assert(i->first==label);
131    return size(i->second);
132  }
133 
134  const size_t Target::size(size_t cl) const
135  {
136    return std::count(classes_.begin(),classes_.end(),cl);
137  }
138
139
140  std::ostream& operator<<(std::ostream& s, const Target& a)
141  {
142    for (size_t j = 0; j < a.size(); ++j) {
143      s << a(j);
144      if ( (j+1)<a.size() )
145        s << " ";
146    }
147
148    return s;
149  }
150
151
152  const Target& Target::operator=(const Target& other)
153  {
154    binary_ = other.binary_;
155    classes_ = other.classes_;
156    class_map_ = other.class_map_;
157    labels_ = other.labels_;
158    return *this;
159  }
160
161
162}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.