LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
DefaultProperties.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6// NAUTITIA
7//
8// jys 22-OCT-2002
9//
10// DefaultProperties is the implementation of the last module of
11// Morphological Analysis. Each token from the main tokens
12// path which remains without linguuistic properties receives
13// properties by default found into dictionnary using Tokenizer
14// status names as keys.
15//<if token has any linguistic properties, directly or by its
16// orthographic alternatives, default properties are not applied>
17
18#include "DefaultProperties.h"
19
31
32#include <fstream>
33
34using namespace boost;
35using namespace Lima::Common::MediaticData;
37using namespace std;
38
39namespace Lima
40{
41namespace LinguisticProcessing
42{
43namespace MorphologicAnalysis
44{
45
47
48DefaultProperties::DefaultProperties() : m_language(std::numeric_limits<unsigned char>::max())
49{}
50
53
56 Manager* manager)
57{
59 m_language = manager->getInitializationParameters().media;
60 std::deque<std::string> skipUnmarkStatus;
61 try
62 {
63 QString file = Common::Misc::findFileInPaths(Common::MediaticData::MediaticData::single().getResourcesPath().c_str(), unitConfiguration.getParamsValueAtKey("defaultPropertyFile").c_str());
64 readDefaultsFromFile(file.toUtf8().constData());
65 }
67 {
68 LERROR << "no param 'defaultPropertyFile' in DefaultProperties group for language " << (int) m_language;
70 }
71 try
72 {
73 string dico = unitConfiguration.getParamsValueAtKey("charChart");
74 auto res = LinguisticResources::single().getResource(m_language,dico);
75 m_charChart = std::dynamic_pointer_cast< FlatTokenizer::CharChart >(res);
76 }
78 {
79 LERROR << "no param 'charChart' in DefaultProperties group for language " << (int) m_language;
81 }
82
83 try
84 {
85 skipUnmarkStatus=unitConfiguration.getListsValueAtKey("skipUnmarkStatus");
86 }
88 {
89 // empty display
90 }
91 //m_skipUnmarkStatus is a set of tokenization status that don't need desaccentuation like t_dot_number. We keep the dot.
92 for( std::deque<std::string>::iterator src = skipUnmarkStatus.begin() ; src != skipUnmarkStatus.end() ; src++ )
93 {
94 m_skipUnmarkStatus.insert(Common::Misc::utf8stdstring2limastring(*src));
95 }
96
97}
98
105 AnalysisContent& analysis) const
106{
107 Lima::TimeUtilsController timer("DefaultProperties");
109
110 auto tokenList=std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("AnalysisGraph"));
111 LinguisticGraph* g=tokenList->getGraph();
112
113 try
114 {
116 boost::tie(v, vEnd) = vertices(*g);
117
118 for (; v != vEnd; v++)
119 {
120 MorphoSyntacticData* currentData=get(vertex_data, *g, *v);
121 if (currentData == 0)
122 {
123 continue;
124 } // NULL for first and last vertex
125
126 Token* currentToken=get(vertex_token, *g, *v);
127
128 //<if token has any linguistic properties, directly or by its
129 // orthographic alternatives, default properties are not applied>
130 if (currentData->empty())
131 {
132 std::map<LimaString,std::vector<LinguisticCode> >::const_iterator it=m_defaults.find(currentToken->status().defaultKey());
133 if (it!=m_defaults.end())
134 {
136 elem.inflectedForm=currentToken->form();
137 LimaString str=currentToken->stringForm();
138 if(m_skipUnmarkStatus.find(currentToken->status().defaultKey())==m_skipUnmarkStatus.end())
139 {
140 LimaString unmarked = m_charChart->unmark(currentToken->stringForm());
141 if (! unmarked.isEmpty())
142 {
143 str=unmarked;
144 }
145 }
147 elem.normalizedForm=elem.lemma;
148 elem.type=UNKNOWN_WORD;
149
150 for (std::vector<LinguisticCode>::const_iterator codeItr=it->second.begin();
151 codeItr!=it->second.end();
152 codeItr++)
153 {
154 elem.properties=*codeItr;
155 currentData->push_back(elem);
156 }
157 }
158 else
159 {
160 LWARN << "No default property for "
161 << Common::Misc::limastring2utf8stdstring(currentToken->stringForm()) << ". Status : "
163 }
164 }
165 }
166 }
167 catch (std::exception &exc)
168 {
170 LWARN << "Exception in Default Properties : " << exc.what();
171 return UNKNOWN_ERROR;
172 }
173
174 return SUCCESS_ID;
175}
176
177void DefaultProperties::readDefaultsFromFile(const std::string& filename)
178{
180 LINFO << "read default properties from file : " << filename;
181 ifstream fin(filename.c_str(), std::ifstream::binary);
182 string line;
183 string type;
184 LinguisticCode props;
185 while (fin.good() && !fin.eof())
186 {
188 if (line.size()>0)
189 {
190 istringstream is(line);
191 is >> type;
192 is >> props;
193#ifdef DEBUG_LP
194 LDEBUG << "read default " << type << " => " << props;
195#endif
196 if (props == L_NONE)
197 {
198 LIMA_EXCEPTION("Error: props == 0");
199 }
200 m_defaults[Common::Misc::utf8stdstring2limastring(type)].push_back(props);
201 }
202 }
203}
204
205} // MorphologicAnalysis
206} // LinguisticProcessing
207} // Lima
#define DEFAULTPROPERTIES_CLASSID
#define LWARN
Definition LimaCommon.h:160
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
Definition LimaCommon.h:293
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
LinguisticGraph::vertex_iterator LinguisticGraphVertexIt
@ vertex_token
@ vertex_data
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
#define MORPHOLOGINIT
Defines a Factory to create Object of type Base.
#define L_NONE
Definition StdBitset.h:338
Holds all data that pass through the ProcessUnits Analysis data are shared pointers,...
std::shared_ptr< AnalysisData > getData(const QString &id)
return AnalysisData by id
const FsaStringsPool & stringsPool(MediaId med) const
std::deque< std::string > & getListsValueAtKey(const std::string &key)
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
LimaStatusCode process(AnalysisContent &analysis) const override
Each token of the specified path without dictionnary entry is searched into the specified dictionary.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
This file contains a class to control log of informations about time, such as logging cumulated time ...
std::string readLine(std::istream &inputFile)
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< MediaProcessUnit, DefaultProperties > defaultPropertiesFactory(DEFAULTPROPERTIES_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ UNKNOWN_ERROR
Definition LimaCommon.h:238
@ SUCCESS_ID
Definition LimaCommon.h:237
QString LimaString
Definition LimaString.h:33
STL namespace.