LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
line.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef CONLLU_LINE_H
7#define CONLLU_LINE_H
8
9#include <cstdint>
10#include <string>
11#include <vector>
12#include <map>
13#include <set>
14#include <stdexcept>
15
16namespace deeplima
17{
19{
20public:
21 virtual void read(const std::string& buf, char sep = '\t') = 0;
22 virtual std::string write(char sep = '\t') const = 0;
23};
24
25namespace CoNLLU
26{
27struct idx_t
28{
29 typedef uint16_t base_int_t;
30 base_int_t _first; // First word in token.
31 base_int_t _last; // Last word in token.
32 // In case of single word token _idx_first == _idx_last
33 base_int_t _sub; // index after dot in case of empty nodes (i.1)
34 // Empty nodes start from 1.
35 // Zero in this field means this is a real word.
36
37 idx_t() : _first(0), _last(0), _sub(0) { }
38 idx_t(base_int_t first, base_int_t last, base_int_t sub = 0) : _first(first), _last(last), _sub(sub) { }
39 explicit idx_t(base_int_t first) : _first(first), _last(first), _sub(0) { }
40 explicit idx_t(const std::string& s)
41 {
42 if (!parse(s))
43 {
44 throw std::logic_error("Can't parse string \"" + s + "\" as idx_t");
45 }
46 }
47
48 inline bool is_multiword() const
49 {
50 return _first != _last;
51 }
52
53 inline bool is_empty() const
54 {
55 return _sub != 0;
56 }
57
58 inline bool is_real_word() const
59 {
60 return _first == _last && 0 == _sub;
61 }
62
63 bool parse(const std::string& s);
64 std::string serialize() const;
65};
66
67class Line : public ISerializable, public std::vector<std::string>
68{
69public:
70 explicit Line(const std::string& buf, char sep = '\t')
71 {
72 read(buf, sep);
73 }
74
75 Line() = default;
76 virtual ~Line() = default;
77
78 virtual void read(const std::string& buf, char sep = '\t');
79 virtual std::string write(char sep = '\t') const;
80};
81
83{
84public:
85 explicit CoNLLULine(const std::string& buf, char sep = '\t')
86 {
87 read_impl(buf, sep);
88 }
89
90 explicit CoNLLULine(const Line& line)
91 : _empty(false)
92 {
93 init_impl(line);
94 }
95
96 virtual ~CoNLLULine() = default;
97
98 virtual void read(const std::string& buf, char sep = '\t')
99 {
100 read_impl(buf, sep);
101 }
102
103 virtual std::string write(char sep = '\t') const;
104
106 {
108 feature_t(const std::string& key, const std::string& value) : _key(key) { _values.insert(value); }
109 feature_t(const std::string& key, const std::set<std::string>& values) : _key(key), _values(values) { }
110
111 std::string _key;
112 std::set<std::string> _values;
113
114 std::string serialize() const;
115 };
116
117 struct dep_t
118 {
119 dep_t(idx_t head, std::string deprel) : _head(head), _deprel(deprel) { }
120 dep_t() : _head(0) { }
121
123 std::string _deprel;
124
125 std::string serialize() const;
126 };
127
128 inline bool is_token_line() const
129 {
130 return (!_empty) && (_comment.size() == 0);
131 }
132
133 inline bool is_comment_line() const
134 {
135 return (!_empty) && (_comment.size() > 0);
136 }
137
138 inline bool is_empty_line() const
139 {
140 return _empty;
141 }
142
143 inline bool is_real_word_line() const
144 {
145 return (!_empty) && (_comment.size() == 0) && (_idx.is_real_word());
146 }
147
148 inline const idx_t& idx() const
149 {
150 return _idx;
151 }
152
153 inline const std::string& form() const
154 {
155 return _form;
156 }
157
158 inline const std::string& lemma() const
159 {
160 return _lemma;
161 }
162
163 inline const std::string& upos() const
164 {
165 return _upos;
166 }
167
168 inline const std::string& xpos() const
169 {
170 return _xpos;
171 }
172
173 inline const std::string& deprel() const
174 {
175 return _deprel;
176 }
177
178 inline const std::map<std::string, std::set<std::string>>& feats() const
179 {
180 return _feats;
181 }
182
183 inline std::string feats_str() const
184 {
185 return serialize_feats();
186 }
187
188 inline const idx_t& head() const
189 {
190 return _head;
191 }
192
193 inline bool get_binary_feature(const std::string& feat_name) const
194 {
195 const auto it = _feats.find(feat_name);
196 if (_feats.end() == it)
197 {
198 return false;
199 }
200 if (it->second.end() != it->second.find("Yes") || it->second.end() != it->second.find("yes"))
201 {
202 return true;
203 }
204 return false;
205 }
206
207 inline bool is_foreign() const
208 {
209 return get_binary_feature("Foreign");
210 }
211
212 inline bool is_typo() const
213 {
214 return get_binary_feature("Typo");
215 }
216
217 static bool parse_feats(const std::string& s, std::map<std::string, std::set<std::string>>& _feats);
218
219protected:
220 virtual void init(const Line& line)
221 {
222 init_impl(line);
223 }
224
225 void init_impl(const Line& line);
226
227 inline void read_impl(const std::string& buf, char sep = '\t')
228 {
229 _empty = true;
230
231 if (buf.size() > 0)
232 {
233 _empty = false;
234
235 if ('#' == buf[0])
236 {
237 _comment = buf;
238 }
239 else
240 {
241 init_impl(Line(buf, sep));
242 }
243 }
244 }
245
246 // empty line?
247 bool _empty;
248
249 // comment (in case of comment line)
250 std::string _comment;
251
252 // ID
254
255 // FORM
256 std::string _form;
257
258 // LEMMA
259 std::string _lemma;
260
261 // UPOS
262 std::string _upos;
263
264 // XPOS
265 std::string _xpos;
266
267 // FEATS
268 std::map<std::string, std::set<std::string>> _feats;
269
270 // HEAD
272
273 // DEPREL
274 std::string _deprel;
275
276 // DEPS
277 std::vector<dep_t> _deps;
278
279 // MISC
280 std::map<std::string, std::string> _misc;
281
282 bool parse_feats(const std::string& s);
283 bool parse_deps(const std::string& s);
284 bool parse_misc(const std::string& s);
285 bool parse_string_field(const std::string& s, std::string& out, bool raw=false);
286
287 std::string serialize_feats() const;
288 std::string serialize_deps() const;
289 std::string serialize_misc() const;
290 std::string serialize_string(const std::string& s) const;
291};
292
293} // namespace CoNLLU
294} // namespace deeplima
295
296#endif
bool parse_deps(const std::string &s)
Definition line.cpp:172
bool is_empty_line() const
Definition line.h:138
std::string serialize_string(const std::string &s) const
Definition line.cpp:304
std::string feats_str() const
Definition line.h:183
std::map< std::string, std::string > _misc
Definition line.h:280
std::vector< dep_t > _deps
Definition line.h:277
const std::map< std::string, std::set< std::string > > & feats() const
Definition line.h:178
const idx_t & head() const
Definition line.h:188
const std::string & deprel() const
Definition line.h:173
const std::string & form() const
Definition line.h:153
CoNLLULine(const Line &line)
Definition line.h:90
bool get_binary_feature(const std::string &feat_name) const
Definition line.h:193
const std::string & lemma() const
Definition line.h:158
virtual std::string write(char sep='\t') const
Definition line.cpp:53
bool is_comment_line() const
Definition line.h:133
const idx_t & idx() const
Definition line.h:148
bool parse_misc(const std::string &s)
Definition line.cpp:198
bool parse_string_field(const std::string &s, std::string &out, bool raw=false)
Definition line.cpp:236
bool is_real_word_line() const
Definition line.h:143
void read_impl(const std::string &buf, char sep='\t')
Definition line.h:227
virtual void read(const std::string &buf, char sep='\t')
Definition line.h:98
bool is_foreign() const
Definition line.h:207
bool is_token_line() const
Definition line.h:128
std::string serialize_misc() const
Definition line.cpp:286
const std::string & upos() const
Definition line.h:163
std::string serialize_feats() const
Definition line.cpp:250
CoNLLULine(const std::string &buf, char sep='\t')
Definition line.h:85
virtual void init(const Line &line)
Definition line.h:220
const std::string & xpos() const
Definition line.h:168
std::map< std::string, std::set< std::string > > _feats
Definition line.h:268
void init_impl(const Line &line)
Definition line.cpp:82
virtual ~CoNLLULine()=default
static bool parse_feats(const std::string &s, std::map< std::string, std::set< std::string > > &_feats)
Definition line.cpp:142
std::string serialize_deps() const
Definition line.cpp:268
Line(const std::string &buf, char sep='\t')
Definition line.h:70
virtual void read(const std::string &buf, char sep='\t')
Definition line.cpp:20
virtual ~Line()=default
virtual std::string write(char sep='\t') const
Definition line.cpp:37
virtual std::string write(char sep='\t') const =0
virtual void read(const std::string &buf, char sep='\t')=0
dep_t(idx_t head, std::string deprel)
Definition line.h:119
std::string serialize() const
Definition line.cpp:336
feature_t(const std::string &key, const std::string &value)
Definition line.h:108
std::set< std::string > _values
Definition line.h:112
feature_t(const std::string &key, const std::set< std::string > &values)
Definition line.h:109
base_int_t _first
Definition line.h:30
uint16_t base_int_t
Definition line.h:29
bool parse(const std::string &s)
Definition treebank.cpp:385
bool is_multiword() const
Definition line.h:48
bool is_real_word() const
Definition line.h:58
idx_t(base_int_t first)
Definition line.h:39
bool is_empty() const
Definition line.h:53
base_int_t _sub
Definition line.h:33
idx_t(const std::string &s)
Definition line.h:40
idx_t(base_int_t first, base_int_t last, base_int_t sub=0)
Definition line.h:38
std::string serialize() const
Definition treebank.cpp:420
base_int_t _last
Definition line.h:31