LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
TStatus.cpp
Go to the documentation of this file.
1// Copyright 2002-2019 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
17#include "TStatus.h"
20
21namespace Lima {
22namespace LinguisticProcessing {
23namespace LinguisticAnalysisStructure {
24
26{
27 friend class TStatus;
28
29public:
33 AlphaCapitalType capital,
34 AlphaRomanType roman,
35 bool isHyphen,
36 bool isPossessive,
37 bool isConcatAbbrev,
38 bool isTwitter,
39 NumericType numeric,
40 StatusType status);
41 virtual ~TStatusPrivate();
42
43 // Assignment operator. Copies the specified TStatus
44 // into self. Returns a reference to self.
46
47 // comparison operator (so that status can be sorted)
48 bool operator<(const TStatusPrivate& s) const;
49 bool operator==(const TStatusPrivate& s) const;
50
59 std::vector<Lima::LimaString> _defaultKey;
60
61};
62
64_capital(T_NULL_CAPITAL),
65_roman(T_NULL_ROMAN),
66_isHyphen(false),
67_isPossessive(false),
68m_isConcatAbbrev(false),
69m_isTwitter(false),
70_numeric(T_NULL_NUM),
71_status(T_NULL_STATUS),
72_defaultKey() {
74}
75
77 _capital(tStatus._capital),
78 _roman(tStatus._roman),
79 _isHyphen(tStatus._isHyphen),
80 _isPossessive(tStatus._isPossessive),
81 m_isConcatAbbrev(tStatus.m_isConcatAbbrev),
82 m_isTwitter(tStatus.m_isTwitter),
83 _numeric(tStatus._numeric),
84 _status(tStatus._status),
85 _defaultKey(tStatus._defaultKey)
86{
87}
88
90 AlphaRomanType roman,
91 bool isHyphen,
92 bool isPossessive,
93 bool isConcatAbbrev,
94 bool isTwitter,
95 NumericType numeric,
96 StatusType status) :
97_capital(capital),
98_roman(roman),
99_isHyphen(isHyphen),
100_isPossessive(isPossessive),
101m_isConcatAbbrev(isConcatAbbrev),
102m_isTwitter(isTwitter),
103_numeric(numeric),
104_status(status),
105_defaultKey() {
107}
108
110
111// Assignment operator. Copies the specified TStatus
112// into self. Returns a reference to self.
114 if (this != &tStatus) {
115 _capital = tStatus._capital;
116 _roman = tStatus._roman;
117 _isHyphen = tStatus._isHyphen;
120 m_isTwitter = tStatus.m_isTwitter;
121 _numeric = tStatus._numeric;
122 _status = tStatus._status;
123 _defaultKey = tStatus._defaultKey;
124 }
125 return *this;
126}
127
129 return (_capital == s._capital &&
130 _roman == s._roman &&
131 _isHyphen == s._isHyphen &&
135 _numeric == s._numeric &&
136 _status == s._status &&
138}
139
140// Comparison operator
142 // only need an order:
143
144// LOGINIT("LP::Automaton");
145// std::ostringstream oss1,oss2;
146// TStatus(*this).outputXML(oss1);
147// TStatus(s).outputXML(oss2);
148// LDEBUG << "comparing TStatus "
149// << oss1.str()
150// << " with "
151// << oss2.str();
152
153 if (!_defaultKey.empty() && !_defaultKey.back().isEmpty() &&
154 !s._defaultKey.empty() && !s._defaultKey.back().isEmpty()) {
155 return (_defaultKey.back() < s._defaultKey.back());
156 }
157 else {
158 if (_status < s._status) {
159 //LDEBUG << "return true on status";
160 return true;
161 }
162 if (_status == s._status) {
163 switch(_status) {
164 case T_ALPHA:
165 if (_capital < s._capital) {
166 //LDEBUG << "return true on capital";
167 return true;
168 }
169 if (_capital == s._capital) {
170 if (_roman < s._roman) {
171 //LDEBUG << "return true on roman";
172 return true;
173 }
174 }
175 //LDEBUG << "return false on other alpha";
176 return false;
177 case T_NUMERIC:
178 if (_numeric < s._numeric) {
179 //LDEBUG << "return true on numeric";
180 return true;
181 }
182 //LDEBUG << "return false on other numeric";
183 return false;
184 default:
185 //LDEBUG << "return false on other status";
186 return false;
187 }
188 }
189 }
190 //LDEBUG << "return false on other";
191 return false;
192}
193
194// ===================================================
195
197{
198}
199
200TStatus::TStatus(const TStatus& tStatus) : m_d(new TStatusPrivate(*tStatus.m_d))
201{
202}
203
205 AlphaRomanType roman,
206 bool isHyphen,
207 bool isPossessive,
208 bool isConcatAbbrev,
209 bool isTwitter,
210 NumericType numeric,
211 StatusType status) :
212 m_d(new TStatusPrivate(capital, roman, isHyphen, isPossessive, isConcatAbbrev, isTwitter, numeric, status))
213{
214}
215
217{
218 delete m_d;
219}
220
221// Assignment operator. Copies the specified TStatus
222// into self. Returns a reference to self.
224 if (this != &tStatus) {
225 *m_d = *tStatus.m_d;
226 }
227 return *this;
228}
229
230bool TStatus::operator==(const TStatus& s) const {
231 return (*m_d == *s.m_d);
232}
233
234// Comparison operator
235bool TStatus::operator<(const TStatus& s) const {
236 // only need an order:
237 return *this->m_d < *s.m_d;
238}
239
240// reset Status values
244 m_d->_isHyphen = false;
245 m_d->_isPossessive = false;
246 m_d->m_isConcatAbbrev = false;
247 m_d->m_isTwitter = false;
250 m_d->_defaultKey.clear();
252}
253
255 m_d->_capital = alphaCapital;
256}
257
259 m_d->_roman = alphaRoman;
260}
261
262void TStatus::setAlphaHyphen(bool isAlphaHyphen) {
264}
265
266void TStatus::setAlphaPossessive(bool isAlphaPossessive) {
268}
269
270void TStatus::setTwitter(bool isTwitter) {
272}
273
274void TStatus::setNumeric(const NumericType numeric) {
275 m_d->_numeric = numeric;
276}
277
278void TStatus::setStatus(const StatusType status) {
279 m_d->_status = status;
280}
281
285
289
291 return m_d->_isHyphen;
292}
293
295 return m_d->_isPossessive;
296}
297
299 return m_d->m_isConcatAbbrev;
300}
301
302bool TStatus::isTwitter() const {
303 return m_d->m_isTwitter;
304}
305
306void TStatus::setAlphaConcatAbbrev(bool isConcatAbbrev) {
307 m_d->m_isConcatAbbrev = isConcatAbbrev;
308}
309
311 return m_d->_numeric;
312}
313
315 return m_d->_status;
316}
317
319// return m_d->_defaultKey.empty() ? LimaString() : m_d->_defaultKey.back();
320 if (m_d->_defaultKey.empty())
321 throw std::runtime_error("default key vector cannot be empty");
322 return m_d->_defaultKey.back();
323}
324
326#ifdef DEBUG_LP
328 LTRACE << "TStatus::setDefaultKey to" << Common::Misc::limastring2utf8stdstring(defaultKey);
329 LTRACE << "TStatus::setDefaultKey previous:" << ((m_d->_defaultKey.empty())?LimaString("EMPTY"):m_d->_defaultKey.back());
330 if (!m_d->_defaultKey.empty())
331 {
332 LTRACE << "TStatus::setDefaultKey previous:" << Common::Misc::limastring2utf8stdstring(m_d->_defaultKey.back());
333 }
334#endif
335 // do not push several times the same value
336 if (!m_d->_defaultKey.empty() && m_d->_defaultKey.back() == defaultKey)
337 {
338 return;
339 }
340 // remove roman previous keys if setting to not roman
341 else if (defaultKey == Common::Misc::utf8stdstring2limastring("t_not_roman"))
342 {
343#ifdef DEBUG_LP
344 LDEBUG << "TStatus::setDefaultKey remove roman from all previous";
345#endif
346 std::vector <Lima::LimaString > newDefaultKey;
347 for (std::vector <Lima::LimaString >::const_iterator it = m_d->_defaultKey.begin(); it != m_d->_defaultKey.end(); it++)
348 {
349 if ( *it != Common::Misc::utf8stdstring2limastring("t_cardinal_roman")
350 && (*it != Common::Misc::utf8stdstring2limastring("t_ordinal_roman")) )
351 {
352 newDefaultKey.push_back(*it);
353 }
354 }
355 m_d->_defaultKey = newDefaultKey;
356 }
357 // push the new default
358 else
359 {
360 // remove the fallback default key if present
361 if (m_d->_defaultKey.size()==1 && Common::Misc::limastring2utf8stdstring(m_d->_defaultKey.back()) == "t_fallback")
362 m_d->_defaultKey.pop_back();
363 m_d->_defaultKey.push_back(defaultKey);
364 }
365}
366
367
368std::string TStatus::toString() const
369{
370 std::string result;
371 if (m_d->_status == T_ALPHA)
372 {
373 result += "t_alpha ";
374 result += "t_alpha_capital: ";
375 switch (m_d->_capital)
376 {
377 case T_CAPITAL : result += "t_capital; "; break;
378 case T_SMALL : result += "t_small; "; break;
379 case T_CAPITAL_1ST : result += "t_capital_1st; "; break;
380 case T_ACRONYM : result += "t_acronym; "; break;
381 case T_CAPITAL_SMALL : result += "t_capital_small; "; break;
382 default : ;
383 }
384 if (m_d->_roman != T_NULL_ROMAN)
385 {
386 result += "t_alpha_roman: ";
387 switch (m_d->_roman)
388 {
389 case T_CARDINAL_ROMAN : result += "t_cardinal_roman; "; break;
390 case T_ORDINAL_ROMAN : result += "t_ordinal_roman; "; break;
391 case T_NOT_ROMAN : result += "t_not_roman; "; break;
392 default : ;
393 }
394 }
395 if (m_d->_isHyphen)
396 result += "t_alpha_hyphen; ";
397 if (m_d->_isPossessive)
398 result += "t_alpha_possessive; ";
400 result += "t_alpha_concat_abbrev; ";
401 if (m_d->m_isTwitter)
402 result += "t_twitter; ";
403 }
404 else if (m_d->_status == T_NUMERIC)
405 {
406 result += "t_numeric ";
407 switch (m_d->_numeric)
408 {
409 case T_INTEGER : result += "t_integer; "; break;
410 case T_COMMA_NUMBER : result += "t_comma_number; "; break;
411 case T_DOT_NUMBER : result += "t_dot_number; "; break;
412 case T_FRACTION : result += "t_fraction; "; break;
413 case T_ORDINAL_INTEGER : result += "t_ordinal_integer; "; break;
414 default : result += "unknown; "; break;
415 }
416 }
417 else
418 {
419 result += "t_other ";
420 switch (m_d->_status)
421 {
422 case T_ALPHANUMERIC : result += "t_alphanumeric; "; break;
423 case T_PATTERN : result += "t_pattern; "; break;
424 case T_WORD_BRK : result += "t_word_brk; "; break;
425 case T_SENTENCE_BRK : result += "t_sentence_brk; "; break;
426 case T_PARAGRAPH_BRK : result += "t_paragraph_brk; "; break;
427 default : result += "unknown; "; break;
428 }
429 }
430 result += "t_default: " + Common::Misc::limastring2utf8stdstring(m_d->_defaultKey.empty()?LimaString():m_d->_defaultKey.back());
431 return result;
432}
433
434void TStatus::outputXML(std::ostream& xmlStream) const
435{
436 if (m_d->_status == T_ALPHA)
437 {
438 xmlStream << " <t_alpha>" << std::endl;
439 xmlStream << " <t_alpha_capital>";
440 switch (m_d->_capital)
441 {
442 case T_CAPITAL : xmlStream << "t_capital"; break;
443 case T_SMALL : xmlStream << "t_small"; break;
444 case T_CAPITAL_1ST : xmlStream << "t_capital_1st"; break;
445 case T_ACRONYM : xmlStream << "t_acronym"; break;
446 case T_CAPITAL_SMALL : xmlStream << "t_capital_small"; break;
447 case T_ABBREV : xmlStream << "t_abbrev"; break;
448 default : ;//xmlStream << _capital;
449 }
450 xmlStream << "</t_alpha_capital>" << std::endl;
451 if (m_d->_roman != T_NULL_ROMAN)
452 {
453 xmlStream << " <t_alpha_roman>";
454 switch (m_d->_roman)
455 {
456 case T_CARDINAL_ROMAN : xmlStream << "t_cardinal_roman"; break;
457 case T_ORDINAL_ROMAN : xmlStream << "t_ordinal_roman"; break;
458 case T_NOT_ROMAN : xmlStream << "t_not_roman"; break;
459 default : ;//xmlStream << _roman;
460 }
461 xmlStream << "</t_alpha_roman>" << std::endl;
462 }
463 if (m_d->_isHyphen)
464 xmlStream << " <t_alpha_hyphen/>" << std::endl;
465 if (m_d->_isPossessive)
466 xmlStream << " <t_alpha_possessive/>" << std::endl;
468 xmlStream << " <t_alpha_concat_abbrev/>" << std::endl;
469 if (m_d->m_isTwitter)
470 xmlStream << " <t_twitter/>" << std::endl;
471 xmlStream << " </t_alpha>" << std::endl;
472 }
473 else if (m_d->_status == T_NUMERIC)
474 {
475 xmlStream << " <t_numeric>";
476 switch (m_d->_numeric)
477 {
478 case T_INTEGER : xmlStream << "t_integer"; break;
479 case T_COMMA_NUMBER : xmlStream << "t_comma_number"; break;
480 case T_DOT_NUMBER : xmlStream << "t_dot_number"; break;
481 case T_FRACTION : xmlStream << "t_fraction"; break;
482 case T_ORDINAL_INTEGER : xmlStream << "t_ordinal_integer"; break;
483 default : xmlStream << "unknown"; break;
484 }
485 xmlStream << "</t_numeric>" << std::endl;
486 }
487 else
488 {
489 xmlStream << " <t_other>";
490 switch (m_d->_status)
491 {
492 case T_ALPHANUMERIC : xmlStream << "t_alphanumeric"; break;
493 case T_PATTERN : xmlStream << "t_pattern"; break;
494 case T_WORD_BRK : xmlStream << "t_word_brk"; break;
495 case T_SENTENCE_BRK : xmlStream << "t_sentence_brk"; break;
496 case T_PARAGRAPH_BRK : xmlStream << "t_paragraph_brk"; break;
497 default : xmlStream << "unknown"; break;
498 }
499 xmlStream << "</t_other>" << std::endl;
500 }
501 xmlStream << " <t_default>";
503 xmlStream << "</t_default>" << std::endl;
504}
505
506} // closing namespace Data
507} // closing namespace LinguisticProcessing
508} // closing namespace Lima
#define LTRACE
Definition LimaCommon.h:156
#define LDEBUG
Definition LimaCommon.h:157
#define TOKENIZERLOGINIT
void setDefaultKey(const Lima::LimaString &defaultKey)
Definition TStatus.cpp:325
void setAlphaCapital(const AlphaCapitalType alphaCapital)
Definition TStatus.cpp:254
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
LimaString utf8stdstring2limastring(const std::string &src)
NAUTITIA.
QString LimaString
Definition LimaString.h:33