mapanalyser.h
5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright (C) 2010 Tomasz Śniatowski, Adam Radziszewski
Part of the libmaca project
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the LICENSE.MACA, LICENSE.SFST, LICENSE.GUESSER, COPYING.LESSER and COPYING files for more details.
*/
#ifndef LIBMACA_MAPANALYSER_H
#define LIBMACA_MAPANALYSER_H
#include <libmaca/morph/morphanalyser.h>
#include <libmaca/util/settings.h>
#include <libtoki/util/confignode.h>
#include <boost/foreach.hpp>
#include <boost/unordered_map.hpp>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <libmaca/typedefs.h>
namespace Maca {
/**
* A standard-container-based analyser template for use with map-like
* containers. Data is loaded from a file in tab-separated format of
* orth, lemma and tag-string triplets.
*
* Distinct variants of this template are provided for case sensitive and
* cas insensitive analysis, and std::map or boost::unordered_map backend.
*
* std::map, case-sensitive configuration class key: \b map-case
* std::map, case-insensitive configuration class key: \b map
* unordered_map, case-sensitive configuration class key: \b hashmap-case
* unordered_map, case-insensitive configuration class key: \b hashmap
*/
template<typename MapT>
class MapAnalyser : public MorphAnalyser
{
public:
/// Constructor for an empty analyser working with a tagset
explicit MapAnalyser(const Corpus2::Tagset* tagset);
/**
* Config node constructor. Recognized keys are:
* - data - the data file to load
*/
explicit MapAnalyser(const Config::Node& cfg);
/// Cloning
MapAnalyser* clone() const;
/// Data file loading function
void load_m_dictionary(const std::string& fn);
/// MorphAnalyser override
bool process_functional(const Toki::Token &t,
boost::function<void (Corpus2::Token*)> sink);
/// Class identifier
static const char* identifier;
/// Registered flag
static bool registered;
private:
/// the orth to analysis map
MapT map_;
};
/// Helper struct for ICU string caseless compare
struct IcuStringCaselessCompare
{
bool operator()(const UnicodeString& u1,
const UnicodeString& u2) const {
return u1.caseCompare(u2, 0) < 0;
}
};
/// Helper struct for ICU hashing
struct IcuHash
{
std::size_t operator()(const UnicodeString& x) const
{
return x.hashCode();
}
};
/// Helper struct for ICU string caseless compare and hashing
struct IcuStringCaselessEqual
{
bool operator()(const UnicodeString& u1,
const UnicodeString& u2) const {
return u1.caseCompare(u2, 0) == 0;
}
std::size_t operator()(const UnicodeString& x) const
{
UnicodeString xx = x;
xx.toLower();
return xx.hashCode();
}
};
/// typedef for a tree-map (std::map) analyser
typedef MapAnalyser<
std::map<
UnicodeString,
std::vector< std::pair<std::string, std::string>
>
>
> StdMapAnalyser;
/// typedef for a tree-map (std::map) analyser, caseless
typedef MapAnalyser<
std::map<
UnicodeString,
std::vector< std::pair<std::string, std::string> >,
IcuStringCaselessCompare
>
> StdMapCaselessAnalyser;
/// typedef for a hashmap (std::unordered_map) analyser
typedef MapAnalyser<
boost::unordered_map<
UnicodeString,
std::vector< std::pair<std::string, std::string> >,
IcuHash
>
> HashMapAnalyser;
/// typedef for a hashmap (std::unordered_map) analyser, caseless
typedef MapAnalyser<
boost::unordered_map<
UnicodeString,
std::vector< std::pair<std::string, std::string> >,
IcuStringCaselessEqual,
IcuStringCaselessEqual
>
> HashMapCaselessAnalyser;
/* implementation */
template<typename MapT>
MapAnalyser<MapT>::MapAnalyser(const Corpus2::Tagset* tagset)
: MorphAnalyser(tagset), map_()
{
}
template<typename MapT>
MapAnalyser<MapT>::MapAnalyser(const Config::Node &cfg)
: MorphAnalyser(cfg), map_()
{
load_m_dictionary(cfg.get<std::string>("data"));
}
template<typename MapT>
MapAnalyser<MapT>* MapAnalyser<MapT>::clone() const
{
MapAnalyser<MapT>* copy = new MapAnalyser<MapT>(&tagset());
copy->map_ = map_;
return copy;
}
template<typename MapT>
void MapAnalyser<MapT>::load_m_dictionary(const std::string &fn)
{
std::ifstream ifs;
Path::Instance().open_stream_or_throw(fn, ifs, "map analyser data");
static const size_t BUFSIZE = 2000;
char buf[BUFSIZE + 1];
while (ifs.good()) {
ifs.getline(buf, BUFSIZE);
//std::vector< boost::iterator_range<const char*> > v;
//std::string b(buf);
size_t len = ifs.gcount();
size_t i = 0;
while (i < BUFSIZE && (buf[i] == ' ' || buf[i] == '\t')) {
++i;
}
if (i + 1 < len) {
std::vector< std::string > v;
// do not include the trailing null
string_range r(buf + i, buf + len - 1);
boost::algorithm::split(v, r, boost::is_any_of("\t "),
boost::algorithm::token_compress_on);
if (v.size() == 3) {
const UnicodeString& key = UnicodeString::fromUTF8(v[0]);
map_[key].push_back(std::make_pair(v[1], v[2]));
} else {
std::cerr << "Invalid map line (" << v.size() << "): "
<< buf << "\n";
}
}
}
}
template<typename MapT>
bool MapAnalyser<MapT>::process_functional(const Toki::Token &t,
boost::function<void (Corpus2::Token*)> sink)
{
typename MapT::const_iterator i;
const UnicodeString& key = t.orth();
i = map_.find(key);
if (i != map_.end()) {
Corpus2::Token* tt = create_from_toki(t);
typedef std::pair<std::string, std::string> sp;
BOOST_FOREACH(const sp& o, i->second) {
tagset().lexemes_into_token(*tt, o.first, o.second);
}
sink(tt);
return true;
} else {
return false;
}
}
} /* end ns Maca */
#endif // LIBMACA_MAPANALYSER_H