Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

## Unreleased

* `LimaAnalyzer` (C++): the handlers of an analysis are the ones its
pipeline's dumpers need, created from the LIMA configuration (aymara/lima#3,
aymara/lima#247), instead of five hard-coded ones; `analyzeText` still
returns the output of `simpleStreamHandler`. Needs LIMA with #247.

## 0.10.0 - 20261127

Phrase matching with spaCy's `PhraseMatcher` API, on LIMA's ModEx rules.
Expand Down
141 changes: 95 additions & 46 deletions lima.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "linguisticProcessing/client/AnalysisHandlers/BowTextHandler.h"
#include "linguisticProcessing/client/AnalysisHandlers/SimpleStreamHandler.h"
#include "linguisticProcessing/client/AnalysisHandlers/LTRTextHandler.h"
#include "linguisticProcessing/client/AnalysisHandlers/PipelineHandlers.h"
#include "linguisticProcessing/core/Automaton/recognizer.h"
#include "linguisticProcessing/core/Automaton/SpecificEntityAnnotation.h"
#include "linguisticProcessing/tools/automatonCompiler/libautomatonCompiler/recognizerCompiler.h"
Expand Down Expand Up @@ -150,10 +151,6 @@ struct character_escaper
}
};

std::shared_ptr< std::ostringstream > openHandlerOutputString(
AbstractTextualAnalysisHandler* handler,
const std::set<std::string>&dumpers,
const std::string& dumperId);

int run(int aargc,char** aargv);

Expand Down Expand Up @@ -311,13 +308,23 @@ class LimaAnalyzerPrivate

