-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_simple_translator.cc
More file actions
46 lines (36 loc) · 1.63 KB
/
Copy pathpython_simple_translator.cc
File metadata and controls
46 lines (36 loc) · 1.63 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
#include <pybind11/embed.h>
#include <rime/candidate.h>
#include <rime/segmentation.h>
#include "python_simple_translator.h"
namespace pythonext {
PythonSimpleTranslator::PythonSimpleTranslator( const rime::Ticket& ticket, py::function py_entry )
: Translator { ticket }, py_entry { py_entry } {}
rime::an<rime::Translation> PythonSimpleTranslator::Query( const std::string& input, const rime::Segment& segment ) noexcept {
if ( !segment.HasTag( "abc" ) )
return nullptr;
const size_t segment_start { segment.start };
const size_t segment_end { segment.end };
try {
// skip if return value is None
// Note: cannot assign to type `py::list` directly
// https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html#assigning-py-none-to-wrappers
const py::object output { py_entry( 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& item : output_list ) {
const std::string item_string { item.cast<std::string>() };
translation->Append( rime::New<rime::SimpleCandidate>( "python_simple", segment_start, segment_end, item_string ) );
}
return translation;
} catch ( const py::error_already_set& e ) {
LOG( ERROR ) << e.what();
}
return nullptr;
}
} // namespace pythonext