147 LINFO <<
"start RegexMatcher process";
150 LERROR <<
"no AnalysisGraph available for RegexMatcher ! abort";
154 LERROR <<
"AnalysisGraph is not a string ! abort";
157 auto annotationData = std::dynamic_pointer_cast< AnnotationData >(analysis.
getData(
"AnnotationData"));
158 if (annotationData==0)
160 annotationData = std::make_shared<AnnotationData>();
162 if (std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph")) != 0)
164 std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph"))->populateAnnotationGraph(
165 annotationData.get(),
"PosGraph");
167 analysis.
setData(
"AnnotationData",annotationData);
170 std::map< size_t, RegexMatch > matches;
176 auto originalText = std::dynamic_pointer_cast<LimaStringText>(analysis.
getData(
"Text"));
180 QRegularExpression re(reit.key());
181 QString& tstatus = reit.value();
183 qsizetype position = 0;
184 QRegularExpressionMatch qmatch;
185 while ((position = originalText->indexOf(re, position, &qmatch)) != -1) {
186 QString matchedString = originalText->mid(position, qmatch.capturedLength());
188 LDEBUG <<
"Matched '" << matchedString <<
"' at " << position <<
" as " << tstatus;
190 RegexMatch match = boost::make_tuple(position+1, matchedString.size(), matchedString, tstatus);
191 matches.insert(std::make_pair(position+1, match));
193 position += qmatch.capturedLength();
197 LDEBUG <<
"Matching finished. Updating graph";
201 if (!matches.empty())
203 std::map< size_t, RegexMatch >::const_iterator matchesIt = matches.begin();
204 size_t currentMatchPosition = (*matchesIt).second.get<0>();
205 size_t currentMatchLength = (*matchesIt).second.get<1>();
206 QString currentMatchString = (*matchesIt).second.get<2>();
207 QString currentMatchStatus = (*matchesIt).second.get<3>();
211 for (boost::tie(outItr,outItrEnd)=boost::out_edges(v,*graph); outItr!=outItrEnd; outItr++)
213 v=target(*outItr,*graph);
218 while (matchesIt != matches.end() && v != anagraph->
lastVertex())
221 uint64_t tokenPosition = token->
position();
223 LDEBUG <<
"Working on current match '" << currentMatchString <<
"' (" << currentMatchPosition <<
", " << currentMatchLength <<
") and token" << v <<
"at position" << tokenPosition;
225 while (v != anagraph->
lastVertex() && tokenPosition < currentMatchPosition)
227 for (boost::tie(outItr,outItrEnd)=boost::out_edges(v,*graph); outItr != outItrEnd; outItr++)
229 v=target(*outItr, *graph);
240 LDEBUG <<
"current token ("<<v<<
", "<<token->
stringForm()<<
") position is: " << tokenPosition
241 <<
" ; current match position is: " << currentMatchPosition;
244 if (tokenPosition == currentMatchPosition)
247 LDEBUG <<
" current match found on token on token" << v <<
"at position" << tokenPosition <<
". Now filling mach vertices" ;
249 std::vector< LinguisticGraphVertex > matchVertices;
250 matchVertices.push_back(v);
253 Token* nextToken = 0;
254 uint64_t nextTokenPosition = std::numeric_limits<uint64_t>::max();
255 for (boost::tie(outItr,outItrEnd)=boost::out_edges(next,*graph); outItr!=outItrEnd; outItr++)
257 next=target(*outItr,*graph);
259 nextTokenPosition = nextToken!=0?nextToken->
position():0;
263 while (next != anagraph->
lastVertex() && nextTokenPosition < (currentMatchPosition + currentMatchLength))
266 LDEBUG <<
" next token ("<<next<<
") position is: " << nextTokenPosition;
268 matchVertices.push_back(next);
269 for (boost::tie(outItr,outItrEnd)=boost::out_edges(next,*graph); outItr!=outItrEnd; outItr++)
271 next=target(*outItr,*graph);
273 nextTokenPosition = nextToken!=0?nextToken->
position():0;
277 auto matchVerticesIt = matchVertices.begin(), matchVerticesIt_end = matchVertices.end();
280 LDEBUG <<
"Match Found. Vertices are: ";
281 for (; matchVerticesIt != matchVerticesIt_end; matchVerticesIt++)
283 LDEBUG << (*matchVerticesIt) <<
", ";
287 if (matchVertices.empty())
continue;
291 boost::tie (inIt, inIt_end) = boost::in_edges(*matchVertices.begin(), *graph);
294 for (; inIt != inIt_end; inIt++)
296 boost::remove_edge(*inIt, *graph);
300 boost::tie (outIt, outIt_end) = boost::out_edges(*matchVertices.rbegin(), *graph);
302 if (outIt != outIt_end) after = boost::target(*outIt, *graph);
304 for (; outIt != outIt_end; outIt++)
306 boost::remove_edge(*outIt, *graph);
310 Token* newToken=
new Token( form, currentMatchString, currentMatchPosition, currentMatchLength );
316 tokenMap[newVertex]=newToken;
317 dataMap[newVertex]=newData;
318 newToken-> setPosition(currentMatchPosition);
320 boost::add_edge(before, newVertex, *graph);
322 boost::add_edge(newVertex, after, *graph);
324 annotationData->addMatching(
"AnalysisGraph", newVertex,
"annot", agv);
331 if (matchesIt != matches.end())
333 currentMatchPosition = (*matchesIt).second.get<0>();
334 currentMatchLength = (*matchesIt).second.get<1>();
335 currentMatchString = (*matchesIt).second.get<2>();
336 currentMatchStatus = (*matchesIt).second.get<3>();
338 LDEBUG <<
" current match is now: " << currentMatchPosition << currentMatchLength << currentMatchString << currentMatchStatus;
344 LDEBUG <<
" no more match";
352 LDEBUG <<
"Skiping matches up to after next token position: " << tokenPosition << currentMatchPosition << currentMatchLength;
355 while ((matchesIt != matches.end()) && ((currentMatchPosition ) <= tokenPosition))
358 if (matchesIt == matches.end())
break;
359 currentMatchPosition = (*matchesIt).second.get<0>();
360 currentMatchLength = (*matchesIt).second.get<1>();
361 currentMatchString = (*matchesIt).second.get<2>();
362 currentMatchStatus = (*matchesIt).second.get<3>();
365 if (matchesIt != matches.end())
366 LDEBUG <<
" current match is now: " << currentMatchPosition << currentMatchLength << currentMatchString << currentMatchStatus;
368 LDEBUG <<
" no more match";