103 LDEBUG <<
"KnowledgeBasedSemanticRoleLabeler::init";
114 LERROR <<
"Missing 'dumper' parameter in KnowledgeBasedSemanticRoleLabeler group for language "
115 << (int)language <<
" !";
129 LERROR <<
"Missing 'loader' parameter in KnowledgeBasedSemanticRoleLabeler group for language "
130 << (int)language <<
" !";
148 LERROR <<
"Missing 'inputSuffix' parameter in KnowledgeBasedSemanticRoleLabeler group for language "
149 << (int)language <<
" !";
158 LERROR <<
"Missing 'outputSuffix' parameter in KnowledgeBasedSemanticRoleLabeler group for language "
159 << (int)language <<
" !";
163 QString kbsrlLogLevel =
"error";
166 kbsrlLogLevel = QString::fromUtf8(unitConfiguration.
getParamsValueAtKey(
"loglevel").c_str());
181 LERROR <<
"no param 'path' in KnowledgeBasedSemanticRoleLabeler group configuration";
185 QString mode =
"VerbNet";
189 if (mode !=
"VerbNet" && mode !=
"FrameNet")
192 LERROR <<
"Unknown semantic annotation mode" << mode;
207 QString str_program_name;
208 QString pathEnv = QString::fromUtf8(qgetenv(
"PATH").constData());
209 QStringList paths = pathEnv.split(QRegularExpression(
"[;:]"));
210 for (
auto it = paths.begin(); it != paths.end(); ++it)
212 const auto & path = *it;
213 if (QFile::exists(path +
"/python" ))
215 str_program_name = path +
"/python";
220 Py_SetProgramName(
const_cast<wchar_t*
>( str_program_name.toStdWString().c_str()));
222 Py_SetProgramName( (
wchar_t*)str_program_name.unicode() );
228 PyObject* main_module = PyImport_ImportModule(
"__main__");
229 PyObject* main_dict = PyModule_GetDict(main_module);
230 PyObject* sys_module = PyImport_ImportModule(
"sys");
231 if (sys_module ==
nullptr)
237 PyDict_SetItemString(main_dict,
"sys", sys_module);
240 PyObject* pythonpath = PySys_GetObject(
"path");
241 if (PyList_Append(pythonpath, PyUnicode_DecodeFSDefault(path.toUtf8().constData())) == -1)
244 LERROR <<
"Failed to append to python path";
250 PyObject* semanticrolelabeler_module = PyImport_ImportModule(
"semanticrolelabeler");
251 if (semanticrolelabeler_module == NULL)
254 LERROR <<
"KnowledgeBasedSemanticRoleLabeler::init"<< __FILE__ << __LINE__ <<
": Failed to import srl semanticrolelabeler module";
261 semanticrolelabeler_module,
"SemanticRoleLabeler",
"[sss]",
262 QString(QLatin1String(
"--loglevel=%1")).arg(kbsrlLogLevel).toUtf8().constData(),
263 QString(QLatin1String(
"--frame-lexicon=%1")).arg(mode).toUtf8().constData(),
310 TimeUtilsController knowledgeBasedSemanticRoleLabelerProcessTime(
"KnowledgeBasedSemanticRoleLabeler");
313 LINFO <<
"start SRL process";
316 auto metadata = std::dynamic_pointer_cast<LinguisticMetaData>(analysis.
getData(
"LinguisticMetaData"));
317 if (metadata ==
nullptr)
323 QScopedPointer<QTemporaryFile> temporaryFile;
326 QScopedPointer<QTemporaryFile> otherTemp(
new QTemporaryFile());
327 temporaryFile.swap(otherTemp);
328 if (!temporaryFile->open())
334 temporaryFile->fileName().toUtf8().constData());
339 returnCode=m_d->
m_dumper->process(analysis);
342 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: failed to dump data to temporary file";
350 QString fileName = QString::fromUtf8(metadata->getMetaData(
"FileName").c_str());
351 QString inputFilename;
356 QFile inputFile(inputFilename);
357 inputFile.open(QIODevice::ReadOnly);
358 conllInput = QString::fromUtf8(inputFile.readAll().constData());
363 if (!temporaryFile->open())
366 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: unable to open temporary file after dumping CoNLL data to it"<< temporaryFile->fileName();
367 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: keep (do not auto remove) it for debug purpose." ;
368 temporaryFile->setAutoRemove(
false);
371 conllInput = QString::fromUtf8(temporaryFile->readAll().constData());
373 temporaryFile->setAutoRemove(
false);
375 LDEBUG <<
"KnowledgeBasedSemanticRoleLabeler: keeping temporary file after dumping CoNLL data to it for debugging"<< temporaryFile->fileName();
377 temporaryFile->close();
381 PyObject* callResult = PyObject_CallMethod(m_d->
m_instance,
"annotate",
"ss",
382 conllInput.toUtf8().constData(),
383 metadata->getMetaData(
"Lang").c_str()
385 if (callResult ==
nullptr)
391 const char* result = PyUnicode_AsUTF8(callResult);
395 LERROR <<
"Cannot convert result item to string";
400 LDEBUG <<
"Python result is:" << result;
404 QString outputFilename;
407 QString fileName = QString::fromUtf8(metadata->getMetaData(
"FileName").c_str());
410 QFile outputFile(outputFilename);
411 outputFile.open(QIODevice::WriteOnly);
412 outputFile.write(result);
417 if ( !temporaryFile->open() )
422 if (!temporaryFile->seek(0))
425 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: unable to seek to the beginning of temporary file"<< temporaryFile->fileName();
426 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: keep (do not auto remove) it for debug purpose." ;
427 temporaryFile->setAutoRemove(
false);
430 if (temporaryFile->write(result) == -1)
433 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: unable to write SRL result to temporary file"<< temporaryFile->fileName();
434 LERROR <<
"KnowledgeBasedSemanticRoleLabeler: keep (do not auto remove) it for debug purpose." ;
435 temporaryFile->setAutoRemove(
false);
438 temporaryFile->close();
440 Py_DECREF(callResult);
442 returnCode=m_d->
m_loader->process(analysis);
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