1 module bindbc.mecab.binddynamic;
2 
3 version (BindMeCab_Static)
4 {
5 }
6 else version = BindMeCab_Dynamic;
7 
8 version (BindMeCab_Dynamic)  : public import bindbc.mecab.types;
9 
10 private import bindbc.loader;
11 
12 extern (C) @nogc nothrow:
13 
14 __gshared
15 {
16     /* C interface */
17     /* old mecab interface */
18     /**
19      * C wrapper of MeCab::Tagger::create(argc, argv)
20      */
21     mecab_t* function(int argc, char** argv) mecab_new;
22 
23     /**
24      * C wrapper of MeCab::Tagger::create(arg)
25      */
26     mecab_t* function(const(char)* arg) mecab_new2;
27 
28     /**
29      * C wrapper of MeCab::Tagger::version()
30      */
31     const(char)* function() mecab_version;
32 
33     /**
34      * C wrapper of MeCab::getLastError()
35      */
36     const(char)* function(mecab_t* mecab) mecab_strerror;
37 
38     /**
39      * C wrapper of MeCab::deleteTagger(tagger)
40      */
41     void function(mecab_t* mecab) mecab_destroy;
42 
43     /**
44      *C wrapper of MeCab::Tagger:set_partial()
45      */
46     int function(mecab_t* mecab) mecab_get_partial;
47 
48     /**
49      * C wrapper of MeCab::Tagger::partial()
50      */
51     void function(mecab_t* mecab, int partial) mecab_set_partial;
52 
53     /**
54      * C wrapper of MeCab::Tagger::theta()
55      */
56     float function(mecab_t* mecab) mecab_get_theta;
57 
58     /**
59      * C wrapper of  MeCab::Tagger::set_theta()
60      */
61     void function(mecab_t* mecab, float theta) mecab_set_theta;
62 
63     /**
64      * C wrapper of MeCab::Tagger::lattice_level()
65      */
66     int function(mecab_t* mecab) mecab_get_lattice_level;
67 
68     /**
69      * C wrapper of MeCab::Tagger::set_lattice_level()
70      */
71     void function(mecab_t* mecab, int level) mecab_set_lattice_level;
72 
73     /**
74      * C wrapper of MeCab::Tagger::all_morphs()
75      */
76     int function(mecab_t* mecab) mecab_get_all_morphs;
77 
78     /**
79      * C wrapper of MeCab::Tagger::set_all_moprhs()
80      */
81     void function(mecab_t* mecab, int all_morphs) mecab_set_all_morphs;
82 
83     /**
84      * C wrapper of MeCab::Tagger::parse(MeCab::Lattice *lattice)
85      */
86     int function(mecab_t* mecab, mecab_lattice_t* lattice) mecab_parse_lattice;
87 
88     /**
89      * C wrapper of MeCab::Tagger::parse(char *str)
90      */
91     const(char)* function(mecab_t* mecab, char* str) mecab_sparse_tostr;
92 
93     /**
94      * C wrapper of MeCab::Tagger::parse(char *str, size_t len)
95      */
96     const(char)* function(mecab_t* mecab, char* str, size_t len) mecab_sparse_tostr2;
97 
98     /**
99      * C wrapper of MeCab::Tagger::parse(char *str, char *ostr, size_t olen)
100      */
101     char* function(mecab_t* mecab, const(char)* str, size_t len, char* ostr, size_t olen) mecab_sparse_tostr3;
102 
103     /**
104      * C wrapper of MeCab::Tagger::parseToNode(char *str)
105      */
106     const(mecab_node_t)* function(mecab_t* mecab, const(char)*) mecab_sparse_tonode;
107 
108     /**
109      * C wrapper of MeCab::Tagger::parseToNode(char *str, size_t len)
110      */
111     const(mecab_node_t)* function(mecab_t* mecab, const(char)*, size_t) mecab_sparse_tonode2;
112 
113     /**
114      * C wrapper of MeCab::Tagger::parseNBest(size_t N, char *str)
115      */
116     const(char)* function(mecab_t* mecab, size_t N, const(char)* str) mecab_nbest_sparse_tostr;
117 
118     /**
119      * C wrapper of MeCab::Tagger::parseNBest(size_t N, char *str, size_t len)
120      */
121     const(char)* function(mecab_t* mecab, size_t N, const(char)* str, size_t len) mecab_nbest_sparse_tostr2;
122 
123     /**
124      * C wrapper of MeCab::Tagger::parseNBest(size_t N, char *str, char *ostr, size_t olen)
125      */
126     char* function(mecab_t* mecab, size_t N, const(char)* str, size_t len, char* ostr, size_t olen) mecab_nbest_sparse_tostr3;
127 
128     /**
129      * C wrapper of MeCab::Tagger::parseNBestInit(char *str)
130      */
131     int function(mecab_t* mecab, const(char)* str) mecab_nbest_init;
132 
133     /**
134      * C wrapper of MeCab::Tagger::parseNBestInit(char *str, size_t len)
135      */
136     int function(mecab_t* mecab, const(char)* str, size_t len) mecab_nbest_init2;
137 
138     /**
139      * C wrapper of MeCab::Tagger::next()
140      */
141     const(char)* function(mecab_t* mecab) mecab_nbest_next_tostr;
142 
143     /**
144      * C wrapper of MeCab::Tagger::next(char *ostr, size_t olen)
145      */
146     char* function(mecab_t* mecab, char* ostr, size_t olen) mecab_nbest_next_tostr2;
147 
148     /**
149      * C wrapper of MeCab::Tagger::nextNode()
150      */
151     const(mecab_node_t)* function(mecab_t* mecab) mecab_nbest_next_tonode;
152 
153     /**
154      * C wrapper of MeCab::Tagger::formatNode(const Node *node)
155      */
156     const(char)* function(mecab_t* mecab, const(mecab_node_t)* node) mecab_format_node;
157 
158     /**
159      * C wrapper of MeCab::Tagger::dictionary_info()
160      */
161     const(mecab_dictionary_info_t)* function(mecab_t* mecab) mecab_dictionary_info;
162 
163     /* lattice interface */
164     /**
165      * C wrapper of MeCab::createLattice()
166      */
167     mecab_lattice_t* function() mecab_lattice_new;
168 
169     /**
170      * C wrapper of MeCab::deleteLattice(lattice)
171      */
172     void function(mecab_lattice_t* lattice) mecab_lattice_destroy;
173 
174     /**
175      * C wrapper of MeCab::Lattice::clear()
176      */
177     void function(mecab_lattice_t* lattice) mecab_lattice_clear;
178 
179     /**
180      * C wrapper of MeCab::Lattice::is_available()
181      */
182     int function(mecab_lattice_t* lattice) mecab_lattice_is_available;
183 
184     /**
185      * C wrapper of MeCab::Lattice::bos_node()
186      */
187     mecab_node_t* function(mecab_lattice_t* lattice) mecab_lattice_get_bos_node;
188 
189     /**
190      * C wrapper of MeCab::Lattice::eos_node()
191      */
192     mecab_node_t* function(mecab_lattice_t* lattice) mecab_lattice_get_eos_node;
193 
194     /**
195      * C wrapper of MeCab::Lattice::begin_nodes()
196      */
197     mecab_node_t** function(mecab_lattice_t* lattice) mecab_lattice_get_all_begin_nodes;
198 
199     /**
200      * C wrapper of MeCab::Lattice::end_nodes()
201      */
202     mecab_node_t** function(mecab_lattice_t* lattice) mecab_lattice_get_all_end_nodes;
203 
204     /**
205      * C wrapper of MeCab::Lattice::begin_nodes(pos)
206      */
207     mecab_node_t* function(mecab_lattice_t* lattice, size_t pos) mecab_lattice_get_begin_nodes;
208 
209     /**
210      * C wrapper of MeCab::Lattice::end_nodes(pos)
211      */
212     mecab_node_t* function(mecab_lattice_t* lattice, size_t pos) mecab_lattice_get_end_nodes;
213 
214     /**
215      * C wrapper of MeCab::Lattice::sentence()
216      */
217     char* function(mecab_lattice_t* lattice) mecab_lattice_get_sentence;
218 
219     /**
220      * C wrapper of MeCab::Lattice::set_sentence(sentence)
221      */
222     void function(mecab_lattice_t* lattice, const(char)* sentence) mecab_lattice_set_sentence;
223 
224     /**
225      * C wrapper of MeCab::Lattice::set_sentence(sentence, len)
226      */
227     void function(mecab_lattice_t* lattice, const(char)* sentence, size_t len) mecab_lattice_set_sentence2;
228 
229     /**
230      * C wrapper of MeCab::Lattice::size()
231      */
232     size_t function(mecab_lattice_t* lattice) mecab_lattice_get_size;
233 
234     /**
235      * C wrapper of MeCab::Lattice::Z()
236      */
237     double function(mecab_lattice_t* lattice) mecab_lattice_get_z;
238 
239     /**
240      * C wrapper of MeCab::Lattice::set_Z()
241      */
242     void function(mecab_lattice_t* lattice, double Z) mecab_lattice_set_z;
243 
244     /**
245      * C wrapper of MeCab::Lattice::theta()
246      */
247     double function(mecab_lattice_t* lattice) mecab_lattice_get_theta;
248 
249     /**
250      * C wrapper of MeCab::Lattice::set_theta()
251      */
252     void function(mecab_lattice_t* lattice, double theta) mecab_lattice_set_theta;
253 
254     /**
255      * C wrapper of MeCab::Lattice::next()
256      */
257     int function(mecab_lattice_t* lattice) mecab_lattice_next;
258 
259     /**
260      * C wrapper of MeCab::Lattice::request_type()
261      */
262     int function(mecab_lattice_t* lattice) mecab_lattice_get_request_type;
263 
264     /**
265      * C wrapper of MeCab::Lattice::has_request_type()
266      */
267     int function(mecab_lattice_t* lattice, int request_type) mecab_lattice_has_request_type;
268 
269     /**
270      * C wrapper of MeCab::Lattice::set_request_type()
271      */
272     void function(mecab_lattice_t* lattice, int request_type) mecab_lattice_set_request_type;
273 
274     /**
275      * C wrapper of MeCab::Lattice::add_request_type()
276      */
277     void function(mecab_lattice_t* lattice, int request_type) mecab_lattice_add_request_type;
278 
279     /**
280      * C wrapper of MeCab::Lattice::remove_request_type()
281      */
282     void function(mecab_lattice_t* lattice, int request_type) mecab_lattice_remove_request_type;
283 
284     /**
285      * C wrapper of MeCab::Lattice::newNode();
286      */
287     mecab_node_t* function(mecab_lattice_t* lattice) mecab_lattice_new_node;
288 
289     /**
290      * C wrapper of MeCab::Lattice::toString()
291      */
292     const(char)* function(mecab_lattice_t* lattice) mecab_lattice_tostr;
293 
294     /**
295      * C wrapper of MeCab::Lattice::toString(buf, size)
296      */
297     const(char)* function(mecab_lattice_t* lattice, char* buf, size_t size) mecab_lattice_tostr2;
298 
299     /**
300      * C wrapper of MeCab::Lattice::enumNBestAsString(N)
301      */
302     const(char)* function(mecab_lattice_t* lattice, size_t N) mecab_lattice_nbest_tostr;
303 
304     /**
305      * C wrapper of MeCab::Lattice::enumNBestAsString(N, buf, size)
306      */
307     const(char)* function(mecab_lattice_t* lattice, size_t N, char* buf, size_t size) mecab_lattice_nbest_tostr2;
308 
309     /**
310      * C wrapper of MeCab::Lattice::has_constraint()
311      */
312     int function(mecab_lattice_t* lattice) mecab_lattice_has_constraint;
313 
314     /**
315      * C wrapper of MeCab::Lattice::boundary_constraint(pos)
316      */
317     int function(mecab_lattice_t* lattice, size_t pos) mecab_lattice_get_boundary_constraint;
318 
319     /**
320      * C wrapper of MeCab::Lattice::feature_constraint(pos)
321      */
322     const(char)* function(mecab_lattice_t* lattice, size_t pos) mecab_lattice_get_feature_constraint;
323 
324     /**
325      * C wrapper of MeCab::Lattice::boundary_constraint(pos, type)
326      */
327     void function(mecab_lattice_t* lattice, size_t pos, int boundary_type) mecab_lattice_set_boundary_constraint;
328 
329     /**
330      * C wrapper of MeCab::Lattice::set_feature_constraint(begin_pos, end_pos, feature)
331      */
332     void function(mecab_lattice_t* lattice, size_t begin_pos, size_t end_pos, const(char)* feature) mecab_lattice_set_feature_constraint;
333 
334     /**
335      * C wrapper of MeCab::Lattice::what()
336      */
337     const(char)* function(mecab_lattice_t* lattice) mecab_lattice_strerror;
338 
339     /* model interface */
340     /**
341      * C wapper of MeCab::Model::create(argc, argv)
342      */
343     mecab_model_t* function(int argc, char** argv) mecab_model_new;
344 
345     /**
346      * C wapper of MeCab::Model::create(arg)
347      */
348     mecab_model_t* function(const(char)* arg) mecab_model_new2;
349 
350     /**
351      * C wapper of MeCab::deleteModel(model)
352      */
353     void function(mecab_model_t* model) mecab_model_destroy;
354 
355     /**
356      * C wapper of MeCab::Model::createTagger()
357      */
358     mecab_t* function(mecab_model_t* model) mecab_model_new_tagger;
359 
360     /**
361      * C wapper of MeCab::Model::createLattice()
362      */
363     mecab_lattice_t* function(mecab_model_t* model) mecab_model_new_lattice;
364 
365     /**
366      * C wrapper of MeCab::Model::swap()
367      */
368     int function(mecab_model_t* model, mecab_model_t* new_model) mecab_model_swap;
369 
370     /**
371      * C wapper of MeCab::Model::dictionary_info()
372      */
373     const(mecab_dictionary_info_t)* function(mecab_model_t* model) mecab_model_dictionary_info;
374 
375     /**
376      * C wrapper of MeCab::Model::transition_cost()
377      */
378     int function(mecab_model_t* model, ushort rcAttr, ushort lcAttr) mecab_model_transition_cost;
379 
380     /**
381      * C wrapper of MeCab::Model::lookup()
382      */
383     mecab_node_t* function(mecab_model_t* model, const(char)* begin, const(char)* end, mecab_lattice_t* lattice) mecab_model_lookup;
384 
385     /* static functions */
386     int function(int argc, char** argv) mecab_do;
387     int function(int argc, char** argv) mecab_dict_index;
388     int function(int argc, char** argv) mecab_dict_gen;
389     int function(int argc, char** argv) mecab_cost_train;
390     int function(int argc, char** argv) mecab_system_eval;
391     int function(int argc, char** argv) mecab_test_gen;
392 }
393 
394 enum MeCabSupport
395 {
396     noLibrary,
397     badLibrary,
398     mecab,
399 }
400 
401 private __gshared SharedLib lib;
402 
403 void unloadMeCab()
404 {
405     if (lib != invalidHandle)
406     {
407         lib.unload();
408     }
409 }
410 
411 MeCabSupport loadMeCab()
412 {
413     version (Windows)
414     {
415         const(char)[][1] libNames = ["libmecab.dll"];
416     }
417     else version (OSX)
418     {
419         const(char)[][1] libNames = ["libmecab.dylib"];
420     }
421     else version (Posix)
422     {
423         const(char)[][1] libNames = ["mecab.so"];
424     }
425     else
426     {
427         static assert(false);
428     }
429 
430     lib = load(libNames[0].ptr);
431     if (lib == invalidHandle)
432     {
433         return MeCabSupport.noLibrary;
434     }
435 
436     void bind(alias symbol)()
437     {
438         lib.bindSymbol(cast(void**)&symbol, symbol.stringof);
439     }
440 
441     const errCount = errorCount();
442     bind!(mecab_new);
443     bind!(mecab_new2);
444     bind!(mecab_version);
445     bind!(mecab_strerror);
446     bind!(mecab_destroy);
447     bind!(mecab_get_partial);
448     bind!(mecab_set_partial);
449     bind!(mecab_get_theta);
450     bind!(mecab_set_theta);
451     bind!(mecab_get_lattice_level);
452     bind!(mecab_set_lattice_level);
453     bind!(mecab_get_all_morphs);
454     bind!(mecab_set_all_morphs);
455     bind!(mecab_parse_lattice);
456     bind!(mecab_sparse_tostr);
457     bind!(mecab_sparse_tostr2);
458     bind!(mecab_sparse_tostr3);
459     bind!(mecab_sparse_tonode);
460     bind!(mecab_sparse_tonode2);
461     bind!(mecab_nbest_sparse_tostr);
462     bind!(mecab_nbest_sparse_tostr2);
463     bind!(mecab_nbest_sparse_tostr3);
464     bind!(mecab_nbest_init);
465     bind!(mecab_nbest_init2);
466     bind!(mecab_nbest_next_tostr);
467     bind!(mecab_nbest_next_tostr2);
468     bind!(mecab_nbest_next_tonode);
469     bind!(mecab_format_node);
470     bind!(mecab_dictionary_info);
471     bind!(mecab_lattice_new);
472     bind!(mecab_lattice_destroy);
473     bind!(mecab_lattice_clear);
474     bind!(mecab_lattice_is_available);
475     bind!(mecab_lattice_get_bos_node);
476     bind!(mecab_lattice_get_eos_node);
477     bind!(mecab_lattice_get_all_begin_nodes);
478     bind!(mecab_lattice_get_all_end_nodes);
479     bind!(mecab_lattice_get_begin_nodes);
480     bind!(mecab_lattice_get_end_nodes);
481     bind!(mecab_lattice_get_sentence);
482     bind!(mecab_lattice_set_sentence);
483     bind!(mecab_lattice_set_sentence2);
484     bind!(mecab_lattice_get_size);
485     bind!(mecab_lattice_get_z);
486     bind!(mecab_lattice_set_z);
487     bind!(mecab_lattice_get_theta);
488     bind!(mecab_lattice_set_theta);
489     bind!(mecab_lattice_next);
490     bind!(mecab_lattice_get_request_type);
491     bind!(mecab_lattice_has_request_type);
492     bind!(mecab_lattice_set_request_type);
493     bind!(mecab_lattice_add_request_type);
494     bind!(mecab_lattice_remove_request_type);
495     bind!(mecab_lattice_new_node);
496     bind!(mecab_lattice_tostr);
497     bind!(mecab_lattice_tostr2);
498     bind!(mecab_lattice_nbest_tostr);
499     bind!(mecab_lattice_nbest_tostr2);
500     bind!(mecab_lattice_has_constraint);
501     bind!(mecab_lattice_get_boundary_constraint);
502     bind!(mecab_lattice_get_feature_constraint);
503     bind!(mecab_lattice_set_boundary_constraint);
504     bind!(mecab_lattice_set_feature_constraint);
505     bind!(mecab_lattice_strerror);
506     bind!(mecab_model_new);
507     bind!(mecab_model_new2);
508     bind!(mecab_model_destroy);
509     bind!(mecab_model_new_tagger);
510     bind!(mecab_model_new_lattice);
511     bind!(mecab_model_swap);
512     bind!(mecab_model_dictionary_info);
513     bind!(mecab_model_transition_cost);
514     bind!(mecab_model_lookup);
515 
516     /* static functions */
517     bind!(mecab_do);
518     bind!(mecab_dict_index);
519     bind!(mecab_dict_gen);
520     bind!(mecab_cost_train);
521     bind!(mecab_system_eval);
522     bind!(mecab_test_gen);
523 
524     if (errorCount() != errCount)
525         return MeCabSupport.badLibrary;
526 
527     return MeCabSupport.mecab;
528 }