Blame view

morfeusz/wrappers/morfeusz_java.i 9.96 KB
Michał Lenart authored
1
2
3
4
5

%include "morfeusz_javadoc.i"

%include <stdint.i>
%include <std_except.i>
Michał Lenart authored
6
%include <std_common.i>
Michał Lenart authored
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
// make vector compatible with java.util.List interface

namespace std {

    template<class T> class vector {
      public:
        typedef size_t size_type;
        typedef T value_type;
        typedef const value_type& const_reference;
//        vector();
//        vector(size_type n);
//        vector(const vector& o);
        size_type capacity() const;
        void reserve(size_type n);
        %rename(isEmpty) empty;
        bool empty() const;
        void clear();
        void push_back(const value_type& x);
        %extend {
            const_reference get(int32_t i) const throw (std::out_of_range) {
                return $self->at(i);
            }
            value_type set(int32_t i, const value_type& VECTOR_VALUE_IN) throw (std::out_of_range) {
                const T old = $self->at(i);
                $self->at(i) = VECTOR_VALUE_IN;
                return old;
            }
            void add(int32_t i, const value_type& VECTOR_VALUE_IN) {
                $self->insert($self->begin() + i, VECTOR_VALUE_IN);
            }
            int32_t size() const {
              return $self->size();
            }
            void removeRange(int32_t from, int32_t to) {
              $self->erase($self->begin()+from, $self->begin()+to);
            }
        }
    };
Michał Lenart authored
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

    template<class T> class list {
        public:
        typedef size_t size_type;
        typedef T value_type;
        typedef const value_type& const_reference;

        %rename(isEmpty) empty;
        bool empty() const;
        void clear();

        %extend {

        const_reference get(int32_t i) const throw (std::out_of_range) {
            std::list<T>::const_iterator it = $self->begin();
            std::advance(it, i);
            return *it;
        }

        value_type set(int32_t i, const value_type& VECTOR_VALUE_IN) throw (std::out_of_range) {
            std::list<T>::iterator it = $self->begin();
            std::advance(it, i);
            std::string old = *it;
            *it = VECTOR_VALUE_IN;
            return old;
        }

        void add(int32_t i, const value_type& VECTOR_VALUE_IN) {
            std::list<T>::iterator it = $self->begin();
            std::advance(it, i);
            $self->insert(it, VECTOR_VALUE_IN);
        }

        value_type remove(int32_t i, const value_type& VECTOR_VALUE_IN) throw (std::out_of_range) {
            std::list<T>::iterator it = $self->begin();
            std::advance(it, i);
            std::string old = *it;
            $self->erase(it);
            return old;
        }

        int32_t size() const {
            return $self->size();
        }
        }
    };

    template<class T> class set {
        public:
        typedef size_t size_type;
        typedef T value_type;
        typedef const value_type& const_reference;

        %rename(isEmpty) empty;
        bool empty() const;

        %extend {

        const_reference get(int32_t i) const throw (std::out_of_range) {
            std::set<T>::const_iterator it = $self->begin();
            std::advance(it, i);
            return *it;
        }

        int32_t size() const {
            return $self->size();
        }
        }
    };
Michał Lenart authored
114
115
}
Michał Lenart authored
116
%typemap(javaimports) morfeusz::Morfeusz %{
Michał Lenart authored
117
import java.io.IOException;
Michał Lenart authored
118
import java.lang.RuntimeException;
Michał Lenart authored
119
import java.util.List;
Michał Lenart authored
120
import java.util.ArrayList;
Michał Lenart authored
121
122

/**
Michał Lenart authored
123
 * Performs morphological analysis (analyse methods) and syntesis (generate methods).
Michał Lenart authored
124
125
126
127
128
129
130
131
132
133
134
 * 
 * It is NOT thread-safe
 * but it is possible to use separate Morfeusz instance for each concurrent thread.
 */
%}

