LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
DeepTokenizerBase.cpp
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "DeepTokenizerBase.h"
7
8#include <QtCore/QRegularExpression>
9
11
12using namespace std;
14using namespace Lima::Common::Misc;
15
16namespace Lima
17{
19{
20
21// set default key in status according to other elements in status
23{
24 string defaultKey;
25 switch (curSettings.getStatus()) {
26 case T_ALPHA : {
27 switch (curSettings.getAlphaCapital()) {
28 case T_CAPITAL : defaultKey = "t_capital" ; break;
29 case T_SMALL : defaultKey = "t_small" ; break;
30 case T_CAPITAL_1ST : defaultKey = "t_capital_1st" ; break;
31 case T_ACRONYM : defaultKey = "t_acronym" ; break;
32 case T_CAPITAL_SMALL : defaultKey = "t_capital_small"; break;
33 case T_ABBREV : defaultKey = "t_abbrev" ; break;
34 default : break;
35 }
36 switch (curSettings.getAlphaRoman()) { // Roman supersedes Cardinal
37 case T_CARDINAL_ROMAN : defaultKey = "t_cardinal_roman"; break;
38 case T_ORDINAL_ROMAN : defaultKey = "t_ordinal_roman" ; break;
39 case T_NOT_ROMAN : defaultKey = "t_not_roman" ; break;
40 default : break;
41 }
42 if (curSettings.isAlphaHyphen()) {
43 //no change
44 //defaultKey = "t_alpha_hyphen";
45 }
46 if (curSettings.isAlphaPossessive()) {
47 defaultKey = "t_alpha_possessive";
48 }
49 break;
50 } // end T_ALPHA
51 case T_NUMERIC : {
52 switch (curSettings.getNumeric()) {
53 case T_INTEGER : defaultKey = "t_integer" ; break;
54 case T_COMMA_NUMBER : defaultKey = "t_comma_number" ; break;
55 case T_DOT_NUMBER : defaultKey = "t_dot_number" ; break;
56 case T_FRACTION : defaultKey = "t_fraction" ; break;
57 case T_ORDINAL_INTEGER : defaultKey = "t_ordinal_integer"; break;
58 default: break;
59 }
60 break;
61 }
62 case T_ALPHANUMERIC : defaultKey = "t_alphanumeric" ; break;
63 case T_PATTERN : defaultKey = "t_pattern" ; break;
64 case T_WORD_BRK : defaultKey = "t_word_brk" ; break;
65 case T_SENTENCE_BRK : defaultKey = "t_sentence_brk" ; break;
66 case T_PARAGRAPH_BRK: defaultKey = "t_paragraph_brk" ; break;
67 default: defaultKey = "t_fallback";
68 }
69// #ifdef DEBUG_LP
70// TOKENIZERLOGINIT;
71// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus" << defaultKey;
72// #endif
73 curSettings.setDefaultKey(utf8stdstring2limastring(defaultKey));
74}
75
76// set default key in status according to other elements in status
78{
79// #ifdef DEBUG_LP
80// TOKENIZERLOGINIT;
81// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus"
82// << token.stringForm();
83// #endif
84 static QRegularExpression reCapital("^[[:upper:]]+$", QRegularExpression::UseUnicodePropertiesOption);
85 static QRegularExpression reSmall("^[[:lower:]]+$", QRegularExpression::UseUnicodePropertiesOption);
86 static QRegularExpression reCapital1st("^[[:upper:]]\\w+$", QRegularExpression::UseUnicodePropertiesOption);
87 static QRegularExpression reAcronym("^([[:upper:]]\\.)+$", QRegularExpression::UseUnicodePropertiesOption);
88 static QRegularExpression reCapitalSmall("^([[:upper:][:lower:]])+$", QRegularExpression::UseUnicodePropertiesOption);
89 static QRegularExpression reAbbrev("^\\w+\\.$", QRegularExpression::UseUnicodePropertiesOption);
90 static QRegularExpression reTwitter("^[@#]\\w+$", QRegularExpression::UseUnicodePropertiesOption);
91 static QRegularExpression reAlphaHyphen("^\\w+[\\-]\\w+$", QRegularExpression::UseUnicodePropertiesOption);
92
93 // t_cardinal_roman
94 static QRegularExpression reCardinalRoman("^(?=[MDCLXVI])M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$");
95 // t_ordinal_roman
96 static QRegularExpression reOrdinalRoman("^(?=[MDCLXVI])M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})(st|nd|d|th|er|ème)$");
97 // t_integer
98 static QRegularExpression reInteger("^\\d+$");
99 // t_comma_number
100 static QRegularExpression reCommaNumber("^\\d+,\\d$");
101 // t_dot_number
102 static QRegularExpression reDotNumber("^\\d\\.\\d$");
103 // t_fraction
104 static QRegularExpression reFraction("^\\d([.,]\\d+)?/\\d([.,]\\d+)?$");
105 // t_ordinal_integer
106 static QRegularExpression reOrdinalInteger("^\\d+(st|nd|d|th|er|ème)$");
107 // t_alphanumeric
108 static QRegularExpression reAlphanumeric("^[\\d[:lower:][:upper:]]+$", QRegularExpression::UseUnicodePropertiesOption);
109 static QRegularExpression reSentenceBreak("^[;.!?]$");
110
111 TStatus curSettings;
112 if (reCapital.match(token.stringForm()).hasMatch())
113 {
114// #ifdef DEBUG_LP
115// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_capital";
116// #endif
117 curSettings.setDefaultKey(QString::fromUtf8("t_capital"));
118 }
119 else if (reSmall.match(token.stringForm()).hasMatch())
120 {
121// #ifdef DEBUG_LP
122// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_small";
123// #endif
124 curSettings.setDefaultKey(QString::fromUtf8("t_small"));
125 }
126 else if (reCapital1st.match(token.stringForm()).hasMatch())
127 {
128// #ifdef DEBUG_LP
129// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_capital_1st";
130// #endif
131 curSettings.setDefaultKey(QString::fromUtf8("t_capital_1st"));
132 }
133 else if (reAcronym.match(token.stringForm()).hasMatch())
134 {
135// #ifdef DEBUG_LP
136// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_acronym";
137// #endif
138 curSettings.setDefaultKey(QString::fromUtf8("t_acronym"));
139 }
140 else if (reCapitalSmall.match(token.stringForm()).hasMatch())
141 {
142// #ifdef DEBUG_LP
143// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_capital_small";
144// #endif
145 curSettings.setDefaultKey(QString::fromUtf8("t_capital_small"));
146 }
147 else if (reAbbrev.match(token.stringForm()).hasMatch())
148 {
149// #ifdef DEBUG_LP
150// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_abbrev";
151// #endif
152 curSettings.setDefaultKey(QString::fromUtf8("t_abbrev"));
153 }
154 else if (reTwitter.match(token.stringForm()).hasMatch())
155 {
156// #ifdef DEBUG_LP
157// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_twitter";
158// #endif
159 curSettings.setDefaultKey(QString::fromUtf8("t_twitter"));
160 }
161 else if (reCardinalRoman.match(token.stringForm()).hasMatch())
162 {
163// #ifdef DEBUG_LP
164// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_cardinal_roman";
165// #endif
166 curSettings.setDefaultKey(QString::fromUtf8("t_cardinal_roman"));
167 }
168 else if (reOrdinalRoman.match(token.stringForm()).hasMatch())
169 {
170// #ifdef DEBUG_LP
171// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_ordinal_roman";
172// #endif
173 curSettings.setDefaultKey(QString::fromUtf8("t_ordinal_roman"));
174 }
175 else if (reInteger.match(token.stringForm()).hasMatch())
176 {
177// #ifdef DEBUG_LP
178// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_integer";
179// #endif
180 curSettings.setDefaultKey(QString::fromUtf8("t_integer"));
181 // Numeric transitions (e.g. t_integer>1<31) check the numeric status
182 curSettings.setStatus(T_NUMERIC);
183 curSettings.setNumeric(T_INTEGER);
184 }
185 else if (reCommaNumber.match(token.stringForm()).hasMatch())
186 {
187// #ifdef DEBUG_LP
188// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_comma_number";
189// #endif
190 curSettings.setDefaultKey(QString::fromUtf8("t_comma_number"));
191 // Numeric transitions (e.g. t_integer>1<31) check the numeric status
192 curSettings.setStatus(T_NUMERIC);
193 curSettings.setNumeric(T_COMMA_NUMBER);
194 }
195 else if (reDotNumber.match(token.stringForm()).hasMatch())
196 {
197// #ifdef DEBUG_LP
198// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_dot_number";
199// #endif
200 curSettings.setDefaultKey(QString::fromUtf8("t_dot_number"));
201 // Numeric transitions (e.g. t_integer>1<31) check the numeric status
202 curSettings.setStatus(T_NUMERIC);
203 curSettings.setNumeric(T_DOT_NUMBER);
204 }
205 else if (reFraction.match(token.stringForm()).hasMatch())
206 {
207// #ifdef DEBUG_LP
208// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_fraction";
209// #endif
210 curSettings.setDefaultKey(QString::fromUtf8("t_fraction"));
211 // Numeric transitions (e.g. t_integer>1<31) check the numeric status
212 curSettings.setStatus(T_NUMERIC);
213 curSettings.setNumeric(T_FRACTION);
214 }
215 else if (reOrdinalInteger.match(token.stringForm()).hasMatch())
216 {
217// #ifdef DEBUG_LP
218// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_ordinal_integer";
219// #endif
220 curSettings.setDefaultKey(QString::fromUtf8("t_ordinal_integer"));
221 // Numeric transitions (e.g. t_integer>1<31) check the numeric status
222 curSettings.setStatus(T_NUMERIC);
223 curSettings.setNumeric(T_ORDINAL_INTEGER);
224 }
225 else if (reAlphanumeric.match(token.stringForm()).hasMatch())
226 {
227// #ifdef DEBUG_LP
228// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_alphanumeric";
229// #endif
230 curSettings.setDefaultKey(QString::fromUtf8("t_alphanumeric"));
231 }
232 else if (reAlphaHyphen.match(token.stringForm()).hasMatch())
233 {
234 curSettings.setDefaultKey(QString::fromUtf8("t_alpha_hyphen"));
235 curSettings.setAlphaHyphen(true);
236 }
237 else if (reSentenceBreak.match(token.stringForm()).hasMatch())
238 {
239// #ifdef DEBUG_LP
240// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_sentence_brk";
241// #endif
242 curSettings.setDefaultKey(QString::fromUtf8("t_sentence_brk"));
243 }
244 else // if (reSmall.match(token.stringForm()).hasMatch())
245 {
246// #ifdef DEBUG_LP
247// LDEBUG << "CppUppsalaTokenizerPrivate::computeDefaultStatus t_word_brk (default)";
248// #endif
249 curSettings.setDefaultKey(QString::fromUtf8("t_word_brk"));
250 }
251
252 /*
253 // t_not_roman
254 static QRegularExpression reNotRoman("^$");
255 // t_alpha_concat_abbrev
256 static QRegularExpression reAlphConcatAbbrev("^$");
257 // t_pattern
258 static QRegularExpression rePattern("^$");
259 // t_word_brk
260 static QRegularExpression reWordBreak("^$");
261 // t_sentence_brk
262 static QRegularExpression reSentenceBreak("^$");
263 // t_paragraph_brk
264 static QRegularExpression reParagraphBreak("^$");
265 */
266
267 token.setStatus(curSettings);
268}
269
270} // namespace LinguisticProcessing
271} // namespace Lima
virtual void computeDefaultStatus(LinguisticAnalysisStructure::TStatus &curSettings)
void setDefaultKey(const Lima::LimaString &defaultKey)
Definition TStatus.cpp:325
void setStatus(const TStatus &status)
Set the TStatus of a token.
Definition Token.h:100
LimaString utf8stdstring2limastring(const std::string &src)
NAUTITIA.
STL namespace.