명이 있습니다. 도와 주시면 감사하겠습니다. 응용 프로그램의 목적은 문장에있는 단어의 보조를 러시아어에서 영어로 번역하는 것입니다. 저는 C++ 프로그램에 의해 호출되는 python 스크립트에 의해 질의되는 sdict 형식의 어휘의 도움으로 이것을하고 있습니다. 4 :: :: 3 которой이/являются/:: 5 РАО을 표시c에 임베드 된 Python. PyRun_SimpleString을 동기식으로 호출하고 있습니까?
,210Выставка/전시 : 1 конгресс/의회 :: 2 организаторами/주최자 :
내 목적은 다음과 같은 출력을 얻을 수 있습니다/NONE :: 6 ЕЭС/NONE :: 7 России/없음 :: 8 EESR/NONE :: 오일 및 오일 :: 10 компания/회사 :: 11 ЮКОС/NONE :: 12 유코/NONE :: 13 и/: :: 14/관리 :: 15 Томской/없음 :: 16 области/지역 :: 17 продлится/last :: 18 дея/day :: 20
다음 코드는 그러나 두 번째 문장과 내가 잘못된 출력을 얻을에, 문장에 대한 성공 :
Егор/NONE : 1 Гайдар/NONE : 2 возглавлял/NONE을 :: 3 первое/헤드 4 : российское/제 5 :: правительство/없음 :: 6 которое/정부 :: :: называли 7/8 правительством/전화 : 9 камикадзе/정부 10 :
참고 :NONE
은 tran이없는 단어에 사용됩니다. 슬레이트.
PyRun_SimpleString
를 호출하는 다음과 같은 C++ 코드 발췌 실행 해요
:
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 사이에서 변수 값을 공유하는 것이 옳지 않을까요? 미리 감사드립니다.
'except : \ ncontinue'를 없애십시오. 실제 문제가 무엇이든간에 아마 마스킹합니다. 오류 처리 코드를 추가하여 파이썬이 어떤 오류가 발생했는지 알리십시오. 코드가 당신에게 말하게하지 않을 때 코드를 디버그하려고 할 필요가 없습니다. –