diff --git a/CHANGES.md b/CHANGES.md index bee0636..9dd9a9d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/lima.cpp b/lima.cpp index 8c70273..1ad627c 100644 --- a/lima.cpp +++ b/lima.cpp @@ -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" @@ -150,10 +151,6 @@ struct character_escaper } }; -std::shared_ptr< std::ostringstream > openHandlerOutputString( - AbstractTextualAnalysisHandler* handler, - const std::set&dumpers, - const std::string& dumperId); int run(int aargc,char** aargv); @@ -311,13 +308,23 @@ class LimaAnalyzerPrivate const PropertyCodeManager* propertyCodeManager = nullptr; // Fixed members that do not change at each analysis - std::map handlers; - std::unique_ptr bowTextWriter = nullptr; - std::unique_ptr bowTextHandler = nullptr; - std::unique_ptr simpleStreamHandler = nullptr; - std::unique_ptr fullXmlSimpleStreamHandler = nullptr; - std::unique_ptr ltrTextHandler = nullptr; - std::set 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::shared_ptr> analysisHandlers; + /** The handlers named by the units added by addPipelineUnit (they are not + * in the configuration), by media and pipeline */ + std::map, std::set> 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 discardedOutput = std::make_shared(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 handlersFor( + const std::string& lang, + const std::string& pipeline); std::shared_ptr< AbstractLinguisticProcessingClient > m_client; std::map metaData; // Store constructor parameters to be able to implement copy constructor @@ -509,17 +516,8 @@ LimaAnalyzerPrivate::LimaAnalyzerPrivate(const QStringList& iqlangs, m_client = std::dynamic_pointer_cast( LinguisticProcessingClientFactory::single().createClient(clientId)); - // Set the handlers - bowTextWriter = std::make_unique(); - handlers.insert(std::make_pair("bowTextWriter", bowTextWriter.get())); - bowTextHandler = std::make_unique(); - handlers.insert(std::make_pair("bowTextHandler", bowTextHandler.get())); - simpleStreamHandler = std::make_unique(); - handlers.insert(std::make_pair("simpleStreamHandler", simpleStreamHandler.get())); - fullXmlSimpleStreamHandler = std::make_unique(); - handlers.insert(std::make_pair("fullXmlSimpleStreamHandler", fullXmlSimpleStreamHandler.get())); - ltrTextHandler= std::make_unique(); - 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 lock(mediaUsersMutex()); for (const auto& lang: QSet(qlangs.begin(), qlangs.end())) ++mediaUsers()[lang.toStdString()]; @@ -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); } } @@ -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"; @@ -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(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 LimaAnalyzerPrivate::handlersFor( + const std::string& lang, + const std::string& pipeline) +{ + auto media = Lima::Common::MediaticData::MediaticData::single().getMediaId(lang); + std::set 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 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; } @@ -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; } @@ -1167,24 +1234,6 @@ bool LimaAnalyzerPrivate::setRecognizerRules(const std::string& media, }); } -std::shared_ptr< std::ostringstream > openHandlerOutputString( - AbstractTextualAnalysisHandler* handler, - const std::set&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