LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
AlternativesReader.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 * Copyright (C) 2004-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
10#include "AlternativesReader.h"
13
16
17namespace Lima
18{
19
20namespace LinguisticProcessing
21{
22
23namespace MorphologicAnalysis
24{
25
27 bool confidentMode,
28 bool tryDirect,
29 bool tryUncapitalized,
30 bool tryDesaccentedForm,
31 std::shared_ptr<FlatTokenizer::CharChart> charChart,
32 FsaStringsPool* sp) :
33 m_confidentMode(confidentMode),
34 m_tryDirect(tryDirect),
35 m_tryUncapitalized(tryUncapitalized),
36 m_tryDesaccentedForm(tryDesaccentedForm),
37 m_charChart(charChart),
38 m_sp(sp)
39{}
40
42
44 Token& token,
49{
50#ifdef DEBUG_LP
52#endif
53 const LimaString& str=token.stringForm();
54#ifdef DEBUG_LP
55 LDEBUG << "AlternativesReader::readAlternatives (direct: " << m_tryDirect
56 << " ; uncap: " << m_tryUncapitalized << " ; desac: "
57 << m_tryDesaccentedForm << ")" << str;
58#endif
59 if (m_tryDirect)
60 {
61#ifdef DEBUG_LP
62 LDEBUG << "AlternativesReader::readAlternatives trying direct";
63#endif
64 DictionaryEntry entry=dico.getEntry(token.form(),str);
65 if (!entry.isEmpty())
66 {
67 if (lingInfosHandler && entry.hasLingInfos())
68 {
69#ifdef DEBUG_LP
70 LDEBUG << "AlternativesReader::readAlternatives direct, hasLingInfos";
71#endif
72 entry.parseLingInfos(lingInfosHandler);
73 // if hasLingInfos, assume that infos has been read, then,
74 // in confident mode, we can return
75 if (m_confidentMode) return;
76 }
77 if (concatHandler && entry.hasConcatenated())
78 {
79#ifdef DEBUG_LP
80 LDEBUG << "AlternativesReader::readAlternatives direct, hasConcatenated";
81#endif
82 entry.parseConcatenated(concatHandler);
83 if (m_confidentMode) return;
84 }
85 if (accentedHandler && entry.hasAccentedForms())
86 {
87#ifdef DEBUG_LP
88 LDEBUG << "AlternativesReader::readAlternatives direct, hasAccentedForms";
89#endif
90 entry.parseAccentedForms(accentedHandler);
91 if (m_confidentMode) return;
92 }
93 }
94 }
95 if (m_tryUncapitalized && m_charChart)
96 {
97 LimaString lowerWord = m_charChart->toLower(str);
98#ifdef DEBUG_LP
99 LDEBUG << "AlternativesReader::readAlternatives trying lower:" << lowerWord;
100#endif
101 if (!(lowerWord.isEmpty()) && (lowerWord!=str))
102 {
103#ifdef DEBUG_LP
104 LDEBUG << "add word "
105 << "<marked>" << str << "</marked>"
106 << "<lower>" << lowerWord << "</lower>";
107#endif
108 StringsPoolIndex idx=(*m_sp)[lowerWord];
110 DictionaryEntry entry=dico.getEntry(idx,lowerWord);
111 if (!entry.isEmpty())
112 {
113 if (lingInfosHandler && entry.hasLingInfos())
114 {
115 entry.parseLingInfos(lingInfosHandler);
116 // if hasLingInfos, assume that infos has been read, then,
117 // in confident mode, we can return
118#ifdef DEBUG_LP
119 LDEBUG << "lower word" << lowerWord << "found";
120#endif
121 if (m_confidentMode) return;
122 }
123 if (concatHandler && entry.hasConcatenated())
124 {
125 entry.parseConcatenated(concatHandler);
126#ifdef DEBUG_LP
127 LDEBUG << "lower word" << lowerWord << "concatenated found";
128#endif
129 if (m_confidentMode) return;
130 }
131 if (accentedHandler && entry.hasAccentedForms())
132 {
133 entry.parseAccentedForms(accentedHandler);
134#ifdef DEBUG_LP
135 LDEBUG << "lower word" << lowerWord << "accented found";
136#endif
137 if (m_confidentMode) return;
138 }
139 }
140 }
141 }
142 if (m_tryDesaccentedForm && m_charChart)
143 {
144 LimaString unmarked=m_charChart->unmark(str);
145#ifdef DEBUG_LP
146 LDEBUG << "AlternativesReader::readAlternatives trying unmarked:" << unmarked;
147#endif
148 if ((unmarked!=LimaString()) && (unmarked!=str))
149 {
150#ifdef DEBUG_LP
151 LDEBUG << "add word "
152 << "<marked>" << Common::Misc::limastring2utf8stdstring(str) << "</marked>"
153 << "<unmarked>" << Common::Misc::limastring2utf8stdstring(unmarked) << "</unmarked>"
154 << " to stringpool " << m_sp;
155#endif
156 StringsPoolIndex idx=(*m_sp)[unmarked];
157#ifdef DEBUG_LP
158 LDEBUG << "-> StringPool returned index " << idx;
159#endif
161 DictionaryEntry entry=dico.getEntry(idx,unmarked);
162#ifdef DEBUG_LP
163 LDEBUG << "entry.isEmpty:" << entry.isEmpty();
164#endif
165
166 if (!entry.isEmpty())
167 {
168 #ifdef DEBUG_LP
169 LDEBUG << "confident mode: " << m_confidentMode;
170 LDEBUG << "lingInfosHandler: " << (void*)accentedHandler << " entry.hasLingInfos:" << entry.hasLingInfos();
171#endif
172 if (lingInfosHandler && entry.hasLingInfos())
173 {
174 entry.parseLingInfos(lingInfosHandler);
175 // if hasLingInfos, assume that infos has been read, then,
176 // in confident mode, we can return
177 if (m_confidentMode) return;
178 }
179#ifdef DEBUG_LP
180 LDEBUG << "concatHandler: " << (void*)accentedHandler << " entry.hasConcatenated:" << entry.hasConcatenated();
181 #endif
182 if (concatHandler && entry.hasConcatenated())
183 {
184 entry.parseConcatenated(concatHandler);
185 if (m_confidentMode) return;
186 }
187#ifdef DEBUG_LP
188 LDEBUG << "accentedHandler: " << (void*)accentedHandler << " entry.hasAccentedForms:" << entry.hasAccentedForms();
189#endif
190 if (accentedHandler && entry.hasAccentedForms())
191 {
192 entry.parseAccentedForms(accentedHandler);
193 if (m_confidentMode) return;
194 }
195 }
196 }
198 {
199 StringsPoolIndex idx=(*m_sp)[unmarked];
200 token = Token(idx, unmarked, token.position(), token.length(),token.status());
201#ifdef DEBUG_LP
202 LDEBUG << "AlternativesReader::readAlternatives is an acronym; using simpler form" << unmarked;
203#endif
204 }
205 }
206#ifdef DEBUG_LP
207 LDEBUG << "AlternativesReader::readAlternatives no alternative found;";
208#endif
209}
210
211}
212
213}
214
215}
#define LDEBUG
Definition LimaCommon.h:157
#define MORPHOLOGINIT
virtual DictionaryEntry getEntry(const Lima::LimaString &word) const =0
get dictionary entry for a word
void parseAccentedForms(AbstractDictionaryEntryHandler *handler) const
void parseConcatenated(AbstractDictionaryEntryHandler *handler) const
void parseLingInfos(AbstractDictionaryEntryHandler *handler) const
AlternativesReader(bool confidentMode, bool tryDirect, bool tryUncapitalized, bool tryDesaccentedForm, std::shared_ptr< FlatTokenizer::CharChart > charChart, FsaStringsPool *sp)
void readAlternatives(LinguisticAnalysisStructure::Token &token, const AnalysisDict::AbstractAnalysisDictionary &dico, AnalysisDict::AbstractDictionaryEntryHandler *lingInfosHandler=0, AnalysisDict::AbstractDictionaryEntryHandler *concatHandler=0, AnalysisDict::AbstractDictionaryEntryHandler *accentedHandler=0) const
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
NAUTITIA.
QString LimaString
Definition LimaString.h:33