const PropertyCodeManager* propertyCodeManager = nullptr;
// Fixed members that do not change at each analysis
std::map<std::string, AbstractAnalysisHandler*> handlers;
std::unique_ptr<BowTextWriter> bowTextWriter = nullptr;
std::unique_ptr<BowTextHandler> bowTextHandler = nullptr;
std::unique_ptr<SimpleStreamHandler> simpleStreamHandler = nullptr;
std::unique_ptr<SimpleStreamHandler> fullXmlSimpleStreamHandler = nullptr;
std::unique_ptr<LTRTextHandler> ltrTextHandler = nullptr;
std::set<std::string> dumpers = {"text"};
/** The handlers given to the analyses, created from the configuration
* (PipelineHandlers.h) when an analysis needs them, by media and name
* (nullptr: a handler that cannot be created) */
std::map<std::pair<MediaId, std::string>,
std::shared_ptr<AbstractTextualAnalysisHandler>> analysisHandlers;
/** The handlers named by the units added by addPipelineUnit (they are not
* in the configuration), by media and pipeline */
std::map<std::pair<MediaId, std::string>, std::set<std::string>> addedUnitsHandlers;
/** The output of the handlers whose output is not used: discarded (a stream
* without buffer, safe for all the handlers, unlike a null one) */
std::shared_ptr<std::ostream> discardedOutput = std::make_shared<std::ostream>(nullptr);
/** The handlers of an analysis by the pipeline of lang: the ones of its
* dumpers, simpleStreamHandler (whose output is analyzeText's result) and
* the ones of the units added by addPipelineUnit */
std::map<std::string, AbstractAnalysisHandler*> handlersFor(
const std::string& lang,
const std::string& pipeline);
std::shared_ptr< AbstractLinguisticProcessingClient > m_client;
std::map<std::string,std::string> metaData;
// Store constructor parameters to be able to implement copy constructor
Expand Down Expand Up @@ -509,17 +516,8 @@ LimaAnalyzerPrivate::LimaAnalyzerPrivate(const QStringList& iqlangs,
m_client = std::dynamic_pointer_cast<AbstractLinguisticProcessingClient>(
LinguisticProcessingClientFactory::single().createClient(clientId));

// Set the handlers
bowTextWriter = std::make_unique<BowTextWriter>();
handlers.insert(std::make_pair("bowTextWriter", bowTextWriter.get()));
bowTextHandler = std::make_unique<BowTextHandler>();
handlers.insert(std::make_pair("bowTextHandler", bowTextHandler.get()));
simpleStreamHandler = std::make_unique<SimpleStreamHandler>();
handlers.insert(std::make_pair("simpleStreamHandler", simpleStreamHandler.get()));
fullXmlSimpleStreamHandler = std::make_unique<SimpleStreamHandler>();
handlers.insert(std::make_pair("fullXmlSimpleStreamHandler", fullXmlSimpleStreamHandler.get()));
ltrTextHandler= std::make_unique<LTRTextHandler>();
handlers.insert(std::make_pair("ltrTextHandler", ltrTextHandler.get()));
// The handlers are created from the configuration by the analyses that
// need them (handlersFor)
std::lock_guard<std::mutex> lock(mediaUsersMutex());
for (const auto& lang: QSet<QString>(qlangs.begin(), qlangs.end()))
++mediaUsers()[lang.toStdString()];
Expand Down Expand Up @@ -812,7 +810,8 @@ Doc LimaAnalyzerPrivate::operator()(
{
// analyze it
// std::cerr << "Analyzing " << contentText.toStdString() << std::endl;
auto analysis = m_client->analyze(contentText, localMetaData, localPipeline, handlers);
auto analysis = m_client->analyze(contentText, localMetaData, localPipeline,
handlersFor(localLang, localPipeline));
return docFrom_analysis(analysis);
}
}
Expand Down Expand Up @@ -889,7 +888,9 @@ const std::string LimaAnalyzerPrivate::analyzeText(const std::string& text,
const std::string& pipeline,
const std::string& meta)
{
auto txtofs = openHandlerOutputString(simpleStreamHandler.get(), dumpers, "text");
// The output of simpleStreamHandler, the result
std::ostringstream output;
AbstractTextualAnalysisHandler* textHandler = nullptr;

auto localMetaData = parseMetaData(QString::fromStdString(meta), ',', ':', metaData);
localMetaData["FileName"]="param";
Expand Down Expand Up @@ -922,11 +923,71 @@ const std::string LimaAnalyzerPrivate::analyzeText(const std::string& text,
{
// analyze it
// std::cerr << "Analyzing " << contentText.toStdString() << std::endl;
m_client->analyze(contentText, localMetaData, localPipeline, handlers);
auto handlers = handlersFor(localLang, localPipeline);
auto textHandlerIt = handlers.find("simpleStreamHandler");
if (textHandlerIt != handlers.end())
{
textHandler = dynamic_cast<AbstractTextualAnalysisHandler*>(textHandlerIt->second);
}
if (textHandler != nullptr)
{
textHandler->setOut(&output);
}
try
{
m_client->analyze(contentText, localMetaData, localPipeline, handlers);
}
catch (...)
{
if (textHandler != nullptr)
{
textHandler->setOut(discardedOutput.get());
}
throw;
}
if (textHandler != nullptr)
{
textHandler->setOut(discardedOutput.get());
}
}
return output.str();
}

std::map<std::string, AbstractAnalysisHandler*> LimaAnalyzerPrivate::handlersFor(
const std::string& lang,
const std::string& pipeline)
{
auto media = Lima::Common::MediaticData::MediaticData::single().getMediaId(lang);
std::set<std::string> names = {"simpleStreamHandler"};
for (const auto& pipelineHandler: pipelineHandlers(media, pipeline))
{
names.insert(pipelineHandler.handler);
}
auto added = addedUnitsHandlers.find(std::make_pair(media, pipeline));
if (added != addedUnitsHandlers.end())
{
names.insert(added->second.begin(), added->second.end());
}
std::map<std::string, AbstractAnalysisHandler*> result;
for (const auto& name: names)
{
auto key = std::make_pair(media, name);
auto it = analysisHandlers.find(key);
if (it == analysisHandlers.end())
{
// Created once (nullptr if it cannot be, e.g. an XML handler)
auto handler = createAnalysisHandler(media, name);
if (handler != nullptr)
{
handler->setOut(discardedOutput.get());
}
it = analysisHandlers.emplace(key, handler).first;
}
if (it->second != nullptr)
{
result[name] = it->second.get();
}
}
auto result = txtofs->str();
// std::cerr << "LimaAnalyzerPrivate::analyzeText result: " << result << std::endl;
simpleStreamHandler->setOut(nullptr);
return result;
}

Expand Down Expand Up @@ -968,6 +1029,12 @@ bool LimaAnalyzerPrivate::addPipelineUnit(const std::string& pipeline,
unitConfig,
managers[mediaid]);
pipe->push_back(pu);
// Its handler, given to the analyses by this pipeline (handlersFor)
auto handler = jsonGroup["handler"].toString().toStdString();
if (!handler.empty())
{
addedUnitsHandlers[std::make_pair(mediaid, pipeline)].insert(handler);
}

return true;
}
Expand Down Expand Up @@ -1167,24 +1234,6 @@ bool LimaAnalyzerPrivate::setRecognizerRules(const std::string& media,
});
}

std::shared_ptr< std::ostringstream > openHandlerOutputString(
AbstractTextualAnalysisHandler* handler,
const std::set<std::string>&dumpers,
const std::string& dumperId)
{
auto ofs = std::make_shared< std::ostringstream >();
if (dumpers.find(dumperId)!=dumpers.end())
{
handler->setOut(ofs.get());
}
else
{
DUMPERLOGINIT;
LERROR << dumperId << "is not in the dumpers list";
}
return ofs;
}

QString LimaAnalyzerPrivate::getFeats(const LinguisticAnalysisStructure::MorphoSyntacticData& morphoData)
{
#ifdef DEBUG_LP
Expand Down
Loading