morfeusz_python.i
5.38 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
%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");
}
}
%exception morfeusz::Morfeusz::setDictionary {
try{
$action
}
catch(const std::ios_base::failure& e) {
SWIG_exception(SWIG_IOError, const_cast<char*>(e.what()));
}
catch(const morfeusz::MorfeuszException& 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::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);
}
};
%ignore morfeusz::Morfeusz::createInstance(morfeusz::MorfeuszUsage);
%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;
}
static morfeusz::Morfeusz* morfeusz::Morfeusz::createAnalyzerInstance() {
return morfeusz::Morfeusz::createInstance(morfeusz::ANALYSE_ONLY);
}
static morfeusz::Morfeusz* morfeusz::Morfeusz::createGeneratorInstance() {
return morfeusz::Morfeusz::createInstance(morfeusz::GENERATE_ONLY);
}
static morfeusz::Morfeusz* morfeusz::Morfeusz::createInstance() {
return morfeusz::Morfeusz::createInstance(morfeusz::BOTH_ANALYSE_AND_GENERATE);
}
};
%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"