LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
linearTextRepresentationDumper.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
22
23// #include "linguisticProcessing/core/LinguisticProcessors/HandlerStreamBuf.h"
25#include "common/LimaCommon.h"
33
34#include <iostream>
35#include <fstream>
36
38using namespace Lima::Common::BagOfWords;
39using namespace Lima::Common::MediaticData;
41using namespace std;
42
43namespace Lima {
44namespace LinguisticProcessing {
45namespace AnalysisDumpers {
46
48
55// -----------------------------------------------------------------------------
56// -- process unit management
57// -----------------------------------------------------------------------------
58
61 Manager* manager) {
62
64 m_language = manager->getInitializationParameters().media;
65 // a stop list is used ?
66 bool useStopList = false;
67 try {
68 string stopFlag = unitConfiguration.getParamsValueAtKey("useStopList");
69 useStopList = (stopFlag == "true");
70 }
71 catch (NoSuchParam& ) {
72 LWARN << "No param 'useStopList' in ltrDumper configuration group for language "
73 << m_language;
74 LWARN << "use default value: false";
75 }
76
77 if (useStopList) {
78 try {
79 string stoplist = unitConfiguration.getParamsValueAtKey("stopList");
80 m_stopList = std::dynamic_pointer_cast<StopList>(LinguisticResources::single().getResource(m_language, stoplist));
81 }
82 catch (NoSuchParam& ) {
83 LERROR << "LinearTextRepresentationDumper::init: No param 'stopList' in LinearTextRepresentationDumper configuration group for language " << m_language;
85 }
86 }
87 else {
88 m_stopList = 0;
89 }
90 try {
91 m_handler = unitConfiguration.getParamsValueAtKey("handler");
92 }
93 catch (NoSuchParam& ) {
95 LERROR << "LinearTextRepresentationDumper::init: Missing parameter handler in LinearTextRepresentationDumper configuration";
97 }
98}
99
101 AnalysisContent& analysis) const {
102
104 // get metadata
105 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
106 if (metadata == 0) {
107 LERROR << "LinearTextRepresentationDumper::process: no LinguisticMetaData ! abort";
108 return MISSING_DATA;
109 }
110 // get the analysis graph
111 auto anaGraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph"));
112 if (anaGraph == 0) {
113 LERROR << "LinearTextRepresentationDumper::process: no AnalysisGraph ! abort";
114 return MISSING_DATA;
115 }
116 // get sentence boundaries
117 auto sb = std::dynamic_pointer_cast<SegmentationData>(analysis.getData("SentenceBoundaries"));
118 if (sb == 0) {
119 LDEBUG << "LinearTextRepresentationDumper::process: no SentenceBounds available: ignored";
120 // sentence bounds ignored: null pointer passed to LTRTextBuilder will be handled there
121 }
122 // build LTRText
123 boost::shared_ptr<LTR_Text> textRep( new LTR_Text() );
124 LTRTextBuilder builder(m_language, m_stopList);
125 builder.buildLTRTextFrom(
126 *(anaGraph->getGraph()),
127 sb.get(),
128 anaGraph->firstVertex(),
129 anaGraph->lastVertex(),
130 textRep,
131 metadata->getStartOffset());
132 // write LTR_Text
133 LDEBUG << "handler will be: " << m_handler;
134// MediaId langid = static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(metadata->getMetaData("Lang"))).getMedia();
135 auto h = std::dynamic_pointer_cast<AnalysisHandlerContainer>(analysis.getData("AnalysisHandlerContainer"));
136 AbstractTextualAnalysisHandler* handler = static_cast<AbstractTextualAnalysisHandler*>(h->getHandler(m_handler));
137 if (handler == 0) {
138 LERROR << "LinearTextRepresentationDumper::process: handler " << m_handler << " has not been given to the core client";
139 return MISSING_DATA;
140 }
141 handler->startAnalysis();
142 HandlerStreamBuf hsb(handler);
143 ostream out(&hsb);
144 LDEBUG << "LinearTextRepresentationDumper::process result:" << *textRep;
145 textRep->binaryWriteOn(out);
146 out.flush();
147 handler->endAnalysis();
148 textRep->clear();
149 return SUCCESS_ID;
150}
151
152
153} // AnalysisDumpers
154} // LinguisticProcessing
155} // Lima
#define LWARN
Definition LimaCommon.h:160
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define DUMPERLOGINIT
Defines a Factory to create Object of type Base.
virtual void startAnalysis()=0
function called by the LIMA analyzer on start of a new document
virtual void endAnalysis()=0
function called by the LIMA analyzer on the end of a text part that is to be analyzed
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
a linear text representation
Definition ltrText.h:41
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
build a LTRText with all the tokens from the analysis
void buildLTRTextFrom(const LinguisticGraph &graph, Lima::LinguisticProcessing::SegmentationData *sb, const LinguisticGraphVertex &graphFirstVertex, const LinguisticGraphVertex &graphLastVertex, boost::shared_ptr< Lima::Common::BagOfWords::LTR_Text > textRep, uint64_t offset)
build a LTRText representation of the analyzed text
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
static const LinguisticResources & single()
const singleton accessor
Definition Singleton.h:51
=========================================================================
#define LTRDUMPER_CLASSID
SimpleFactory< MediaProcessUnit, LinearTextRepresentationDumper > ltrDumperFactory(LTRDUMPER_CLASSID)
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ SUCCESS_ID
Definition LimaCommon.h:237
@ MISSING_DATA
Definition LimaCommon.h:243
STL namespace.
launch exception related to the configuration file parsing