1 | /* |
---|
2 | * cabocha.c |
---|
3 | * |
---|
4 | * 2009.3.15 by naoya_t |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include <gauche.h> |
---|
9 | #include <gauche/extend.h> |
---|
10 | |
---|
11 | #include <mecab.h> |
---|
12 | #include "mecab.h" |
---|
13 | #include <cabocha.h> |
---|
14 | #include "cabocha.h" |
---|
15 | |
---|
16 | // cabocha_t |
---|
17 | cabocha_t* unwrap_cabocha_t(ScmObj obj) |
---|
18 | { |
---|
19 | return SCM_CABOCHA(obj)->c; |
---|
20 | } |
---|
21 | ScmObj wrap_cabocha_t(cabocha_t *c) |
---|
22 | { |
---|
23 | ScmCaboCha *obj = SCM_NEW(ScmCaboCha); |
---|
24 | SCM_SET_CLASS(obj, SCM_CLASS_CABOCHA); |
---|
25 | obj->c = c; |
---|
26 | return SCM_OBJ(obj); |
---|
27 | } |
---|
28 | |
---|
29 | // cabocha_tree_t |
---|
30 | cabocha_tree_t* unwrap_cabocha_tree_t(ScmObj obj) |
---|
31 | { |
---|
32 | return SCM_CABOCHA_TREE(obj)->ctree; |
---|
33 | } |
---|
34 | ScmObj wrap_cabocha_tree_t(cabocha_tree_t *ctree) |
---|
35 | { |
---|
36 | ScmCaboChaTree *obj = SCM_NEW(ScmCaboChaTree); |
---|
37 | SCM_SET_CLASS(obj, SCM_CLASS_CABOCHA_TREE); |
---|
38 | obj->ctree = ctree; |
---|
39 | return SCM_OBJ(obj); |
---|
40 | } |
---|
41 | |
---|
42 | // cabocha_chunk_t |
---|
43 | cabocha_chunk_t* unwrap_cabocha_chunk_t(ScmObj obj) |
---|
44 | { |
---|
45 | return SCM_CABOCHA_CHUNK(obj)->cchunk; |
---|
46 | } |
---|
47 | ScmObj wrap_cabocha_chunk_t(cabocha_chunk_t *cchunk) |
---|
48 | { |
---|
49 | ScmCaboChaChunk *obj = SCM_NEW(ScmCaboChaChunk); |
---|
50 | SCM_SET_CLASS(obj, SCM_CLASS_CABOCHA_CHUNK); |
---|
51 | obj->cchunk = cchunk; |
---|
52 | return SCM_OBJ(obj); |
---|
53 | } |
---|
54 | |
---|
55 | // cabocha_token_t |
---|
56 | cabocha_token_t* unwrap_cabocha_token_t(ScmObj obj) |
---|
57 | { |
---|
58 | return SCM_CABOCHA_TOKEN(obj)->ctok; |
---|
59 | } |
---|
60 | ScmObj wrap_cabocha_token_t(cabocha_token_t *ctok) |
---|
61 | { |
---|
62 | ScmCaboChaToken *obj = SCM_NEW(ScmCaboChaToken); |
---|
63 | SCM_SET_CLASS(obj, SCM_CLASS_CABOCHA_TOKEN); |
---|
64 | obj->ctok = ctok; |
---|
65 | return SCM_OBJ(obj); |
---|
66 | } |
---|
67 | |
---|
68 | /* mecab_node_t */ |
---|
69 | const mecab_node_t* unwrap_mecab_node_t(ScmObj obj) |
---|
70 | { |
---|
71 | return SCM_MECAB_NODE(obj)->mn; |
---|
72 | } |
---|
73 | ScmObj wrap_mecab_node_t(const mecab_node_t *mn) |
---|
74 | { |
---|
75 | ScmMeCabNode *obj = SCM_NEW(ScmMeCabNode); |
---|
76 | SCM_SET_CLASS(obj, SCM_CLASS_MECAB_NODE); |
---|
77 | obj->mn = mn; |
---|
78 | return SCM_OBJ(obj); |
---|
79 | } |
---|
80 | |
---|
81 | /* APIs with (int argc, char **argv) */ |
---|
82 | #define cabocha_call_func(result_type,fn,args) do{ \ |
---|
83 | result_type result; \ |
---|
84 | if (SCM_NULLP(args)) { \ |
---|
85 | char *argv[] = {""}; \ |
---|
86 | result = (fn)(1,argv); \ |
---|
87 | } else { \ |
---|
88 | int argc, i=0; \ |
---|
89 | ScmObj argl; \ |
---|
90 | char **argv = NULL; \ |
---|
91 | if (SCM_INTEGERP(Scm_Car(args))) { \ |
---|
92 | argc = SCM_INT_VALUE(Scm_Car(args)); argl = Scm_Cadr(args); \ |
---|
93 | } else { \ |
---|
94 | argc = 1 + Scm_Length(args); i++; argl = args; \ |
---|
95 | } \ |
---|
96 | argv = (char**)malloc(sizeof(char*) * argc); \ |
---|
97 | if (i) argv[0] = ""; \ |
---|
98 | if (argv) { \ |
---|
99 | if (SCM_VECTORP(argl)) { \ |
---|
100 | for (;i<argc;i++) \ |
---|
101 | argv[i] = (char *)Scm_GetStringConst((ScmString *)SCM_VECTOR_ELEMENT(argl,i)); \ |
---|
102 | } else { \ |
---|
103 | for (;i<argc && !SCM_NULLP(argl); argl=Scm_Cdr(argl),i++) \ |
---|
104 | argv[i] = (char *)Scm_GetStringConst((ScmString *)Scm_Car(argl)); \ |
---|
105 | } \ |
---|
106 | result = (fn)(argc,argv); \ |
---|
107 | } \ |
---|
108 | free((void *)argv); \ |
---|
109 | } \ |
---|
110 | return result; \ |
---|
111 | } while(0) |
---|
112 | |
---|
113 | cabocha_t *cabocha_call_cabocha_func(cabocha_func_with_args fn, ScmObj args) |
---|
114 | { |
---|
115 | cabocha_call_func(cabocha_t*,fn,args); |
---|
116 | } |
---|
117 | int cabocha_call_int_func(int_func_with_args fn, ScmObj args) |
---|
118 | { |
---|
119 | cabocha_call_func(int,fn,args); |
---|
120 | } |
---|
121 | |
---|
122 | ScmObj Scm_MakeStringList(const char **strs, int size) |
---|
123 | { |
---|
124 | if (size==0) return SCM_NIL; |
---|
125 | ScmObj p = Scm_Cons(SCM_MAKE_STR_COPYING(strs[size-1]),SCM_NIL); |
---|
126 | for (int i=size-2; i>=0; i--) { |
---|
127 | p = Scm_Cons(SCM_MAKE_STR_COPYING(strs[i]), p); |
---|
128 | } |
---|
129 | return p; |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * Module initialization function. |
---|
134 | */ |
---|
135 | extern void Scm_Init_cabochalib(ScmModule*); |
---|
136 | |
---|
137 | void Scm_Init_cabocha(void) |
---|
138 | { |
---|
139 | ScmModule *mod; |
---|
140 | |
---|
141 | /* Register this DSO to Gauche */ |
---|
142 | SCM_INIT_EXTENSION(cabocha); |
---|
143 | |
---|
144 | /* Create the module if it doesn't exist yet. */ |
---|
145 | mod = SCM_MODULE(SCM_FIND_MODULE("cabocha", TRUE)); |
---|
146 | |
---|
147 | /* Register stub-generated procedures */ |
---|
148 | Scm_Init_cabochalib(mod); |
---|
149 | } |
---|