该应用程序的目的是将句子中存在的单词的词条从俄语翻译成英语。我是在 sdict 格式化词汇的帮助下完成的,该词汇由 c++ 程序调用的 python 脚本查询。
我的目的是获得以下输出:
Выставка/exhibition::1 конгресс/congress::2 организаторами/组织者::3 которой/ which::4 являются/出现::5 РАО/NONE::6 ЕЭС/NONE::7 Росси/NONE::8 EESR/ NONE ::9 нефтяная/oil::10 компания/company::11 ЮКОС/NONE::12 YUKOS/NONE::13 и/and::14 администрация/administration::15 Томской/NONE::16 области/地区: :17 продлится/last::18 четыре/四::19 дня/day::20
该句子的以下代码成功,但是对于第二个句子,依此类推,我得到了错误的输出:
Егор/ NONE ::1 Гайдар/NONE::2 возглавлял/NONE::3 первое/head::4 российское/first::5 правительство/NONE::6 которое/government::7 назмывали/which::8 правивое call::9 камикадзе/government::10
注: NONE
用于缺少翻译的单词。
我正在运行以下实际调用的 C++ 代码摘录PyRun_SimpleString
:
for (unsigned int i = 0; i < theSentenceRows->size(); i++){
stringstream ss;
ss << (i + 1);
parsedFormattedOutput << theSentenceRows->at(i)[FORMINDEX] << "/";
getline(lemmaOutFileForTranslation, lemma);
PyObject *main_module, *main_dict;
PyObject *toTranslate_obj, *translation, *emptyString;
/* Setup the __main__ module for us to use */
main_module = PyImport_ImportModule("__main__");
main_dict = PyModule_GetDict(main_module);
/* Inject a variable into __main__, in this case toTranslate */
toTranslate_obj = PyString_FromString(lemma.c_str());
PyDict_SetItemString(main_dict, "start_word", toTranslate_obj);
/* Run the code snippet above in the current environment */
PyRun_SimpleString(pycode);
**usleep(2);**
translation = PyDict_GetItemString(main_dict, "translation");
Py_XDECREF(toTranslate_obj);
/* writing results */
parsedFormattedOutput << PyString_AsString(translation) << "::" << ss.str() << " ";
其中pycode定义为:
const char *pycode =
"import sys\n"
"import re\n"
"import sdictviewer.formats.dct.sdict as sdict\n"
"import sdictviewer.dictutil\n"
"dictionary = sdict.SDictionary( 'rus_eng_full2.dct' )\n"
"dictionary.load()\n"
"translation = \"*NONE*\"\n"
"p = re.compile('( )([a-z]+)(.*?)( )')\n"
"for item in dictionary.get_word_list_iter(start_word):\n"
" try:\n"
" if start_word == str(item):\n"
" instance, definition = item.read_articles()[0]\n"
" translation = p.findall(definition)[0][1]\n"
" except:\n"
" continue\n";
我注意到第二句话的输出有些延迟,所以我添加了 usleep(2); 到 C++,同时认为它发生是因为调用PyRun_SimpleString
不是同步的。但是,它没有帮助,我不确定这就是原因。延迟错误发生在后面和增加的句子中。
那么,调用是PyRun_SimpleString
同步的吗?也许,在 C++ 和 Python 之间共享变量值是不对的?先感谢您。