LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
EventTemplateDefinitionResource.cpp
Go to the documentation of this file.
1// Copyright (C) 2011 by CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
9#include <boost/tokenizer.hpp>
10
12using namespace std;
13
14namespace Lima {
15namespace LinguisticProcessing {
16namespace EventAnalysis {
17
18//----------------------------------------------------------------------
21
22
23//----------------------------------------------------------------------
26m_language(0),
27m_structure(),
28m_elementMapping()
29{
30}
31
34
36{
37 return m_structure.getMention();
38}
39
41{
42 return m_structure.getName();
43}
44
45const std::map<std::string,Common::MediaticData::EntityType>& EventTemplateDefinitionResource::getStructure () const
46{
47 return m_structure.getStructure();
48}
49
50unsigned int EventTemplateDefinitionResource::getCardinality(const std::string& role) const
51{
52 auto it=m_cardinalities.find(role);
53 if (it!=m_cardinalities.end()) {
54 return (*it).second;
55 }
56 // default cardinality for unknown role
57 return(1);
58}
59
60//----------------------------------------------------------------------
62 Manager* manager)
63
64{
66
67 m_language=manager->getInitializationParameters().language;
68 LDEBUG << "EventTemplateDefinitionResource::init() for language " << (int)m_language;
69 // get name
70 try
71 {
72 string name = unitConfiguration.getParamsValueAtKey("templateName");
73 m_structure.setName(name);
74 LDEBUG << "Template name ="<< name;
75
76 }
77 catch (NoSuchParam& ) {
78 LERROR << "No param 'templateName' in EventTemplateDefinitionResource for language " << (int)m_language;
80 }
81
82 try{
83 string nameMention = unitConfiguration.getParamsValueAtKey("templateMention");
84 LDEBUG << "Template mention ="<< nameMention;
85 m_structure.setMention(nameMention);
86 }
87
88 catch (NoSuchParam& ) {
89 LERROR << "No param 'templateMention' in EventTemplateDefinitionResource for language " << (int)m_language;
90 //throw InvalidConfiguration();
91 }
92
93 // get template elements: role and entity types
94 try
95 {
96 map<string,string> elts = unitConfiguration.getMapAtKey("templateElements");
97 LDEBUG << "templateElements .size " << elts.size();
98 for(auto it=elts.begin(),it_end=elts.end();it!=it_end;it++) {
99 LDEBUG << "templateElement =" << (*it).first;
100 m_structure.addTemplateElement((*it).first,(*it).second);
101 }
102 }
103 catch (NoSuchMap& ) {
104 LERROR << "No map 'templateElements' in EventTemplateDefinition for language " << (int)m_language;
105 throw InvalidConfiguration();
106 }
107
108 try
109 {
110 map<string,string> elts = unitConfiguration.getMapAtKey("cardinalities");
111 LDEBUG << "cardinalities .size " << elts.size();
112 for(auto it=elts.begin(),it_end = elts.end();it!=it_end;it++) {
113 LDEBUG << "templateElement =" << (*it).first;
114 m_cardinalities[(*it).first]=stoi((*it).second);
115 }
116 }
117 catch (NoSuchMap& ) {
118 // ignored
119 }
120 // default cardinalities for all elements is 1
121 for (auto it: m_structure.getStructure()) {
122 if (m_cardinalities.find(it.first)==m_cardinalities.end()) {
123 m_cardinalities[it.first]=1;
124 }
125 }
126
127
128 // get element mapping, for template merging
129 LDEBUG << "get elementMapping for template merging";
130 try
131 {
132 map<string,string> mapping = unitConfiguration.getMapAtKey("elementMapping");
133 LDEBUG << "elementMapping .size" << mapping.size();
134 for(auto it=mapping.cbegin(),it_end=mapping.cend();it!=it_end;it++) {
135 const std::string& elements=(*it).second;
136 // comma-separated list of elements
137 boost::char_separator<char> sep(",; ");
138 boost::tokenizer<boost::char_separator<char> > tok(elements,sep);
139 for(auto e=tok.begin(),e_end=tok.end(); e!=e_end;e++) {
140 LDEBUG << "EventTemplateDefinitionResource: add mapping "
141 << (*it).first << ":" << *e;
142 m_elementMapping[(*it).first].insert(*e);
143 }
144 }
145 }
146 catch (NoSuchMap& ) {
147 LDEBUG << "No param 'elementMapping' in EventTemplateDefinition for language "
148 << (int)m_language;
149 }
150
151 LDEBUG << "EventTemplateDefinitionResource::init() Done";
152}
153
155existsMapping(const std::string& eltName1, const std::string& eltName2) const
156{
157 int res=0;
158 map<string,set<string> >::const_iterator it=m_elementMapping.find(eltName1);
159 if (it!=m_elementMapping.end()) {
160 if ( (*it).second.find(eltName2) != (*it).second.end() ) {
161 res=1;
162 }
163 }
164 else {
165 // try other way
166 map<string,set<string> >::const_iterator it=m_elementMapping.find(eltName2);
167 if (it!=m_elementMapping.end()) {
168 if ( (*it).second.find(eltName1) != (*it).second.end() ) {
169 res=-1;
170 }
171 }
172 }
173 //EVENTANALYSISLOGINIT;
174 //LDEBUG << "EventTemplateDefinitionResource::existsMapping : compare " << eltName1 << " and " << eltName2 << "->" << res;
175 return res;
176}
177
178
179
180} // end namespace
181} // end namespace
182} // end namespace
#define EVENTTEMPLATEDEFINITIONRESOURCE_CLASSID
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define EVENTANALYSISLOGINIT
Defines a Factory to create Object of type Base.
std::map< std::string, std::string > & getMapAtKey(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
int existsMapping(const std::string &eltName1, const std::string &eltName2) const
const std::map< std::string, Common::MediaticData::EntityType > & getStructure() const
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
void addTemplateElement(const std::string &role, const std::string entityType)
const std::map< std::string, Common::MediaticData::EntityType > & getStructure(void) const
SimpleFactory< AbstractResource, EventTemplateDefinitionResource > EventTemplateDefinitionResourceFactory(EVENTTEMPLATEDEFINITIONRESOURCE_CLASSID)
NAUTITIA.
STL namespace.