Blame view

morfeusz/wrappers/morfeusz_python.i 8.15 KB
Michał Lenart authored
1
Michał Lenart authored
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
%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");
    }
}
Michał Lenart authored
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
%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");
    }
}
Michał Lenart authored
47
48
%ignore morfeusz::MorfeuszException;
%ignore morfeusz::FileFormatException;
Michał Lenart authored
49
Michał Lenart authored
50
%rename (_generateByTagId) morfeusz::Morfeusz::generate(const std::string&, int, std::vector<std::string>&);
Michał Lenart authored
51
Michał Lenart authored
52
53
54
55
56
%extend morfeusz::ResultsIterator {
    morfeusz::ResultsIterator& morfeusz::ResultsIterator::__iter__() {
        return *($self);
    }
};
Michał Lenart authored
57
Michał Lenart authored
58
//%ignore morfeusz::Morfeusz::createInstance(morfeusz::MorfeuszUsage);
Michał Lenart authored
59
60
61
62
63
64
65
%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;
    }
};
Michał Lenart authored
66
Michał Lenart authored
67
68
69
70
71
72
73
74
%feature("shadow") morfeusz::ResultsIterator::next %{
def next(self):
    if self.hasNext():
        return $action(self)
    else:
        raise StopIteration
%}
Michał Lenart authored
75
%feature("shadow") morfeusz::Morfeusz::_analyseAsIterator %{
Michał Lenart authored
76
Michał Lenart authored
77
def analyse_iter(self, text):
Michał Lenart authored
78
    """
Michał Lenart authored
79
    Analyse given text and return an iterator over MorphInterpretation objects as a result.
Michał Lenart authored
80
    """
Michał Lenart authored
81
82
    return $action(self, text)
%}
Michał Lenart authored
83
Michał Lenart authored
84
85
%feature("shadow") morfeusz::Morfeusz::analyse %{
def analyse(self, text):
Michał Lenart authored
86
87
88
    """
    Analyse given text and return a list of MorphInterpretation objects.
    """
Michał Lenart authored
89
90
91
92
    res = InterpsList()
    $action(self, text, res)
    return res
%}
Michał Lenart authored
93
Michał Lenart authored
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
%feature("shadow") morfeusz::Morfeusz::setAggl %{
def setAggl(self, optionString):
    """
    Select agglutination rules option
    """
    $action(self, optionString.encode('utf8'))
%}

%feature("shadow") morfeusz::Morfeusz::setPraet %{
def setPraet(self, optionString):
    """
    Select past tense segmentation
    """
    $action(self, optionString.encode('utf8'))
%}

%feature("shadow") morfeusz::Morfeusz::setCaseHandling %{
def setCaseHandling(self, option):
    """
    Set case handling option (valid options are CONDITIONALLY_CASE_SENSITIVE, STRICTLY_CASE_SENSITIVE, IGNORE_CASE)
    """
    $action(self, option)
%}

%feature("shadow") morfeusz::Morfeusz::setTokenNumbering %{
def setTokenNumbering(self, option):
    """
    Set token numbering option (valid options are SEPARATE_NUMBERING, CONTINUOUS_NUMBERING)
    """
    $action(self, option)
%}

%feature("shadow") morfeusz::Morfeusz::setWhitespaceHandling %{
def setWhitespaceHandling(self, option):
    """
    Set whitespace handling handling option (valid options are SKIP_WHITESPACES, KEEP_WHITESPACES, APPEND_WHITESPACES)
    """
    $action(self, option)
%}

%feature("shadow") morfeusz::Morfeusz::setDictionary %{
def setDictionary(self, dictName):
    """
    Set dictionary to be used by this instance (by name)
    """
    $action(self, dictName.encode('utf8'))
%}

%feature("shadow") morfeusz::Morfeusz::createInstance(morfeusz::MorfeuszUsage) %{
@staticmethod
def _createInstance(usage):
    return $action(usage)
%}

%feature("shadow") morfeusz::Morfeusz::createInstance(const std::string&, morfeusz::MorfeuszUsage) %{
@staticmethod
def createInstance(dictName=None, usage=BOTH_ANALYSE_AND_GENERATE):
    """
    Creates new instance of Morfeusz class. Usage may be BOTH_ANALYZE_AND_GENERATE (default), ONLY_ANALYSE and ONLY_GENERATE.
    """
    if dictName is None:
        return Morfeusz._createInstance(usage)
    else:
        return $action(dictName.encode('utf8'), usage)
%}
Michał Lenart authored
160
161
162
163
164
165
%feature("shadow") morfeusz::Morfeusz::_generateByTagId %{
def _generateByTagId(self, lemma, tagId):
    res = InterpsList()
    $action(self, lemma, tagId, res)
    return res
%}
Michał Lenart authored
166
Michał Lenart authored
167
168
%feature("shadow") morfeusz::Morfeusz::generate %{
def generate(self, lemma, tagId=None):
Michał Lenart authored
169
170
171
    """
    Perform morphological synthesis on given text and return a list of MorphInterpretation objects.
    """
Michał Lenart authored
172
173
174
175
176
177
178
    if tagId is not None:
        return self._generateByTagId(lemma, tagId)
    else:
        res = InterpsList()
        $action(self, lemma, res)
        return res
%}
Michał Lenart authored
179
Michał Lenart authored
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
%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')
    %}
};
Michał Lenart authored
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
%feature("shadow") morfeusz::MorphInterpretation::getTag %{
def getTag(self, morfeusz):
    """
    Returns tag as string.
    """
    return $action(self, morfeusz)
%}

%feature("shadow") morfeusz::MorphInterpretation::getName %{
def getName(self, morfeusz):
    """
    Returns this interpretation named entity as string
    """
    return $action(self, morfeusz)
%}

%feature("shadow") morfeusz::MorphInterpretation::getLabelsAsString %{
def getLabelsAsUnicode(self, morfeusz):
    """
    Returns this interpretation labels as string
    """
    return $action(self, morfeusz).decode('utf8')
%}

%feature("shadow") morfeusz::MorphInterpretation::getLabels %{
def getLabels(self, morfeusz):
    """
    Returns this interpretation labels as a set of strings
    """
    return { l.decode('utf8') for l in $action(self, morfeusz) }
%}

%feature("shadow") morfeusz::MorphInterpretation::createIgn %{
@staticmethod
def createIgn(startNode, endNode, orth, lemma):
    """
    Creates unknown interpretation
    """
    return $action(self, startNode, endNode, orth.encode('utf8'), lemma.encode('utf8'))
%}

%feature("shadow") morfeusz::MorphInterpretation::createWhitespace %{
@staticmethod
def createWhitespace(startNode, endNode, orth):
    """
    Creates whitespace interpretation
    """
    return $action(self, startNode, endNode, orth.encode('utf8'))
%}
Michał Lenart authored
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
%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) }
%}
Michał Lenart authored
287
Michał Lenart authored
288
289
290
%feature("shadow") morfeusz::IdResolver::getLabelsId %{
def getLabelsId(self, labelsStr):
    return $action(self, labelsStr.encode('utf8'))
Michał Lenart authored
291
292
293
294
295
296
297
298
299
%}

%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"