-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_translator.cc
More file actions
49 lines (38 loc) · 1.95 KB
/
Copy pathpython_translator.cc
File metadata and controls
49 lines (38 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <pybind11/embed.h>
#include <rime/candidate.h>
#include <rime/segmentation.h>
#include "python_translator.h"
namespace pythonext {
PythonTranslator::PythonTranslator( const rime::Ticket& ticket, py::function py_entry )
: Translator { ticket }, py_entry { py_entry }, TranslatorQuery { py::eval( "rimeext.TranslatorQuery", py::globals(), py::globals() ) } {}
rime::an<rime::Translation> PythonTranslator::Query( const std::string& input, const rime::Segment& segment ) noexcept {
if ( !segment.HasTag( "abc" ) )
return nullptr;
const size_t segment_start { segment.start };
try {
// skip if return value is None
const py::object output { py_entry( TranslatorQuery( input ) ) };
if ( output.is_none() )
return nullptr;
const py::list output_list { output };
// skip if returned list is empty
if ( output_list.empty() )
return nullptr;
// create translation list from the returned list
const rime::an<rime::FifoTranslation> translation { rime::New<rime::FifoTranslation>() };
for ( const py::handle& candidate : output_list ) {
// get all the 5 attributes from the object
const std::string text { candidate.attr( "text" ).cast<std::string>() };
const size_t length { candidate.attr( "length" ).cast<size_t>() };
const std::string candidate_type { candidate.attr( "candidate_type" ).cast<std::string>() };
const std::string comment { candidate.attr( "comment" ).cast<std::string>() };
const std::string preedit { candidate.attr( "preedit" ).cast<std::string>() };
translation->Append( rime::New<rime::SimpleCandidate>( candidate_type, segment_start, segment_start + length, text, comment, preedit ) );
}
return translation;
} catch ( const py::error_already_set& e ) {
LOG( ERROR ) << e.what();
}
return nullptr;
}
} // namespace pythonext