LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
BratDumper.cpp
Go to the documentation of this file.
1// Copyright (C) 2004-2012 by CEA LIST *
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#include "BratDumper.h"
8
21
22#include <boost/algorithm/string.hpp>
23#include <fstream>
24#include <queue>
25#include <tuple>
26
27using namespace std;
28using namespace Lima::Common::AnnotationGraphs;
33using namespace boost;
34
35namespace Lima {
36namespace LinguisticProcessing {
37namespace AnalysisDumpers {
38
40
43m_useNormalizedForms(false)
44{
46 LDEBUG << "BratDumper::BratDumper()";
47}
48
49
52
55 Manager* manager)
56
57{
59 LDEBUG << "BratDumper::init";
60 AbstractIEDumper::init(unitConfiguration,manager);
61 // here, specific parameters for this dumper if different from AbstractIEDumper ones
62 try
63 {
64 string str=unitConfiguration.getParamsValueAtKey("useNorms");
65 if (str=="yes" || str=="1" || str=="true") {
66 m_useNormalizedForms=true;
67 }
68 }
69 catch (Common::XMLConfigurationFiles::NoSuchParam& ) { } // optional keep default
70
71}
72
74outputEntityString(ostream& out,
75 unsigned int entityId,
76 const std::string& entityType,
77 const std::string& entityString,
78 const vector<pair<uint64_t,uint64_t> >& positions,
79 const Automaton::EntityFeatures& entityFeatures, bool noNorm) const
80{
81 out << "T" << entityId << "\t" << entityType << " ";
82 if (positions.size()==0) {
83 // use simple positions
85 LERROR << "BratDumper: no positions given for entity" << entityString;
86 out << 0 << " " << 0;
87 }
88 else {
89 vector<pair<uint64_t,uint64_t> >::const_iterator posIt=positions.begin();
90 out << (*posIt).first << " " << (*posIt).second;
91 for (posIt++; posIt!=positions.end(); posIt++) {
92 out << ";" << (*posIt).first << " " << (*posIt).second;
93 }
94 }
95 out << "\t" << entityString << "\n";
96 if (m_useNormalizedForms && !noNorm) {
97 bool done(false);
98 for (Automaton::EntityFeatures::const_iterator
99 featureItr=entityFeatures.begin(),features_end=entityFeatures.end();
100 featureItr!=features_end; featureItr++)
101 {
102 if (featureItr->getName()=="value") {
103 string valuestr=featureItr->getValueString();
104 // if only whitespaces, do not print it (error in brat format)
105 if (valuestr.find_first_not_of(' ') != std::string::npos) {
106 out << "N" << entityId << "\tReference T" << entityId << " LIMA:1\t" << valuestr << "\n";
107 done=true;
108 break;
109 }
110 }
111 }
112 if (! done) { // backoff to surface form if norm is not found
113 out << "N" << entityId << "\tReference T" << entityId << " LIMA:1\t" << entityString << "\n";
114 }
115 }
116}
117
119outputAttributesString(std::ostream& out,
120 unsigned int entityId,
121 std::map <std::tuple <std::size_t, std::string , std::string >, std::size_t >& mapAttributes) const
122{
123 for(auto iterAttribute = mapAttributes.begin();
124 iterAttribute!=mapAttributes.end();
125 iterAttribute++)
126 {
127 auto feature = iterAttribute->first;
128 std::size_t eId = std::get<0>(feature);
129 if( eId == entityId )
130 {
131 std::size_t attributeId = iterAttribute->second;
132 std::string featName = std::get<1>(feature);
133 std::string featValue = std::get<2>(feature);
134 out << "A" << attributeId << "\t" << featName << " T" << entityId;
135 if(featValue!=""){
136 out << " " << featValue;
137 }
138 out << "\n";
139 }
140 }
141}
142
144outputEntityNorm(ostream& out,
145 unsigned int entityId,
146 const std::string& entityNorm) const
147{
148 out << "N" << entityId << "\tReference T" << entityId << " X:Y\t"
149 << entityNorm << "\n";
150}
151
152unsigned int BratDumper::
153outputRelationString(ostream& out,
154 unsigned int relationId,
155 const std::string& relationType,
156 const std::string& sourceArgString,
157 const std::string& targetArgString) const
158{
159 QString relation = QString::fromUtf8(relationType.c_str());
160 QStringList relations = relation.split(',');
161 unsigned int i = 0;
162 for (auto& rel : relations)
163 {
164 if (rel.isEmpty())
165 {
167 LERROR << "BratDumper::outputRelationString Ignoring relation with empty type:"
168 << relationId << relationType << sourceArgString
169 << targetArgString;
170 continue;
171 }
172 if (sourceArgString.empty())
173 {
175 LERROR << "BratDumper::outputRelationString Ignoring relation with empty source:"
176 << relationId << relationType << sourceArgString
177 << targetArgString;
178 continue;
179 }
180 if (targetArgString.empty())
181 {
183 LERROR << "BratDumper::outputRelationString Ignoring relation with empty target:"
184 << relationId << relationType << sourceArgString
185 << targetArgString;
186 continue;
187 }
188 // filter group name if present
189 string relstr=rel.toUtf8().constData();
190 if (relstr.find(".")!=string::npos) {
191 relstr=relstr.substr(relstr.find("."));
192 }
193 out << "R" << (relationId+i) << "\t" << relstr << " "
194 << "Arg1:T" << sourceArgString
195 << " Arg2:T" << targetArgString << endl;
196 i++;
197 }
198 return i;
199}
200
202outputEventString(ostream& out,
203 unsigned int eventId,
204 unsigned int eventMentionId,
205 const std::string& eventMentionType,
206 const std::vector<unsigned int>& eventRoleId,
207 const std::vector<std::string>& eventRoleType) const
208{
209 out << "E" << eventId << "\t";
210 // mention type is now one of the roles
211 if (eventMentionId!=0) {
212 out << eventMentionType << ":T" << eventMentionId;
213 for (unsigned int i(0), n=eventRoleId.size(); i<n; i++) {
214 out << " " << eventRoleType[i] << ":T" << eventRoleId[i];
215 }
216 }
217 else {
218 // first event role is the event mention: no space before
219 if (eventRoleId.size()==0) {out << endl; return; }
220 out << eventRoleType[0] << ":T" << eventRoleId[0];
221 for (unsigned int i(1), n=eventRoleId.size(); i<n; i++) {
222 out << " " << eventRoleType[i] << ":T" << eventRoleId[i];
223 }
224 }
225 out << endl;
226}
227
228} // AnalysisDumpers
229} // LinguisticProcessing
230} // Lima
This file is the main header file for the data related to annotation graphs.
#define BRATDUMPER_CLASSID
Definition BratDumper.h:15
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define DUMPERLOGINIT
Defines a Factory to create Object of type Base.
return a message when a 'param' was not found
Manage initialization of InitializableObjects using configuration module and parameters.
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
void outputEntityNorm(std::ostream &out, unsigned int entityId, const std::string &entityNorm) const override
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
unsigned int outputRelationString(std::ostream &out, unsigned int relationId, const std::string &relationType, const std::string &sourceArgString, const std::string &targetArgString) const override
void outputEventString(std::ostream &out, unsigned int eventId, unsigned int eventMentionId, const std::string &eventMentionType, const std::vector< unsigned int > &eventRoleId, const std::vector< std::string > &eventRoleType) const override
void outputEntityString(std::ostream &out, unsigned int entityId, const std::string &entityType, const std::string &entityString, const std::vector< std::pair< uint64_t, uint64_t > > &positions, const Automaton::EntityFeatures &entityFeatures, bool noNorm=false) const override
void outputAttributesString(std::ostream &out, unsigned int entityId, std::map< std::tuple< std::size_t, std::string, std::string >, std::size_t > &mapAttributes) const override
a list of generic features: each feature is unique (only one feature for a name)
SimpleFactory< MediaProcessUnit, BratDumper > bratDumperFactory(BRATDUMPER_CLASSID)
NAUTITIA.
STL namespace.