%typemap(javaimports) morfeusz::ResultsIterator %{
/**
 * Iterates through morphological analysis and synthesis results.
 * 
 */
Michał Lenart authored
135
136
%}
Michał Lenart authored
137
138
%rename(_dictionarySearchPaths) morfeusz::Morfeusz::dictionarySearchPaths;
%rename(_getLabels) morfeusz::IdResolver::getLabels;
Michał Lenart authored
139
%ignore morfeusz::FileFormatException;
Michał Lenart authored
140
Michał Lenart authored
141
%javaexception("java.io.IOException") morfeusz::Morfeusz::setAnalyzerDictionary {
Michał Lenart authored
142
143
144
    try {
        $action
    }
Michał Lenart authored
145
    catch(morfeusz::FileFormatException & e) {
Michał Lenart authored
146
147
148
149
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, "Invalid file format");
        return $null;
    }
Michał Lenart authored
150
151
152
153
154
    catch(std::ios_base::failure & e) {
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
Michał Lenart authored
155
156
}
Michał Lenart authored
157
%javaexception("java.io.IOException") morfeusz::Morfeusz::setGeneratorDictionary {
Michał Lenart authored
158
159
160
    try {
        $action
    }
Michał Lenart authored
161
    catch(morfeusz::FileFormatException & e) {
Michał Lenart authored
162
163
164
165
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, "Invalid file format");
        return $null;
    }
Michał Lenart authored
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
196
197
198
199
200
201
202
203
204
205
206
207
    catch(std::ios_base::failure & e) {
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
}

