morfeusz_python.i
4.19 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%exception {
try{
$action
}
catch(const std::ios_base::failure& e) {
SWIG_exception(SWIG_IOError, const_cast<char*>(e.what()));
}
catch(const morfeusz::FileFormatException& e) {
SWIG_exception(SWIG_IOError, const_cast<char*>(e.what()));
}
catch(const std::invalid_argument& e) {
SWIG_exception(SWIG_ValueError, const_cast<char*>(e.what()));
}
catch(const std::exception& e) {
SWIG_exception(SWIG_RuntimeError, const_cast<char*>(e.what()));
}
catch(const std::string& e) {
SWIG_exception(SWIG_RuntimeError, const_cast<char*>(e.c_str()));
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
%ignore morfeusz::MorfeuszException;
%ignore morfeusz::FileFormatException;
%rename (_generateByTagId) morfeusz::Morfeusz::generate(const std::string&, int, std::vector<std::string>&);
%extend morfeusz::ResultsIterator {
morfeusz::ResultsIterator& morfeusz::ResultsIterator::__iter__() {
return *($self);
}
};
%extend morfeusz::Morfeusz {
std::vector<morfeusz::MorphInterpretation> morfeusz::Morfeusz::_generateByTagId(const std::string& lemma, int tagId) const {
std::vector<morfeusz::MorphInterpretation> res;
$self->generate(lemma, tagId, res);
return res;
}
};
%feature("shadow") morfeusz::ResultsIterator::next %{
def next(self):
if self.hasNext():
return $action(self)
else:
raise StopIteration
%}
%feature("shadow") morfeusz::Morfeusz::analyseAsIterator %{
def analyse_iter(self, text):
"""
Analyse given text and return an iterator over MorphInterpretation as a result.
"""
return $action(self, text)
%}
%feature("shadow") morfeusz::Morfeusz::analyse %{
def analyse(self, text):
"""
Analyse given text and return a list of MorphInterpretation objects.
"""
res = InterpsList()
$action(self, text, res)
return res
%}
%feature("shadow") morfeusz::Morfeusz::_generateByTagId %{
def _generateByTagId(self, lemma, tagId):
res = InterpsList()
$action(self, lemma, tagId, res)
return res
%}
%feature("shadow") morfeusz::Morfeusz::generate %{
def generate(self, lemma, tagId=None):
"""
Perform morphological synthesis on given text and return a list of MorphInterpretation objects.
"""
if tagId is not None:
return self._generateByTagId(lemma, tagId)
else:
res = InterpsList()
$action(self, lemma, res)
return res
%}
%rename (_orth) morfeusz::MorphInterpretation::orth;
%extend morfeusz::MorphInterpretation {
%pythoncode %{
@property
def orth(self):
return self._orth.decode('utf8')
@orth.setter
def orth(self, val):
self._orth = val.encode('utf8')
%}
};
%rename (_lemma) morfeusz::MorphInterpretation::lemma;
%extend morfeusz::MorphInterpretation {
%pythoncode %{
@property
def lemma(self):
return self._lemma.decode('utf8')
@lemma.setter
def lemma(self, val):
self._lemma = val.encode('utf8')
%}
};
%feature("shadow") morfeusz::IdResolver::getTag %{
def getTag(self, tagId):
return $action(self, tagId).decode('utf8')
%}
%feature("shadow") morfeusz::IdResolver::getTagId %{
def getTagId(self, tag):
return $action(self, tag.encode('utf8'))
%}
%feature("shadow") morfeusz::IdResolver::getName %{
def getName(self, nameId):
return $action(self, nameId).decode('utf8')
%}
%feature("shadow") morfeusz::IdResolver::getNameId %{
def getNameId(self, name):
return $action(self, name.encode('utf8'))
%}
%feature("shadow") morfeusz::IdResolver::getLabelsAsString %{
def getLabelsAsUnicode(self, labelsId):
return $action(self, labelsId).decode('utf8')
%}
%feature("shadow") morfeusz::IdResolver::getLabels %{
def getLabels(self, labelsId):
return { l.decode('utf8') for l in $action(self, labelsId) }
%}
%feature("shadow") morfeusz::IdResolver::getLabelsId %{
def getLabelsId(self, labelsStr):
return $action(self, labelsStr.encode('utf8'))
%}
%include "std_vector.i"
%include "std_string.i"
%include "std_list.i"
%include "std_set.i"
%include "std_except.i"
%include "exception.i"
%include "typemaps.i"