LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
ParagraphBoundariesFinder.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
6/***************************************************************************
7 * Copyright (C) 2004-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
11#include "SegmentationData.h"
20
21#include <QtCore/QRegularExpression>
22
23using namespace std;
25
26namespace Lima {
27namespace LinguisticProcessing {
28namespace LinguisticAnalysisStructure {
29
31
34m_graph("PosGraph"),
35m_paragraphSeparator(),
36m_dataName("ParagraphBoundaries")
37{
38 m_paragraphSeparator=Common::Misc::utf8stdstring2limastring("\n\n");
39}
40
41
44
47 Manager* /*manager*/)
48
49{
53 try
54 {
55 m_graph=unitConfiguration.getParamsValueAtKey("graph");
56 }
58 {
59 // optional: keep default
60 }
61
62 try
63 {
64 m_dataName=unitConfiguration.getParamsValueAtKey("data");
65 }
67 {
68 // optional: keep default
69 }
70
71
72 try
73 {
74 m_paragraphSeparator=Common::Misc::utf8stdstring2limastring(unitConfiguration.getParamsValueAtKey("paragraphSeparator"));
75 }
77 {
78 // optional: keep default
79 }
80}
81
83 AnalysisContent& analysis) const
84{
87 LINFO << "start finding paragraph founds";
88
89 // find paragraphs in text (positions of double carriage returns),
90 // then find corresponding vertices in graph
91
92 auto graph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.getData(m_graph));
93 if (graph==0) {
94 LERROR << "no graph '" << m_graph << "' available !";
95 return MISSING_DATA;
96 }
97 SegmentationData* boundaries=new SegmentationData(m_graph);
98 analysis.setData(m_dataName,boundaries);
99
100 auto text = std::dynamic_pointer_cast<LimaStringText>(analysis.getData("Text"));
101
102 std::vector<uint64_t> paragraphPositions;
103 int currentPos=0;
104 int i=text->indexOf(m_paragraphSeparator,currentPos);
105 while (i!=-1) {
106 paragraphPositions.push_back((uint64_t)i);
107 // goto next char that is not a carriage return
108 currentPos=text->indexOf(QRegularExpression(QString(QLatin1String("[^%1]")).arg(m_paragraphSeparator)),i+1);
109 i=text->indexOf(m_paragraphSeparator,currentPos);
110 }
111
112 if (paragraphPositions.empty()) {
113 LWARN << "no paragraph found";
114 return SUCCESS_ID;
115 }
116
117 // find vertices related to positions in graph
118 uint64_t parNum=0;
119 std::deque<LinguisticGraphVertex> toVisit;
120 std::set<LinguisticGraphVertex> visited;
121
122 LinguisticGraphVertex beginParagraph=graph->firstVertex();
123
124 toVisit.push_back(graph->firstVertex());
125 visited.insert(graph->firstVertex());
126
127 while (!toVisit.empty())
128 {
129 LinguisticGraphVertex currentVertex=toVisit.front();
130 toVisit.pop_front();
131
132 if (currentVertex == graph->lastVertex()) { // end of the graph
133 continue; // may be other nodes to test in queue
134 }
135
136 if (currentVertex != graph->firstVertex()) {
137 Token* t = get(vertex_token,*(graph->getGraph()),currentVertex);
138 uint64_t position=t->position();
139 if (position >= (paragraphPositions[parNum]+1)) {
140 // end vertex is previous vertex
141 LinguisticGraphInEdgeIt inEdge,inEdge_end;
142 boost::tie(inEdge,inEdge_end)=in_edges(currentVertex,*(graph->getGraph()));
143 LinguisticGraphVertex prevVertex=source(*inEdge,*(graph->getGraph()));
144 if (beginParagraph!=prevVertex) {
145 boundaries->add(Segment("paragraph", beginParagraph, prevVertex, graph.get()));
146 }
147 beginParagraph=prevVertex;
148 //boundaries->add(Segment("paragraph",beginParagraph,currentVertex,graph));
149 //beginParagraph=currentVertex;
150 parNum++;
151 if (parNum >= paragraphPositions.size()) {
152 break;
153 }
154 }
155 }
156
157 // store following nodes to test
158 LinguisticGraphOutEdgeIt outEdge,outEdge_end;
159 boost::tie(outEdge,outEdge_end)=out_edges(currentVertex,*(graph->getGraph()));
160
161 for (; outEdge!=outEdge_end; outEdge++) {
162 LinguisticGraphVertex next=target(*outEdge,*(graph->getGraph()));
163 if (visited.find(next)==visited.end()) {
164 toVisit.push_back(next);
165 visited.insert(next);
166 }
167 }
168 }
169
170 // add last segment as a paragraph
171 if (beginParagraph!=graph->lastVertex()) {
172 boundaries->add(Segment("paragraph", beginParagraph, graph->lastVertex(), graph.get()));
173 }
174
175 TimeUtils::logElapsedTime("ParagraphBoundariesFinder");
176 return SUCCESS_ID;
177}
178
179
180}
181
182}
183
184}
#define LWARN
Definition LimaCommon.h:160
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
LinguisticGraph::in_edge_iterator LinguisticGraphInEdgeIt
@ vertex_token
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
LinguisticGraph::out_edge_iterator LinguisticGraphOutEdgeIt
#define SENTBOUNDLOGINIT
#define PARAGRAPHBOUNDARIESFINDER_CLASSID
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
void setData(const QString &id, std::shared_ptr< AnalysisData > data)
set an analysisData with the given id.
return a message when a 'param' was not found
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
static void logElapsedTime(const std::string &mess, const std::string &taskCategory=std::string(""))
log the number of microseconds since last UpdateCurrentTime
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< MediaProcessUnit, ParagraphBoundariesFinder > paragraphBoundariesFinderFactory(PARAGRAPHBOUNDARIESFINDER_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