1 | // $Id: Target.cc 509 2006-02-18 13:47:32Z 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 | |
---|
15 | namespace theplu { |
---|
16 | namespace classifier { |
---|
17 | |
---|
18 | Target::Target(const std::vector<std::string>& label) |
---|
19 | : one_(0) |
---|
20 | { |
---|
21 | init(label); |
---|
22 | } |
---|
23 | |
---|
24 | Target::Target(const Target& t, |
---|
25 | const std::vector<size_t>& index) |
---|
26 | : class_map_(t.class_map_), one_(t.one_) |
---|
27 | { |
---|
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 | } |
---|
33 | labels_ = t.labels_; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | // Peter to Markus, align with gslapi |
---|
38 | Target::Target(std::istream& is, char sep) |
---|
39 | throw (utility::IO_error,std::exception) |
---|
40 | : one_(0) |
---|
41 | { |
---|
42 | std::vector<std::string> vec; |
---|
43 | std::string word; |
---|
44 | bool ok=true; |
---|
45 | while(ok) { |
---|
46 | if(sep=='\0') |
---|
47 | ok=(is>>word); |
---|
48 | else |
---|
49 | ok=getline(is, word, sep); |
---|
50 | |
---|
51 | // ignore empty words |
---|
52 | if (!word.size() || !ok ) |
---|
53 | continue; |
---|
54 | |
---|
55 | vec.push_back(word); |
---|
56 | } |
---|
57 | |
---|
58 | // manipulate the state of the stream to be good |
---|
59 | is.clear(std::ios::goodbit); |
---|
60 | |
---|
61 | init(vec); |
---|
62 | } |
---|
63 | |
---|
64 | Target::~Target(void) |
---|
65 | { |
---|
66 | } |
---|
67 | |
---|
68 | void Target::init(const std::vector<std::string>& label) |
---|
69 | { |
---|
70 | classes_.clear(); |
---|
71 | classes_.reserve(label.size()); |
---|
72 | class_map_.clear(); |
---|
73 | labels_.clear(); |
---|
74 | |
---|
75 | |
---|
76 | for (size_t i=0; i<label.size(); i++) { |
---|
77 | std::map<std::string,size_t>::const_iterator iter = |
---|
78 | class_map_.lower_bound(label[i]); |
---|
79 | |
---|
80 | // label in map |
---|
81 | if (iter != class_map_.end() && |
---|
82 | !(class_map_.key_comp()(label[i],iter->first)) ){ |
---|
83 | |
---|
84 | classes_.push_back(iter->second); |
---|
85 | } |
---|
86 | // label not found in map |
---|
87 | else{ |
---|
88 | classes_.push_back(class_map_.size()); |
---|
89 | // Peter, should use iter to hint on position |
---|
90 | class_map_.insert(std::make_pair(label[i],classes_[i])); |
---|
91 | labels_.push_back(label[i]); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | // const Target& Target::operator=(const Target& other) |
---|
98 | //{ |
---|
99 | // classes_=other.classes_; |
---|
100 | // class_map_=other.class_map_; |
---|
101 | // labels_=other.labels_; |
---|
102 | // return *this; |
---|
103 | //} |
---|
104 | |
---|
105 | std::ostream& operator<<(std::ostream& s, const Target& a) |
---|
106 | { |
---|
107 | for (size_t j = 0; j < a.size(); ++j) { |
---|
108 | s << a(j); |
---|
109 | if ( (j+1)<a.size() ) |
---|
110 | s << " "; |
---|
111 | } |
---|
112 | |
---|
113 | return s; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | }} // of namespace classifier and namespace theplu |
---|