Blame view

morfeusz/wrappers/morfeusz_java.i 12.6 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
// 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();
Zbigniew Gawłowicz authored
24
        //void push_back(const value_type& x);
Michał Lenart authored
25
26
27
28
29
30
31
32
33
34
35
36
        %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);
            }
Zbigniew Gawłowicz authored
37
38
39
40
            bool add(const value_type& VECTOR_VALUE_IN) {
                $self->push_back(VECTOR_VALUE_IN);
                return true;
            }
Michał Lenart authored
41
42
43
44
45
46
47
48
            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
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

    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);
Zbigniew Gawłowicz authored
80
81
82
83
84
        }

        bool add(const value_type& VECTOR_VALUE_IN) {
            $self->push_back(VECTOR_VALUE_IN);
            return true;
Michał Lenart authored
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
        }

        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
123
124
}
Michał Lenart authored
125
126
127
%rename(_MorphInterpretation) morfeusz::MorphInterpretation;
%rename(_ResultsIterator) morfeusz::ResultsIterator;
Michał Lenart authored
128
%rename(_dictionarySearchPaths) morfeusz::Morfeusz::dictionarySearchPaths;
Michał Lenart authored
129
130
%rename(_getAvailableAgglOptions) morfeusz::Morfeusz::getAvailableAgglOptions;
%rename(_getAvailablePraetOptions) morfeusz::Morfeusz::getAvailablePraetOptions;
Michał Lenart authored
131
%rename(_setDictionary) morfeusz::Morfeusz::setDictionary;
Michał Lenart authored
132
Michał Lenart authored
133
%rename(_getLabels) morfeusz::IdResolver::getLabels;
Michał Lenart authored
134
135
136

%rename(_getLabels) morfeusz::MorphInterpretation::getLabels;
Michał Lenart authored
137
%ignore morfeusz::FileFormatException;
Michał Lenart authored
138
Michał Lenart authored
139
%javaexception("java.io.IOException") morfeusz::Morfeusz::setDictionary {
Michał Lenart authored
140
141
142
    try {
        $action
    }
Michał Lenart authored
143
    catch(morfeusz::FileFormatException & e) {
Michał Lenart authored
144
145
146
147
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, "Invalid file format");
        return $null;
    }
