5#include <QtCore/QTemporaryFile>
6#include <QtCore/QRegularExpression>
38#include <eigen3/Eigen/Core>
41#define DEBUG_THIS_FILE true
54#if defined(DEBUG_LP) && defined(DEBUG_THIS_FILE)
55#define LOG_MESSAGE(stream, msg) stream << msg;
56#define LOG_MESSAGE_WITH_PROLOG(stream, msg) SALOGINIT; LOG_MESSAGE(stream, msg);
58 #define LOG_MESSAGE(stream, msg) ;
59 #define LOG_MESSAGE_WITH_PROLOG(stream, msg) ;
81 std::vector< std::map<std::string, std::string> >
m_tags;
95 THIS_FILE_LOGGING_CATEGORY()),
117 m_d->
init(unitConfiguration);
137 auto tiData = std::dynamic_pointer_cast<TokenIteratorData>(analysis.
getData(
"TokenIterator"));
138 if (tiData ==
nullptr)
141 LERROR <<
"Can't Process RnnDependencyParser : missing data 'TokenIterator'";
144 auto stridxPtr = tiData->getStringIndex();
146 auto tokenIterator = tiData->getTokenIterator();
147 tokenIterator->reset();
149 auto anagraph = std::dynamic_pointer_cast<AnalysisGraph>(analysis.
getData(
"PosGraph"));
150 if (anagraph ==
nullptr)
152 LERROR <<
"no PosGraph ! abort";
156 auto syntacticData = std::dynamic_pointer_cast<SyntacticAnalysis::SyntacticData>(analysis.
getData(
"SyntacticData"));
157 if (syntacticData ==
nullptr)
159 syntacticData = std::make_shared<SyntacticAnalysis::SyntacticData>(anagraph.get(),
nullptr);
160 analysis.
setData(
"SyntacticData",syntacticData);
162 syntacticData->setupDependencyGraph();
169 auto sd = std::dynamic_pointer_cast<SegmentationData>(
173 LERROR <<
"RnnDependencyParser: missing segmentation data '" << m_d->
m_data <<
"'";
184 std::vector<LinguisticGraphVertex> segmentBegin;
185 std::vector<std::vector<LinguisticGraphVertex>> segmentTokens;
186 for (
auto segIt = sd->getSegments().begin(); segIt != sd->getSegments().end();
191 segmentBegin.push_back(sentBegin);
192 segmentTokens.emplace_back();
193 std::vector<LinguisticGraphVertex>& tokens = segmentTokens.back();
195 std::queue<LinguisticGraphVertex> toVisit;
196 std::set<LinguisticGraphVertex> visited;
197 toVisit.push(sentBegin);
198 while (!toVisit.empty())
202 if (visited.count(v) > 0)
208 if (v != sentBegin && get(
vertex_token, *posGraph, v) !=
nullptr)
217 for (boost::tie(outIt, outItEnd) = boost::out_edges(v, *posGraph);
218 outIt != outItEnd; ++outIt)
221 if (visited.count(tgt) == 0 && tgt != lastVertex)
233 for (
size_t i = 0; i < m_d->
m_heads.size(); ++i)
235 if (i < m_d->m_isRoot.size() && m_d->
m_isRoot[i])
242 if (segIdx < 0 || segIdx >=
static_cast<int>(segmentTokens.size()))
246 const std::vector<LinguisticGraphVertex>& tokens = segmentTokens[segIdx];
247 if (localPos > tokens.size())
251 const uint32_t head = m_d->
m_heads[i];
252 const std::string& deprel = (i < m_d->
m_deprels.size())
254 : std::string(
"dep");
256 languageData.getSyntacticRelationId(deprel);
261 (head == 0 || head > tokens.size()) ? segmentBegin[segIdx]
263 syntacticData->addRelationNoChain(relType, src, dest);
277 QString lang_str = MediaticData::single().media(
m_language).c_str();
278 QString resources_path = MediaticData::single().getResourcesPath().c_str();
279 QString dependency_parser_name = dependency_parser_model_prefix;
280 QString tagger_model_name = tagger_model_prefix;
283 MediaticData::single().getOptionValue(
"udlang", udlang);
288 "RnnDependencyParserPrivate::init: Can't parse language id " << udlang.c_str(),
292 dependency_parser_name.replace(QString(
"$udlang"), QString(udlang.c_str()));
293 tagger_model_name.replace(QString(
"$udlang"), QString(udlang.c_str()));
296 QString::fromUtf8(
"/RnnDependencyParser/%1/%2.pt")
297 .arg(lang_str, dependency_parser_name));
300 QString::fromUtf8(
"/RnnTagger/%1/%2.pt")
301 .arg(lang_str, tagger_model_name));
302 if (dependency_parser_file_name.isEmpty())
308 LWARN <<
"RnnDependencyParserPrivate::init: no dependency parser model found for "
309 << lang_str <<
" (" << dependency_parser_name
310 <<
"); dependency parsing disabled.";
315 if (tagger_model_file_name.isEmpty())
320 LOG_MESSAGE(
LDEBUG,
"RnnDependencyParserPrivate::init call TokenSequenceAnalyzer<>().get_classes_from_fn");
324 temp_classes_names.erase(temp_classes_names.begin()+1);
325 temp_classes.erase(temp_classes.begin()+1);
326 m_load_fn = [
this, dependency_parser_file_name, tagger_model_file_name, temp_classes_names, temp_classes]()
340 Eigen::setNbThreads(1);
346 m_dependencyParser = std::make_shared<DependencyParser>(dependency_parser_file_name.toStdString(),
348 for (
size_t i = 0; i < temp_classes.size(); i++)
369 const std::vector<typename DependencyParser::token_with_analysis_t>& tokens,
382 (*m_dependencyParser)(*ti);
This file is the main header file for the data related to annotation graphs.
#define CONFIGURATIONHELPER_LOGGING_INIT(X)
#define LOG_MESSAGE_WITH_PROLOG(stream, msg)
#define LOG_MESSAGE(stream, msg)
#define LIMA_EXCEPTION_SELECT_LOGINIT(X, Y, Z)
This macro writes the message Y to the error stream configured by its first parameter X,...
A graph structure for linguistic analysis.
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.
#define RNNDEPENDENCYPARSER_CLASSID
Defines a Factory to create Object of type Base.
Data used for the syntactic analyzis of texts.
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.
Manage initialization of InitializableObjects using configuration module and parameters.
const InitializationParameters & getInitializationParameters() const
get Initialization Parameters
Use this exception to signal an error in one of the configuration files.
void getStringParameter(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, const std::string &name, std::string &value, int flags=Flags::REQUIRED, std::string default_value="")
~RnnDependencyParserPrivate()=default
std::shared_ptr< StringIndex > m_stridx
std::vector< std::map< std::string, std::string > > m_tags
std::vector< std::string > m_relClassNames
std::vector< std::string > m_class_names
std::vector< uint32_t > m_heads
std::vector< std::string > m_deprels
void init(GroupConfigurationStructure &unitConfiguration)
std::shared_ptr< DependencyParser > m_dependencyParser
void insertDependencies(DependencyParser::TokenIterator &ti)
RnnDependencyParserPrivate()
std::vector< QString > m_lemmas
void analyzer(std::shared_ptr< TokenSequenceAnalyzer<>::TokenIterator > ti)
std::function< void()> m_load_fn
std::vector< bool > m_isRoot
std::vector< std::vector< std::string > > m_classes
LimaStatusCode process(AnalysisContent &analysis) const override
Process on data in analysisContent.
void init(Lima::Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
virtual ~RnnDependencyParser() override
static const MediaticData & single()
const singleton accessor
This file contains a class to control log of informations about time, such as logging cumulated time ...
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
const char * form() const
const char * deprel() const
void get_classes_from_fn(const std::string &fn, std::vector< std::string > &classes_names, std::vector< std::vector< std::string > > &classes)
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
static SimpleFactory< MediaProcessUnit, RnnDependencyParser > RnnDependencyParserFactory(RNNDEPENDENCYPARSER_CLASSID)
bool fix_lang_codes(QString &lang_str, std::string &udlang)