LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
linearTextRepresentationLogger.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"
32
33#include <iostream>
34#include <fstream>
35
36using namespace Lima::Common::BagOfWords;
38using namespace Lima::Common::MediaticData;
40using namespace std;
41
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 // output suffix
77 try {
78 m_outputSuffix = unitConfiguration.getParamsValueAtKey("outputSuffix");
79 }
80 catch (NoSuchParam& ) {
81 m_outputSuffix = string(".ltr");
82 }
83 // get stop list
84 if (useStopList) {
85 try {
86 string stoplist = unitConfiguration.getParamsValueAtKey("stopList");
87 m_stopList = std::dynamic_pointer_cast<StopList>(LinguisticResources::single().getResource(m_language, stoplist));
88 }
89 catch (NoSuchParam& ) {
90 LERROR << "No param 'stopList' in LinearTextRepresentationLogger configuration group for language " << m_language;
92 }
93 }
94 else {
95 m_stopList = 0;
96 }
97}
98
100 AnalysisContent& analysis) const {
101
103 // get metadata
104 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.getData("LinguisticMetaData"));
105 if (metadata == 0) {
106 LERROR << "no LinguisticMetaData ! abort";
107 return MISSING_DATA;
108 }
109 // get the analysis graph
110 auto anaGraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData("PosGraph"));
111 if (anaGraph == 0) {
112 LERROR << "no AnalysisGraph ! abort";
113 return MISSING_DATA;
114 }
115 // get sentence boundaries
116 auto sb = std::dynamic_pointer_cast<SegmentationData>(analysis.getData("SentenceBoundaries"));
117 if (sb == 0) {
118 LDEBUG << "LinearTextRepresentationDumper::process: no SentenceBounds available: ignored";
119 // sentence bounds ignored: null pointer passed to LTRTextBuilder will be handled there
120 }
121 // build LTRText
122 boost::shared_ptr<LTR_Text> textRep( new LTR_Text() );
123 LTRTextBuilder builder(m_language, m_stopList);
124 builder.buildLTRTextFrom(
125 *(anaGraph->getGraph()),
126 sb.get(),
127 anaGraph->firstVertex(),
128 anaGraph->lastVertex(),
129 textRep,
130 metadata->getStartOffset());
131
132 // write LTR_Text
133 string textFileName = metadata->getMetaData("FileName");
134 string outputFile = textFileName + m_outputSuffix;
135 ofstream out(outputFile.c_str(), std::ofstream::binary);
136 if (!out.good()) {
137 throw runtime_error("can't open file " + outputFile);
138 }
139 textRep->binaryWriteOn(out);
140 out.flush();
141 out.close();
142 return SUCCESS_ID;
143}
144
145
146} // AnalysisDumpers
147} // LinguisticProcessing
148} // 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.
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 LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
static const LinguisticResources & single()
const singleton accessor
Definition Singleton.h:51
=========================================================================
#define LTRLOGGER_CLASSID
SimpleFactory< MediaProcessUnit, LinearTextRepresentationLogger > ltrLoggerFactory(LTRLOGGER_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