Michał Lenart authored
148
    catch(morfeusz::MorfeuszException & e) {
Michał Lenart authored
149
        jclass clazz = jenv->FindClass("pl/sgjp/morfeusz/MorfeuszException");
Michał Lenart authored
150
151
152
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
Michał Lenart authored
153
154
155
156
157
    catch(std::ios_base::failure & e) {
        jclass clazz = jenv->FindClass("java/io/IOException");
        jenv->ThrowNew(clazz, e.what());
        return $null;
    }
Michał Lenart authored
158
159
160
161
162
    catch(...) {
        jclass clazz = jenv->FindClass("java/lang/RuntimeException");
        jenv->ThrowNew(clazz, "Unknown exception");
        return $null;
    }
Michał Lenart authored
163
164
}
Michał Lenart authored
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
%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) {
Michał Lenart authored
181
        jclass clazz = jenv->FindClass("pl/sgjp/morfeusz/MorfeuszException");
Michał Lenart authored
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        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
200
201
}
Michał Lenart authored
202
203
//%typemap(javainterfaces) morfeusz::ResultsIterator "java.util.Iterator<_MorphInterpretation>"
%typemap(javabase) std::vector<morfeusz::MorphInterpretation> "java.util.AbstractList<_MorphInterpretation>"
Michał Lenart authored
204
205
206
207
%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
208
Michał Lenart authored
209
210
%typemap(javacode) morfeusz::Morfeusz %{
Michał Lenart authored
211
    /**
Michał Lenart authored
212
213
214
215
216
217
218
219
220
221
222
223
224
     * Analyze given text and return the results as iterator.
     * It does not store results for whole text at once, so may be less memory-consuming for analysis of big texts.
     * 
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
     * @param text text for morphological analysis.
     * @return iterator over morphological analysis results
     */
    public ResultsIterator analyseAsIterator(String text) {
        return new ResultsIterator(_analyseAsIterator(text));
    }

    /**
Michał Lenart authored
225
226
     * Analyze given text and return the results as list.
     * 
Michał Lenart authored
227
228
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
Michał Lenart authored
229
230
231
     * @param text text for morphological analysis.
     * @return list containing the results of morphological analysis
    */
Michał Lenart authored
232
    public List<MorphInterpretation> analyseAsList(String text) {
Michał Lenart authored
233
234
235
        InterpsList interpsList = new InterpsList();
        analyse(text, interpsList);
        return MorphInterpretation.createList(interpsList);
Michał Lenart authored
236
    }
Michał Lenart authored
237
238
239
240

    /**
     * Perform morphological synthesis on a given lemma.
     * 
Michał Lenart authored
241
242
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
Michał Lenart authored
243
244
     * @param lemma lemma to be synthesized
     * @return list containing results of the morphological synthesis
Michał Lenart authored
245
     * @throws MorfeuszException when given parameter contains whitespaces
Michał Lenart authored
246
247
     */
    public List<MorphInterpretation> generate(String lemma) {
Michał Lenart authored
248
249
250
        InterpsList interpsList = new InterpsList();
        generate(lemma, interpsList);
        return MorphInterpretation.createList(interpsList);
Michał Lenart authored
251
    }
Michał Lenart authored
252
253
254
255
256

    /**
     * Perform morphological synthesis on a given lemma.
     * Limit results to interpretations with the specified tag.
     * 
Michał Lenart authored
257
258
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
Michał Lenart authored
259
     * @param lemma lemma to be analysed
Michał Lenart authored
260
261
     * @param tagnum tag number of result interpretations
     * @return list containing results of the morphological synthesis
Michał Lenart authored
262
     * @throws MorfeuszException when given parameter contains whitespaces
Michał Lenart authored
263
264
     */
    public List<MorphInterpretation> generate(String lemma, int tagnum) {
Michał Lenart authored
265
266
267
        InterpsList interpsList = new InterpsList();
        generate(lemma, tagnum, interpsList);
        return MorphInterpretation.createList(interpsList);
Michał Lenart authored
268
269
270
    }

    /**
Michał Lenart authored
271
272
273
     * Get list of paths for dictionaries searching.
     * It is neccessary to modify this list
     * to search for dictionaries under non-default paths.
Michał Lenart authored
274
     * 
Michał Lenart authored
275
276
     * The returned list is NOT THREAD-SAFE (must have exclusive acces to modify it).
     * 
Michał Lenart authored
277
278
     * @return modifiable list of paths
     */
Michał Lenart authored
279
280
    public static List<String> getDictionarySearchPaths() {
        return get_dictionarySearchPaths();
Michał Lenart authored
281
    }
Michał Lenart authored
282
283
284
285

    /**
     * Get list of possible agglutination rules.
     * 
Michał Lenart authored
286
287
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
Michał Lenart authored
288
289
290
291
292
293
294
295
296
     * @return modifiable list of paths
     */
    public List<String> getAvailableAgglOptions() {
        return java.util.Collections.unmodifiableList(_getAvailableAgglOptions());
    }

    /**
     * Get list of possible past-tense segmentation rules.
     * 
Michał Lenart authored
297
298
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
Michał Lenart authored
299
300
301
302
303
     * @return modifiable list of paths
     */
    public List<String> getAvailablePraetOptions() {
        return java.util.Collections.unmodifiableList(_getAvailablePraetOptions());
    }
Michał Lenart authored
304
305
306
307
308
309
310

    /**
     * Set dictionary to be used with this instance.
     * 
     * NOT THREAD-SAFE (must have exclusive access to this instance).
     * 
     * @param dictName new dictionary name
Michał Lenart authored
311
312
     * @throws IOException when IO error occurs when trying to read dictionary
     * @throws MorfeuszException when there is no such dictionary
Michał Lenart authored
313
314
315
316
317
318
319
320
     */
    public void setDictionary(String dictName) throws IOException {

        synchronized (Morfeusz.class) {
            _setDictionary(dictName);
        }

    }
Michał Lenart authored
321
322
%}
Michał Lenart authored
323
324
325
326
327
328
329
%typemap(javacode) morfeusz::IdResolver %{

    public java.util.Collection<java.lang.String> getLabels(int labelsId) {
        return _getLabels(labelsId);
    }
%}
Michał Lenart authored
330
331
%typemap(javafinalize) SWIGTYPE %{
    protected void finalize() {
Michał Lenart authored
332
333
334
335
336
337
338
339
340
341
        delete();
    }  

    public synchronized void delete() {
        if (swigCPtr != 0) {
            if (swigCMemOwn) {
                swigCMemOwn = false;
                $moduleJNI.delete_$javaclassname(getCPtr(this));
            }  
            swigCPtr = 0;
Michał Lenart authored
342
343
344
345
346
347
        }
    }
%}

%typemap(javadestruct, methodname="delete", methodmodifiers="private") SWIGTYPE "";
Michał Lenart authored
348
%javamethodmodifiers morfeusz::Morfeusz::analyse(const std::string&, std::vector<MorphInterpretation>&) const "private";
Michał Lenart authored
349
350
351
%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
352
353
// should be overwritten by getDictionarySearchPaths() in typemap(javacode)
%javamethodmodifiers morfeusz::Morfeusz::dictionarySearchPaths "private";
Michał Lenart authored
354
355
%javamethodmodifiers morfeusz::Morfeusz::getAvailableAgglOptions "private";
%javamethodmodifiers morfeusz::Morfeusz::getAvailablePraetOptions "private";
Michał Lenart authored
356
%javamethodmodifiers morfeusz::Morfeusz::setDictionary "private";
Michał Lenart authored
357
358
359
360

// should be overwritten by getLabels() in typemap(javacode)
%javamethodmodifiers morfeusz::IdResolver::getLabels "private";
Michał Lenart authored
361
%typemap(javaclassmodifiers) std::vector "class"
Michał Lenart authored
362
363
%typemap(javaclassmodifiers) std::list "class"
%typemap(javaclassmodifiers) std::set "class"
Michał Lenart authored
364
365
%typemap(javaclassmodifiers) morfeusz::MorphInterpretation "class"
%typemap(javaclassmodifiers) morfeusz::ResultsIterator "class"
Michał Lenart authored
366
367
368
369
370
371

%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
372
373
374
375
%pragma(java) jniclassclassmodifiers="class"

%pragma(java) moduleclassmodifiers="class"
Michał Lenart authored
376
377
378
379
380
%pragma(java) jniclasscode=%{
  static {
    System.loadLibrary("jmorfeusz");
  }
%}
Michał Lenart authored
381
382
383
384

%pragma(java) jniclassimports=%{
import java.io.IOException;
%}
Michał Lenart authored
385
386
387
388
389
390
391

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