forked from fumiyas/hyperestraier-encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymorph.c
377 lines (353 loc) · 13.7 KB
/
mymorph.c
1
2
3
4
5
6
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
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
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
160
161
162
163
164
165
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
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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*************************************************************************************************
* Morphological analyzers for Hyper Estraier
* Copyright (C) 2004-2007 Mikio Hirabayashi
* This file is part of Hyper Estraier.
* Hyper Estraier is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Hyper Estraier is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Hyper
* Estraier; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "mymorph.h"
#include "estraier.h"
#include "myconf.h"
#if defined(_MYMECAB)
#include <mecab.h>
#endif
#define ESTNUMBUFSIZ 32 /* size of a buffer for a number */
#define ESTSCOREUNIT 100 /* unit of score of each word */
#define ESTWORDMAXLEN 48 /* maximum length of a word */
typedef struct { /* type of structure for a hitting object */
const char *word; /* face of keyword */
int pt; /* score tuned by TF-IDF */
} ESTKEYSC;
/* private function prototypes */
static void est_uwords_add(CBMAP *uwords, const char *word, int wsiz, int pt);
static int est_keysc_compare(const void *ap, const void *bp);
static int est_mecab_analyze(const char *text, CBMAP *uwords);
/*************************************************************************************************
* pseudo API
*************************************************************************************************/
/* Extract keywords of a document object using morphological analyzers. */
CBMAP *est_morph_etch_doc(ESTDOC *doc, int max){
ESTKEYSC *scores;
CBMAP *kwords, *uwords;
const CBLIST *texts;
CBLIST *words;
const char *text, *word;
const unsigned char *uword;
char numbuf[ESTNUMBUFSIZ];
char *enc;
int i, j, all, miss, wsiz, pt, snum;
double dval;
assert(doc && max >= 0);
kwords = cbmapopenex(max * 2 + 1);
uwords = cbmapopen();
all = 0;
texts = est_doc_texts(doc);
for(i = -1; i < cblistnum(texts); i++){
text = i < 0 ? est_doc_hidden_texts(doc) : cblistval(texts, i, NULL);
if(text[0] == '\0') continue;
if(ESTUSEMECAB && (enc = est_iconv(text, -1, "UTF-8", "EUC-JP", NULL, &miss)) != NULL){
if(miss > 0){
free(enc);
} else {
all += est_mecab_analyze(enc, uwords);
free(enc);
continue;
}
}
words = cblistopen();
est_break_text_chrcat(text, words, FALSE);
for(j = 0; j < cblistnum(words); j++){
uword = (unsigned char *)cblistval(words, j, &wsiz);
if(uword[0] >= 0xe3){
if(wsiz <= 3){
pt = ESTSCOREUNIT / 4;
if((uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) ||
(uword[0] == 0xef && uword[1] >= 0xbc)) pt /= 10;
} else {
pt = ESTSCOREUNIT;
if((uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) ||
(uword[0] == 0xef && uword[1] >= 0xbc)) pt /= 2;
if((uword[3] == 0xe3 && (uword[4] == 0x80 || uword[4] == 0x81 ||
(uword[4] == 0x82 && uword[5] <= 0x9f))) ||
(uword[3] == 0xef && uword[4] >= 0xbc)) pt /= 2;
}
} else if((uword[0] > '\0' && uword[0] <= '/') || (uword[0] >= '[' && uword[0] <= '`') ||
(uword[0] >= '{' && uword[0] <= '~')){
pt = ESTSCOREUNIT / 50;
} else {
switch(wsiz){
case 1:
pt = ESTSCOREUNIT / 9;
break;
case 2:
pt = ESTSCOREUNIT / 5;
break;
case 3:
pt = ESTSCOREUNIT / 3;
break;
case 4:
pt = ESTSCOREUNIT / 2;
break;
case 5:
pt = ESTSCOREUNIT / 1.5;
break;
case 6:
pt = ESTSCOREUNIT / 1.25;
break;
default:
pt = ESTSCOREUNIT;
break;
}
}
est_uwords_add(uwords, (char *)uword, wsiz, pt);
all += pt;
}
cblistclose(words);
}
scores = cbmalloc(cbmaprnum(uwords) * sizeof(ESTKEYSC) + 1);
snum = 0;
cbmapiterinit(uwords);
while((word = cbmapiternext(uwords, &wsiz)) != NULL){
scores[snum].word = word;
scores[snum].pt = *(int *)cbmapget(uwords, word, wsiz, NULL);
snum++;
}
qsort(scores, snum, sizeof(ESTKEYSC), est_keysc_compare);
dval = log(all + ESTSCOREUNIT * 64);
dval *= dval;
dval /= 10;
for(i = 0; i < snum && i < max; i++){
pt = (int)(scores[i].pt / dval);
wsiz = sprintf(numbuf, "%d", pt > 1 ? pt : 1);
cbmapput(kwords, scores[i].word, -1, numbuf, wsiz, FALSE);
}
free(scores);
cbmapclose(uwords);
return kwords;
}
/*************************************************************************************************
* private objects
*************************************************************************************************/
/* Add a record to a unique word map.
`uwords' specifies a unique word map.
`word' specifies a word.
`wsiz' specifies size of the word.
`pt' specifies point of the word. */
static void est_uwords_add(CBMAP *uwords, const char *word, int wsiz, int pt){
const char *vbuf;
int num;
assert(uwords && word && wsiz >= 0);
if(wsiz > ESTWORDMAXLEN) return;
num = (vbuf = cbmapget(uwords, word, wsiz, NULL)) ? *(int *)vbuf + pt : pt;
if(num < 0) num = 0;
cbmapput(uwords, word, wsiz, (char *)&num, sizeof(int), TRUE);
}
/* Compare two keywords by scores in descending order.
`ap' specifies the pointer to one keyword.
`bp' specifies the pointer to the other keyword.
The return value is negative if one is small, positive if one is big, 0 if both are equal. */
static int est_keysc_compare(const void *ap, const void *bp){
assert(ap && bp);
return ((ESTKEYSC *)bp)->pt - ((ESTKEYSC *)ap)->pt;
}
/* Analyze text with using mecab.
`text' specifies a sentence whose encoding is EUC-JP.
`uwords' specifies a unique word map.
The return value is the total points of extracted words. */
static int est_mecab_analyze(const char *text, CBMAP *uwords){
#if defined(_MYMECAB)
static mecab_t *mecab = NULL;
static CBMAP *swmap = NULL;
static char *args[] = { "mecab", "--node-format=%m\\t%f[0]\\t%f[1]\\n",
"--unk-format=%m\\t\\t\\n", "--eos-format=", NULL };
static char *stops[] = {
"a", "an", "the", "there", "any", "no", "each", "all", "most", "more",
"i", "my", "me", "mine", "you", "your", "yours", "we", "our", "us", "ours",
"he", "his", "him", "she", "her", "hers", "it", "its", "they", "their", "them", "theirs",
"this", "these", "that", "those",
"what", "who", "which", "where", "when", "why", "whose", "how",
"or", "and", "but", "not", "as", "if", "so", "because", "as", "unless", "until", "while",
"to", "on", "of", "in", "for", "from", "by", "with", "at", "against", "via", "under", "over",
"be", "am", "are", "is", "was", "were", "being", "wanna", "gonna", "gotta", "going",
"do", "does", "did", "have", "has", "had", "get", "got", "go", "went", "come", "came",
"will", "would", "ll", "ve", "can", "could", "may", "might", "shall", "should", "must",
NULL
};
CBDATUM *prefix;
const char *cres;
unsigned char *uword;
char *res, *pv, *ep, *word, *sign, *info;
int i, all, ppt, pnum, wsiz, pt;
assert(text && uwords);
if(!mecab){
if(!(mecab = mecab_new(sizeof(args) / sizeof(char *) - 1, args))) return 0;
/* for (maybe) mecab's bug
cbglobalgc(mecab, (void (*)(void *))mecab_destroy);
*/
swmap = cbmapopenex(sizeof(stops) / sizeof(char *) * 2 + 1);
cbglobalgc(swmap, (void (*)(void *))cbmapclose);
for(i = 0; stops[i]; i++){
cbmapput(swmap, stops[i], -1, "", 0, FALSE);
}
}
if(!(cres = mecab_sparse_tostr(mecab, (char *)text))) return 0;
if(!(res = est_iconv(cres, -1, "EUC-JP", "UTF-8", NULL, NULL))) return 0;
all = 0;
pv = res;
ep = pv;
prefix = cbdatumopen(NULL, -1);
pnum = 0;
ppt = 0;
while(ep){
if((ep = strchr(pv, '\n')) != NULL) *ep = '\0';
word = pv;
if((sign = strchr(pv, '\t')) != NULL){
*(sign++) = '\0';
pv = sign;
} else {
sign = "";
}
if((info = strchr(pv, '\t')) != NULL){
*(info++) = '\0';
pv = info;
} else {
info = "";
}
for(pv = word; *pv != '\0'; pv++){
if(*pv >= 'A' && *pv <= 'Z') *pv += 'a' - 'A';
}
wsiz = pv - word;
if(wsiz > 0){
uword = (unsigned char *)word;
if(!strcmp(sign, "記号")){
pt = ESTSCOREUNIT / 100;
} else if(!strcmp(sign, "助詞") || !strcmp(sign, "助動詞") || !strcmp(sign, "接頭詞") ||
!strcmp(sign, "接続詞") || !strcmp(sign, "フィラー") || !strcmp(sign, "その他")){
pt = ESTSCOREUNIT / 50;
} else if(!strcmp(sign, "名詞") || !strcmp(sign, "動詞")){
pt = ESTSCOREUNIT;
if(!strcmp(info, "非自立") || !strcmp(info, "特殊") || !strcmp(info, "引用文字列") ||
!strcmp(info, "数") || !strcmp(info, "接続詞的") || !strcmp(info, "接尾") ||
!strcmp(info, "代名詞") || !strcmp(info, "動詞非自立的")){
pt /= 2;
}
if(wsiz <= 3){
pt /= 5;
if(uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) pt /= 5;
} else {
if(uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) pt /= 5;
if(uword[3] == 0xe3 && (uword[4] == 0x80 || uword[4] == 0x81 ||
(uword[4] == 0x82 && uword[5] <= 0x9f))) pt /= 2.5;
if(wsiz <= 6) pt /= 1.5;
}
} else if(!strcmp(sign, "形容詞") || !strcmp(sign, "副詞") || !strcmp(sign, "連体詞") ||
!strcmp(sign, "感動詞")){
pt = ESTSCOREUNIT / 2;
if(wsiz <= 3){
pt /= 5;
if(uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) pt /= 5;
} else {
if(uword[0] == 0xe3 && (uword[1] == 0x80 || uword[1] == 0x81 ||
(uword[1] == 0x82 && uword[2] <= 0x9f))) pt /= 5;
if(uword[3] == 0xe3 && (uword[4] == 0x80 || uword[4] == 0x81 ||
(uword[4] == 0x82 && uword[5] <= 0x9f))) pt /= 2.5;
if(wsiz <= 6) pt /= 1.5;
}
} else if((word[0] > '\0' && word[0] <= '/') || (word[0] >= ':' && word[0] <= '@') ||
(word[0] >= '[' && word[0] <= '`') || (word[0] >= '{' && word[0] <= '~')){
pt = ESTSCOREUNIT / 50;
if(wsiz <= 1) pt /= 2;
} else if(cbmapget(swmap, word, wsiz, NULL)){
pt = ESTSCOREUNIT / 25;
if(wsiz <= 1) pt /= 2;
} else {
switch(wsiz){
case 1:
pt = ESTSCOREUNIT / 9;
break;
case 2:
pt = ESTSCOREUNIT / 5;
break;
case 3:
pt = ESTSCOREUNIT / 3;
break;
case 4:
pt = ESTSCOREUNIT / 2;
break;
case 5:
pt = ESTSCOREUNIT / 1.5;
break;
case 6:
pt = ESTSCOREUNIT / 1.25;
break;
default:
pt = ESTSCOREUNIT;
break;
}
}
est_uwords_add(uwords, (char *)uword, wsiz, pt);
all += pt;
if(!strcmp(sign, "名詞")){
ppt += pt;
pnum++;
if(cbdatumsize(prefix) > 0){
est_uwords_add(uwords, cbdatumptr(prefix), cbdatumsize(prefix),
(double)ppt / pnum * -0.15 - 1);
est_uwords_add(uwords, (char *)uword, wsiz, pt * -0.15 - 1);
cbdatumcat(prefix, word, wsiz);
est_uwords_add(uwords, cbdatumptr(prefix), cbdatumsize(prefix),
((double)ppt / pnum) * 0.9);
all += (ppt / pnum) * 0.8;
} else {
cbdatumcat(prefix, word, wsiz);
}
} else if(!strcmp(sign, "") && (cbdatumsize(prefix) < 1 || uword[0] >= 0xc0)){
ppt += pt * 0.8;
pnum++;
if(cbdatumsize(prefix) > 0){
est_uwords_add(uwords, cbdatumptr(prefix), cbdatumsize(prefix),
(double)ppt / pnum * -0.15 - 1);
est_uwords_add(uwords, (char *)uword, wsiz, pt * -0.15 - 1);
cbdatumcat(prefix, word, wsiz);
est_uwords_add(uwords, cbdatumptr(prefix), cbdatumsize(prefix),
((double)ppt / pnum) * 0.9);
all += (ppt / pnum) * 0.8;
} else if(wsiz > 1 || uword[0] >= 0x80 || (word[0] >= '0' && word[0] <= '9') ||
(word[0] >= 'A' && word[0] <= 'Z') || (word[0] >= 'a' && word[0] <= 'z')){
cbdatumcat(prefix, word, wsiz);
} else {
ppt = 0;
pnum = 0;
}
} else {
cbdatumsetsize(prefix, 0);
pnum = 0;
ppt = 0;
}
} else {
cbdatumsetsize(prefix, 0);
pnum = 0;
ppt = 0;
}
pv = ep + 1;
}
cbdatumclose(prefix);
free(res);
return all;
#else
return 0;
#endif
}
/* END OF FILE */