22#include <boost/algorithm/string.hpp>
37namespace LinguisticProcessing {
38namespace AnalysisDumpers {
44m_useNormalizedForms(false),
54 LDEBUG <<
"BratJSONDumper::BratJSONDumper()";
67 LDEBUG <<
"BratJSONDumper::init";
73 if (str==
"yes" || str==
"1" || str==
"true") {
74 m_useNormalizedForms=
true;
84 boost::replace_all(s,
"\n",
"\\n");
85 boost::replace_all(s,
"\r",
"\\r");
86 boost::replace_all(s,
"\t",
"\\t");
99 if (m_entities.size()) {
101 os <<
"\"entities\": [";
102 for (
const auto& e: m_entities) {
103 os << sep << endl << e.second;
108 if (m_eventTriggers.size()) {
111 os <<
"\"triggers\": [";
112 for (
const auto& e: m_eventTriggers) {
113 os << sep << endl << e;
118 if (m_attributes.size()) {
121 os <<
"\"attributes\": [";
122 for (
const auto& a: m_attributes) {
123 os << sep << endl << a;
128 if (m_norms.size()) {
131 os <<
"\"normalizations\": [";
132 for (
const auto& n: m_norms) {
133 os << sep << endl << n;
138 os << endl <<
"}" << endl;
140 m_attributes.clear();
143 m_eventTriggers.clear();
144 m_firstRelation=
true;
158 os <<
"\"relations\": [";
166 os <<
"\"events\": [";
176 unsigned int entityId,
177 const std::string& entityType,
178 const std::string& entityString,
179 const vector<pair<uint64_t,uint64_t> >& positions,
185 oss <<
"[ \"T" << entityId <<
"\", \"" << entityType <<
"\", ";
186 if (positions.size()==0) {
189 LERROR <<
"BratJSONDumper: no positions given for entity" << entityString;
193 vector<pair<uint64_t,uint64_t> >::const_iterator posIt=positions.begin();
194 oss <<
"[ [" << (*posIt).first <<
"," << (*posIt).second <<
"]";
195 for (posIt++; posIt!=positions.end(); posIt++) {
196 oss <<
", [" << (*posIt).first <<
"," << (*posIt).second <<
"]";
201 m_entities[entityId]=oss.str();
202 if (m_useNormalizedForms && !noNorm) {
204 for (Automaton::EntityFeatures::const_iterator
205 featureItr=entityFeatures.begin(),features_end=entityFeatures.end();
206 featureItr!=features_end; featureItr++)
208 if (featureItr->getName()==
"value") {
209 string valuestr=featureItr->getValueString();
211 if (valuestr.find_first_not_of(
' ') != std::string::npos) {
226 unsigned int entityId,
227 std::map <std::tuple <std::size_t, std::string , std::string >, std::size_t >& mapAttributes)
const
229 for(
auto iterAttribute = mapAttributes.begin();
230 iterAttribute!=mapAttributes.end();
233 auto feature = iterAttribute->first;
234 std::size_t eId = std::get<0>(feature);
235 if( eId == entityId )
237 std::size_t attributeId = iterAttribute->second;
238 std::string featName = std::get<1>(feature);
239 std::string featValue = std::get<2>(feature);
241 oss <<
"[ \"" << attributeId <<
"\", \"" << featName <<
"\", \"" << entityId <<
"\"";
243 oss <<
", \"" << featValue <<
"\"";
246 m_attributes.push_back(oss.str());
253 unsigned int entityId,
254 const std::string& entityNorm)
const
258 oss <<
"[ \"N" << entityId <<
"\", \"Reference\", \"T" << entityId <<
"\", \"LIMA\", \"1\", \""
259 << entityNorm <<
"\" ]";
260 m_norms.push_back(oss.str());
265 unsigned int relationId,
266 const std::string& relationType,
267 const std::string& sourceArgString,
268 const std::string& targetArgString)
const
270 QString relation = QString::fromUtf8(relationType.c_str());
271 QStringList relations = relation.split(
',');
273 for (
auto& rel : relations)
278 LERROR <<
"BratJSONDumper::outputRelationString Ignoring relation with empty type:"
279 << relationId << relationType << sourceArgString
283 if (sourceArgString.empty())
286 LERROR <<
"BratJSONDumper::outputRelationString Ignoring relation with empty source:"
287 << relationId << relationType << sourceArgString
291 if (targetArgString.empty())
294 LERROR <<
"BratJSONDumper::outputRelationString Ignoring relation with empty target:"
295 << relationId << relationType << sourceArgString
300 string relstr=rel.toUtf8().constData();
301 if (relstr.find(
".")!=string::npos) {
302 relstr=relstr.substr(relstr.find(
"."));
305 if (m_firstRelation) { m_firstRelation=
false; out << endl; }
else { out <<
"," << endl; }
307 out <<
"[ \"R" << (relationId+i) <<
"\", \"" << relstr <<
"\", ["
308 <<
"[\"Arg1\", \"T" << sourceArgString <<
"\"], "
309 <<
"[\"Arg2\", \"T" << targetArgString <<
"\"]] ]";
317 unsigned int eventId,
318 unsigned int eventMentionId,
320 const std::vector<unsigned int>& eventRoleId,
321 const std::vector<std::string>& eventRoleType)
const
323 if (m_firstEvent) { m_firstEvent=
false; out << endl; }
else { out <<
"," << endl; }
325 out <<
"[ \"E" << eventId <<
"\", ";
327 if (eventMentionId!=0) {
329 out <<
"\"T" << eventMentionId <<
"\", ";
330 m_eventTriggers.push_back(m_entities[eventMentionId]);
331 m_entities.erase(eventMentionId);
335 if (eventRoleId.size()==0) {out <<
"]]" << endl;
return; }
338 for (
unsigned int i(0), n=eventRoleId.size(); i<n; i++) {
339 if (i>0) { out <<
", "; }
340 out <<
"[\"" << eventRoleType[i] <<
"\", \"T" << eventRoleId[i] <<
"\"]";
This file is the main header file for the data related to annotation graphs.
#define BRATJSONDUMPER_CLASSID
Defines a Factory to create Object of type Base.
std::string & getParamsValueAtKey(const std::string &key)
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.
unsigned int outputRelationString(std::ostream &out, unsigned int relationId, const std::string &relationType, const std::string &sourceArgString, const std::string &targetArgString) const override
void outputGlobalHeader(std::ostream &os, const std::string &sourceFile="", const LimaStringText &originalText=LimaStringText("")) const override
specific output functions to deal with actual output format these functions are called inside the pro...
void outputEntityNorm(std::ostream &out, unsigned int entityId, const std::string &entityNorm) 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 outputRelationsHeader(std::ostream &os) const override
void outputEntitiesHeader(std::ostream &os) 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 init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
void outputRelationsFooter(std::ostream &os) 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
void outputEventsHeader(std::ostream &os) const override
void outputEntitiesFooter(std::ostream &os) const override
void outputGlobalFooter(std::ostream &os) const override
virtual ~BratJSONDumper()
void outputEventsFooter(std::ostream &os) const override
a list of generic features: each feature is unique (only one feature for a name)
define the limaStringText resource which is the text in limaString formal
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
std::string jsonEscape(const std::string &str)
SimpleFactory< MediaProcessUnit, BratJSONDumper > bratJSONDumperFactory(BRATJSONDUMPER_CLASSID)