%javaexception("java.util.NoSuchElementException") morfeusz::ResultsIterator::next {
    try {
        $action
    }
    catch(std::out_of_range & e) {
        jclass clazz = jenv->FindClass("java/util/NoSuchElementException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
}

%exception {
    try{
        $action
    }
    catch(const morfeusz::MorfeuszException& e) {
        jclass clazz = jenv->FindClass("pl/waw/ipipan/morfeusz/MorfeuszException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
    catch(const std::exception& e) {
        jclass clazz = jenv->FindClass("java/lang/RuntimeException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
    catch(const std::string& e) {
        jclass clazz = jenv->FindClass("java/lang/RuntimeException");
        jenv->ThrowNew(clazz, e.c_str());
        return $null;
    }
    catch(...) {
        jclass clazz = jenv->FindClass("java/lang/RuntimeException");
        jenv->ThrowNew(clazz, "Unknown exception");
        return $null;
    }
Michał Lenart authored
208
209
}
Michał Lenart authored
210
211
212
213
214
215
%typemap(javainterfaces) morfeusz::ResultsIterator "java.util.Iterator<MorphInterpretation>"
%typemap(javabase) std::vector<morfeusz::MorphInterpretation> "java.util.AbstractList<MorphInterpretation>"
%typemap(javabase) std::vector<std::string> "java.util.AbstractList<java.lang.String>"
%typemap(javabase) std::list<std::string> "java.util.AbstractList<java.lang.String>"
%typemap(javabase) std::set<std::string> "java.util.AbstractList<java.lang.String>"
%typemap(javabase) morfeusz::MorfeuszException "java.lang.RuntimeException"
Michał Lenart authored
216
Michał Lenart authored
217
218
%typemap(javacode) morfeusz::Morfeusz %{
Michał Lenart authored
219
220
221
222
223
224
    /**
     * Analyze given text and return the results as list.
     * 
     * @param text text for morphological analysis.
     * @return list containing the results of morphological analysis
    */
Michał Lenart authored
225
    public List<MorphInterpretation> analyseAsList(String text) {
Michał Lenart authored
226
        InterpsList res = new InterpsList();
Michał Lenart authored
227
228
        analyse(text, res);
        return new ArrayList<MorphInterpretation>(res);
Michał Lenart authored
229
    }
Michał Lenart authored
230
231
232
233
234
235
236
237

    /**
     * Perform morphological synthesis on a given lemma.
     * 
     * @param lemma lemma to be synthesized
     * @return list containing results of the morphological synthesis
     */
    public List<MorphInterpretation> generate(String lemma) {
Michał Lenart authored
238
        InterpsList res = new InterpsList();
Michał Lenart authored
239
        generate(lemma, res);
Michał Lenart authored
240
        return new ArrayList<MorphInterpretation>(res);
Michał Lenart authored
241
    }
Michał Lenart authored
242
243
244
245
246

    /**
     * Perform morphological synthesis on a given lemma.
     * Limit results to interpretations with the specified tag.
     * 
Michał Lenart authored
247
     * @param lemma lemma to be analysed
Michał Lenart authored
248
249
250
251
     * @param tagnum tag number of result interpretations
     * @return list containing results of the morphological synthesis
     */
    public List<MorphInterpretation> generate(String lemma, int tagnum) {
Michał Lenart authored
252
        InterpsList res = new InterpsList();
Michał Lenart authored
253
        generate(lemma, tagnum, res);
Michał Lenart authored
254
255
256
257
258
259
260
261
262
263
        return new ArrayList<MorphInterpretation>(res);
    }

    /**
     * Get list of paths for dictionaries searching
     * 
     * @return modifiable list of paths
     */
    public List<String> getDictionarySearchPaths() {
        return this.get_dictionarySearchPaths();
Michał Lenart authored
264
265
266
267
268
269
270
271
272
273
    }
%}

%typemap(javacode) morfeusz::ResultsIterator %{

    /**
     * Removing of elements from this iterator is not supported.
     */
    public void remove() {
        throw new java.lang.UnsupportedOperationException();
Michał Lenart authored
274
275
276
    }
%}
Michał Lenart authored
277
278
279
280
281
282
283
%typemap(javacode) morfeusz::IdResolver %{

    public java.util.Collection<java.lang.String> getLabels(int labelsId) {
        return _getLabels(labelsId);
    }
%}
Michał Lenart authored
284
285
286
287
288
289
290
291
292
293
%typemap(javafinalize) SWIGTYPE %{
    protected void finalize() {
        if (swigCMemOwn) {
            $moduleJNI.delete_$javaclassname(getCPtr(this));
        }
    }
%}

%typemap(javadestruct, methodname="delete", methodmodifiers="private") SWIGTYPE "";
Michał Lenart authored
294
%javamethodmodifiers morfeusz::Morfeusz::analyse(const std::string&, std::vector<MorphInterpretation>&) const "private";
Michał Lenart authored
295
296
297
%javamethodmodifiers morfeusz::Morfeusz::generate(const std::string&, std::vector<MorphInterpretation>&) const "private";
%javamethodmodifiers morfeusz::Morfeusz::generate(const std::string&, int, std::vector<MorphInterpretation>&) const "private";
Michał Lenart authored
298
299
300
301
302
303
// should be overwritten by getDictionarySearchPaths() in typemap(javacode)
%javamethodmodifiers morfeusz::Morfeusz::dictionarySearchPaths "private";

// should be overwritten by getLabels() in typemap(javacode)
%javamethodmodifiers morfeusz::IdResolver::getLabels "private";
Michał Lenart authored
304
%typemap(javaclassmodifiers) std::vector "class"
Michał Lenart authored
305
306
%typemap(javaclassmodifiers) std::list "class"
%typemap(javaclassmodifiers) std::set "class"
Michał Lenart authored
307
308
309
310
311
312

%include "enums.swg"

/* Force the generated Java code to use the C enum values rather than making a JNI call */
%javaconst(1);
Michał Lenart authored
313
314
315
316
%pragma(java) jniclassclassmodifiers="class"

%pragma(java) moduleclassmodifiers="class"
Michał Lenart authored
317
318
319
320
321
%pragma(java) jniclasscode=%{
  static {
    System.loadLibrary("jmorfeusz");
  }
%}
Michał Lenart authored
322
323
324
325

%pragma(java) jniclassimports=%{
import java.io.IOException;
%}
Michał Lenart authored
326
327
328
329
330
331
332

%include "std_vector.i"
%include "std_string.i"
%include "std_except.i"
%include "exception.i"
%include "typemaps.i"