59#include "common/LimaVersion.h"
104#include <boost/algorithm/string.hpp>
105#include <boost/algorithm/string/find_format.hpp>
106#include <boost/algorithm/string/finder.hpp>
107#include <boost/format.hpp>
109#include <QtCore/QCoreApplication>
110#include <QtCore/QtGlobal>
111#include <QtCore/QString>
129 template<
typename FindResultT>
133 for (
typename FindResultT::const_iterator i = Match.begin();
136 s += boost::str(boost::format(
"\\x%02x") %
static_cast<int>(*i));
144 const std::set<std::string>&dumpers,
145 const std::string& dumperId);
147int run(
int aargc,
char** aargv);
155std::mutex& mediaUsersMutex()
157 static auto* mutex =
new std::mutex();
160std::map<std::string, int>& mediaUsers()
162 static auto* users =
new std::map<std::string, int>();
176 const QString&
meta);
184 const std::string& media,
185 const std::string& jsonGroupString);
187 const std::string
analyzeText(
const std::string& text,
188 const std::string& lang,
189 const std::string& pipeline,
190 const std::string&
meta);
194 std::shared_ptr<Lima::AnalysisContent> analysis);
238 std::map<std::string, AbstractAnalysisHandler*>
handlers;
245 std::shared_ptr< AbstractLinguisticProcessingClient >
m_client;
259QtMessageHandler previousQtMessageHandler =
nullptr;
266void filterQtMessages(QtMsgType type,
const QMessageLogContext& context,
267 const QString& message)
269 if (message.contains(QLatin1String(
"event dispatcher has already been destroyed")))
273 if (previousQtMessageHandler !=
nullptr)
275 previousQtMessageHandler(type, context, message);
279 std::cerr << message.toStdString() << std::endl;
283void installQtMessageFilter()
285 static bool installed =
false;
288 previousQtMessageHandler = qInstallMessageHandler(filterQtMessages);
295 const QStringList& iqpipelines,
296 const QString& imodulePath,
297 const QString& iuser_config_path,
298 const QString& iuser_resources_path,
299 const QString& imeta) :
304 char* argv[2] = {(
char*)(
"LimaAnalyzer"), NULL};
305 QCoreApplication app(argc, argv);
306 installQtMessageFilter();
308 if(iqlangs.size() == 0)
310 throw LimaException(
"Must initialize at least one language");
312 if(iqpipelines.size() == 0)
314 throw LimaException(
"Must initialize at least one pipeline");
317 QStringList additionalPaths({
modulePath+
"/config"});
319 auto limaConf = QString::fromUtf8(qgetenv(
"LIMA_CONF").constData());
320 if (!limaConf.isEmpty())
321 additionalPaths = limaConf.split(
LIMA_PATH_SEPARATOR, Qt::SkipEmptyParts) + additionalPaths;
329 QStringList additionalResourcePaths({
modulePath+
"/resources"});
331 auto limaRes = QString::fromUtf8(qgetenv(
"LIMA_RESOURCES").constData());
332 if (!limaRes.isEmpty())
333 additionalResourcePaths = limaRes.split(
LIMA_PATH_SEPARATOR, Qt::SkipEmptyParts) + additionalResourcePaths;
337 additionalResourcePaths);
352 std::string lpConfigFile =
"lima-analysis.xml";
353 std::string commonConfigFile =
"lima-common.xml";
354 std::string clientId =
"lima-coreclient";
356 std::string strConfigPath;
360 std::deque<std::string> pipelines;
362 pipelines.push_back(pipeline.toStdString());
367 std::deque<std::string> langs;
368 for (
const auto& lang:
qlangs)
369 langs.push_back(lang.toStdString());
380 resourcesPath.toUtf8().constData(),
381 configPath.toUtf8().constData(),
387 bool clientFactoryConfigured =
false;
388 Q_FOREACH(QString configDir, configDirs)
390 if (QFileInfo::exists(configDir +
"/" + lpConfigFile.c_str()))
398 (configDir +
"/" + lpConfigFile.c_str()));
404 clientFactoryConfigured =
true;
408 if(!clientFactoryConfigured)
410 std::cerr <<
"No LinguisticProcessingClientFactory were configured with"
412 <<
"and" << lpConfigFile << std::endl;
417 m_client = std::shared_ptr< AbstractLinguisticProcessingClient >(
418 std::dynamic_pointer_cast<AbstractLinguisticProcessingClient>(
432 std::lock_guard<std::mutex> lock(mediaUsersMutex());
433 for (
const auto& lang: QSet<QString>(
qlangs.begin(),
qlangs.end()))
434 ++mediaUsers()[lang.toStdString()];
443 std::lock_guard<std::mutex> lock(mediaUsersMutex());
444 for (
const auto& lang: QSet<QString>(
qlangs.begin(),
qlangs.end()))
446 auto& users = mediaUsers();
447 auto it = users.find(lang.toStdString());
448 if (it == users.end() || --it->second > 0)
456 catch (
const std::exception& e)
458 std::cerr <<
"LimaAnalyzer: cannot release media " << lang.toStdString()
459 <<
": " << e.what() << std::endl;
465 const std::string& pipelines,
466 const std::string& modulePath,
467 const std::string& user_config_path,
468 const std::string& user_resources_path,
469 const std::string& meta)
473 QStringList qlangs = QString::fromStdString(langs).split(
",", Qt::SkipEmptyParts);
474 QStringList qpipelines = QString::fromStdString(pipelines).split(
",", Qt::SkipEmptyParts);
476 QString::fromStdString(modulePath),
477 QString::fromStdString(user_config_path),
478 QString::fromStdString(user_resources_path),
479 QString::fromStdString(meta));
481 catch (
const std::exception& e)
483 std::cerr <<
"Lima internal error: " << e.what() << std::endl;
505 catch (
const std::exception& e)
507 std::cerr <<
"Lima internal error: " << e.what() << std::endl;
528 catch (
const std::exception& e)
530 std::cerr <<
"Lima internal error: " << e.what() << std::endl;
560 auto metas =
meta.split(
",", Qt::SkipEmptyParts);
561 for (
const auto& aMeta: metas)
563 auto kv = aMeta.split(
":");
566 std::cerr <<
"meta argument '"<< aMeta.toStdString() <<
"' is not of the form XXX:YYY: ignored" << std::endl;
570 metaData[kv[0].toStdString()] = kv[1].toStdString();
577 const std::string& media,
578 const std::string& jsonGroupString)
580 if (! m_d->addPipelineUnit(pipeline, media, jsonGroupString))
583 errorMessage = std::string(
"addPipelineUnit failed:") + pipeline +
", "
584 + media +
",\n" + jsonGroupString;
594 QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());
605 qDebug() <<
"Document is not an object" << Qt::endl;
610 qDebug() <<
"Invalid JSON...\n" << in << Qt::endl;
617 const std::string& media,
618 const std::string& jsonGroupString)
620 auto jsonGroup = QJsonDocument::fromJson(QByteArray::fromStdString(jsonGroupString)).object();
634 jsonGroup[
"class"].toString().toStdString())->create(
643 const std::string& lang,
644 const std::string& pipeline,
645 const std::string& meta)
648 if (m_d ==
nullptr ||
error)
656 return m_d->analyzeText(text, lang, pipeline, meta);
658 catch (
const std::exception& e)
660 std::cerr <<
"Lima internal error: " << e.what() << std::endl;
668 const std::string& lang,
669 const std::string& pipeline,
670 const std::string&
meta)
675 localMetaData[
"FileName"]=
"param";
676 auto qmeta = QString::fromStdString(
meta).split(
",", Qt::SkipEmptyParts);
677 for (
const auto& m: qmeta)
679 auto kv = m.split(
":");
681 localMetaData[kv[0].toStdString()] = kv[1].toStdString();
684 auto localLang = lang;
685 if (localLang.size() == 0)
687 localLang =
qlangs[0].toStdString();
689 auto localPipeline = pipeline;
690 if (localPipeline.size() == 0)
694 localMetaData[
"Lang"] = localLang;
696 QString contentText = QString::fromUtf8(text.c_str());
697 if (contentText.isEmpty())
699 std::cerr <<
"Empty input ! " << std::endl;
707 auto result = txtofs->str();
715 const std::set<std::string>&dumpers,
716 const std::string& dumperId)
718 auto ofs = std::make_shared< std::ostringstream >();
719 if (dumpers.find(dumperId)!=dumpers.end())
721 handler->
setOut(ofs.get());
726 LERROR << dumperId <<
"is not in the dumpers list";
739 QStringList featuresList;
740 for (
auto i = managers.cbegin(); i != managers.cend(); i++)
742 auto key = QString::fromUtf8(i->first.c_str());
743 if (key !=
"MACRO" && key !=
"MICRO")
747 auto value = QString::fromUtf8(i->second.getPropertySymbolicValue(lc).c_str());
750 featuresList << QString(
"%1=%2").arg(key).arg(value);
757 QTextStream featuresStream(&features);
758 if (featuresList.isEmpty())
764 for (
auto featuresListIt = featuresList.cbegin(); featuresListIt != featuresList.cend(); featuresListIt++)
766 if (featuresListIt != featuresList.cbegin())
768 featuresStream <<
"|";
770 featuresStream << *featuresListIt;
774 LDEBUG <<
"LimaAnalyzerPrivate::getFeats features:" << features;
784 LDEBUG <<
"LimaAnalyzerPrivate::collectVertexDependencyInformations";
787 auto posGraphData = std::dynamic_pointer_cast<LinguisticAnalysisStructure::AnalysisGraph>(analysis->getData(
"PosGraph"));
788 if (posGraphData ==
nullptr)
790 std::cerr <<
"Error: PosGraph has not been produced: check pipeline";
794 auto firstVertex = posGraphData->firstVertex();
795 auto lastVertex = posGraphData->lastVertex();
796 auto v = firstVertex;
797 auto [it, it_end] = boost::out_edges(v, *
posGraph);
807 syntacticData = std::dynamic_pointer_cast<SyntacticData>(analysis->getData(
"SyntacticData"));
810 syntacticData = std::make_shared<SyntacticData>(posGraphData.get(),
nullptr);
816 while (v != lastVertex)
829 if (!matches.empty())
831 for (
const auto& vx: matches)
833 if (
annotationData->hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
835 auto se =
annotationData->annotation(vx, QString::fromUtf8(
"SpecificEntity"))
839 for (
const auto& vse : se->vertices())
851 auto anaVertices =
annotationData->matches(
"PosGraph", v,
"AnalysisGraph");
852 auto anaVertex = *anaVertices.begin();
853 if (
annotationData->hasAnnotation(anaVertex, QString::fromUtf8(
"SpecificEntity")))
855 auto se =
annotationData->annotation(anaVertex, QString::fromUtf8(
"SpecificEntity"))
865 for (
const auto& vse : se->vertices())
867 auto posVertices =
annotationData->matches(
"AnalysisGraph", vse,
"PosGraph");
868 auto posVertex = *posVertices.begin();
894 auto [it, it_end] = boost::out_edges(v, *
posGraph);
908 std::shared_ptr<Lima::AnalysisContent> )
912 for (
auto [dit, dit_end] = boost::out_edges(dcurrent, *depGraph); dit != dit_end; dit++)
915 auto type = typeMap[*dit];
916 auto syntRelName =
languageData->getSyntacticRelationName(type);
918 boost::target(*dit, *depGraph));
919 if (syntRelName !=
"")
929 auto neType = QString::fromUtf8(
"_") ;
935 auto matches =
annotationData->matches(
"PosGraph", posGraphVertex,
"annot");
937 for (
const auto& vx: matches)
940 if (
annotationData->hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
942 auto se =
annotationData->annotation(vx, QString::fromUtf8(
"SpecificEntity")).
943 pointerValue<SpecificEntityAnnotation>();
953 auto anaVertices =
annotationData->matches(
"PosGraph", posGraphVertex,
"AnalysisGraph");
955 for (
const auto& anaVertex: anaVertices)
958 auto matches =
annotationData->matches(
"AnalysisGraph", anaVertex,
"annot");
959 for (
const auto& vx: matches)
962 if (
annotationData->hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
964 auto se =
annotationData->annotation(vx, QString::fromUtf8(
"SpecificEntity"))
970 if (neType !=
"_")
break;
984 QString conllRelName =
"_";
985 int targetConllId = 0;
990 LDEBUG <<
"LimaAnalyzerPrivate::getConllRelName target saved for" << v <<
"is" << target;
1001 LERROR <<
"LimaAnalyzerPrivate::getConllRelName target" << target <<
"not found in segmentation mapping";
1004 LDEBUG <<
"LimaAnalyzerPrivate::getConllRelName conll target saved for " << v <<
" is " << targetConllId;
1008 LDEBUG <<
"LimaAnalyzerPrivate::getConllRelName the lima dependency tag for " << v <<
" is " << relName;
1016 conllRelName = relName;
1021 if (conllRelName ==
"root")
1029 LDEBUG <<
"LimaAnalyzerPrivate::getConllRelName no target saved for" << v;
1033 LDEBUG <<
"LimaAnalyzerPrivate::getConllRelName result for" << v <<
"is" << conllRelName << targetConllId;
1035 return { conllRelName, targetConllId };
1043 for (
const auto& vx:
annotationData->matches(
"PosGraph", v,
"annot"))
1045 if (
annotationData->hasAnnotation(vx, QString::fromUtf8(
"SpecificEntity")))
1064 bool SpaceAfter =
true;
1066 for (
auto [outIter, outIterEnd] = boost::out_edges(v, *graph);
1067 outIter != outIterEnd; outIter++)
1069 auto next = boost::target(*outIter, *graph);
1071 if( nt !=
nullptr && (nt->position() == ft->position()+ft->length()) )
1082 return QString::fromUtf8(
static_cast<const LangData&
>(
1084 .getPropertyManager(
"MICRO")
1085 .getPropertySymbolicValue(morphoData.
firstValue(
This file is the main header file for the data related to annotation graphs.
A graph that stores the relations of syntactic dependency between the elements of a DependencyGraph.
#define LIMA_PATH_SEPARATOR
LinguisticGraph::vertex_descriptor LinguisticGraphVertex
LinguisticGraph::out_edge_iterator LinguisticGraphOutEdgeIt
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, LinguisticVertexProperties > LinguisticGraph
Property to identify the chains in the graph.
Data used for the syntactic analyzis of texts.
const FsaStringsPool * sp
LinguisticGraph * posGraph
std::unique_ptr< LTRTextHandler > ltrTextHandler
std::unique_ptr< BowTextWriter > bowTextWriter
QMap< QString, QString > conllLimaDepMapping
QString getNeType(LinguisticGraphVertex posGraphVertex)
Gets the named entity type for the PosGraph vertex posGraphVertex if it is a specific entity.
const std::string analyzeText(const std::string &text, const std::string &lang, const std::string &pipeline, const std::string &meta)
LimaAnalyzerPrivate & operator=(const LimaAnalyzerPrivate &a)=delete
const LanguageData * languageData
friend class LimaAnalyzer
std::map< LinguisticGraphVertex, int > vertexToToken
AnnotationData * annotationData
void collectDependencyInformations(std::shared_ptr< Lima::AnalysisContent > analysis)
std::unique_ptr< SimpleStreamHandler > fullXmlSimpleStreamHandler
std::map< std::string, AbstractAnalysisHandler * > handlers
std::shared_ptr< SyntacticData > syntacticData
QJsonObject objectFromString(const QString &in)
const SpecificEntityAnnotation * getSpecificEntityAnnotation(LinguisticGraphVertex v) const
LimaAnalyzerPrivate(const LimaAnalyzerPrivate &a)=delete
std::unique_ptr< BowTextHandler > bowTextHandler
std::pair< QString, int > getConllRelName(LinguisticGraphVertex v)
bool addPipelineUnit(const std::string &pipeline, const std::string &media, const std::string &jsonGroupString)
const Common::PropertyCode::PropertyAccessor * propertyAccessor
QString getFeats(const LinguisticAnalysisStructure::MorphoSyntacticData &morphoData)
const PropertyCodeManager * propertyCodeManager
void reset()
Reset all members used to store analysis states.
QString getMicro(LinguisticAnalysisStructure::MorphoSyntacticData &morphoData)
std::shared_ptr< AbstractLinguisticProcessingClient > m_client
void collectVertexDependencyInformations(LinguisticGraphVertex v, std::shared_ptr< Lima::AnalysisContent > analysis)
std::set< std::string > dumpers
std::map< std::string, std::string > metaData
LinguisticGraph * anaGraph
LimaAnalyzerPrivate(const QStringList &qlangs, const QStringList &qpipelines, const QString &modulePath, const QString &user_config_path, const QString &user_resources_path, const QString &meta)
QString user_resources_path
std::unique_ptr< SimpleStreamHandler > simpleStreamHandler
bool hasSpaceAfter(LinguisticGraphVertex v, LinguisticGraph *graph)
std::map< LinguisticGraphVertex, std::pair< LinguisticGraphVertex, std::string > > vertexDependencyInformations
LimaAnalyzer & operator=(const LimaAnalyzer &a)
LimaAnalyzer(const std::string &langs, const std::string &pipelines, const std::string &modulePath, const std::string &user_config_path="", const std::string &user_resources_path="", const std::string &meta="")
friend class LimaAnalyzerPrivate
std::string analyzeText(const std::string &text, const std::string &lang="", const std::string &pipeline="", const std::string &meta="")
bool addPipelineUnit(const std::string &pipeline, const std::string &media, const std::string &jsonGroupString)
defines callback interface
virtual void setOut(std::ostream *out)=0
set the output stream.
Holds an annotation graph and gives an API to manipulate it.
Provide function to read write and check a property.
Provide tools to parse a property file, and deal with the property coding system.
Parser class for the lima's xml configuration files.
Use this exception to signal an error in one of the configuration files.
The main LIMA exception class.
Holds morphosyntactic informations.
LinguisticCode firstValue(const Common::PropertyCode::PropertyAccessor &propertyAccessor) const
Return the first non empty value for the given accessor.
A representation of a specific entity to store in the annotation graph.
static std::shared_ptr< InitializableObjectFactory< MediaProcessUnit > > getFactory(const std::string &id)
static const AmosePluginsManager & single()
static AmosePluginsManager & changeable()
originally detectGroupConfigurationStructure.h in detectlibraries
int run(int aargc, char **aargv)
std::shared_ptr< std::ostringstream > openHandlerOutputString(AbstractTextualAnalysisHandler *handler, const std::set< std::string > &dumpers, const std::string &dumperId)
Lima::Common::MediaticData::LanguageData LangData
Lima::Common::MediaticData::MediaticData MedData
QStringList buildResourcesDirectoriesList(const QStringList &projects, const QStringList &paths)
Build a list of resources directories from a list of project names and a list of paths.
QStringList buildConfigurationDirectoriesList(const QStringList &projects, const QStringList &paths)
Build a list of configuration directories from a list of project names and a list of paths.
LIMA_COMMONQSLOG_EXPORT bool initQsLog(const QString &configString)
std::string operator()(const FindResultT &Match) const