source: trunk/lib/classifier/Target.cc @ 514

Last change on this file since 514 was 514, checked in by Peter, 17 years ago

generalised binary functionality in Target

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: Target.cc 514 2006-02-20 09:45:34Z peter $
2
3#include <c++_tools/classifier/Target.h>
4#include <c++_tools/utility/stl_utility.h>
5#include <c++_tools/utility/utility.h>
6
7#include <cassert>
8#include <iostream>
9#include <map>
10#include <string>
11#include <sstream>
12#include <utility>
13#include <vector>
14
15namespace theplu {
16namespace classifier {
17
18  Target::Target(const std::vector<std::string>& label)
19  {
20    init(label);
21  }
22 
23  Target::Target(const Target& t, 
24                 const std::vector<size_t>& index)
25    : class_map_(t.class_map_)
26  {
27    binary_.resize(index.size());
28    classes_.resize(index.size());
29    for (size_t i=0; i<index.size(); i++) {
30      assert(index[i]<t.size());
31      classes_[i]=t.classes_[index[i]];
32      binary_[i]=t.binary_[index[i]];
33    }
34    labels_ = t.labels_;
35  }
36
37
38  Target::Target(std::istream& is, char sep) 
39    throw (utility::IO_error,std::exception)
40  {
41    std::vector<std::string> vec;
42    std::string word;
43    bool ok=true;
44    while(ok) {
45      if(sep=='\0')
46        ok=(is>>word);
47      else 
48        ok=getline(is, word, sep);
49
50      // ignore empty words
51      if (!word.size() || !ok )
52        continue;
53     
54      vec.push_back(word);
55    }
56
57    // manipulate the state of the stream to be good
58    is.clear(std::ios::goodbit);
59
60    init(vec);
61  }
62 
63  Target::~Target(void)
64  {
65  }
66
67  void Target::init(const std::vector<std::string>& label) 
68  {
69    classes_.clear();
70    classes_.reserve(label.size());
71    class_map_.clear();
72    labels_.clear();
73   
74    for (size_t i=0; i<label.size(); i++) {
75      std::map<std::string,size_t>::iterator iter = 
76        class_map_.lower_bound(label[i]);
77
78      // label in map
79      if (iter != class_map_.end() && 
80          !(class_map_.key_comp()(label[i],iter->first)) ){
81
82        classes_.push_back(iter->second);
83      }
84      // label not found in map
85      else{
86        classes_.push_back(class_map_.size());
87        class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 
88        labels_.push_back(label[i]);
89      }
90    }
91    // setting binary to false for every class
92    binary_=std::vector<short>(label.size(),0);
93   
94    set_binary(0,true);
95
96  }
97
98  //  const Target& Target::operator=(const Target& other)
99  //{
100  //  classes_=other.classes_;   
101  //  class_map_=other.class_map_;   
102  //  labels_=other.labels_;
103  //  return *this;
104  //}
105
106  void Target::set_binary(const size_t target, const bool b)
107  {
108    for (size_t i=0; i<classes_.size(); i++)
109      if (classes_[i]==target)
110        binary_[i]=b;
111  }
112
113
114  std::ostream& operator<<(std::ostream& s, const Target& a)
115  {
116    for (size_t j = 0; j < a.size(); ++j) {
117      s << a(j);
118      if ( (j+1)<a.size() )
119        s << " ";
120    }
121
122    return s;
123  }
124
125
126}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.