-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathheadword.c
165 lines (139 loc) · 2.79 KB
/
headword.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
/*
+----------------------------------------------------------------------+
| Author: Xingzhi Liu <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "recognizer.h"
#include "charset.h"
//提取中心词
void *headword_init(const char *dicdir, int codetype)
{
void * Dic;
Dic = recognizer_init(NULL, dicdir, RECOGNIZER_INIT_TYPE_LOAD, 0, codetype);
return Dic;
}
int headword_extract(void *Dic, const char *content, char *wordbuf, int bufsize,int codetype)
{
char words[MAX_SEGMENT_OUTPUT+1];
int len, num, buflen, cnt,clen;
char *p, *q, *pc;
if (!Dic || !content || !wordbuf || !bufsize)
return -1;
if(codetype != CODETYPE_GBK && codetype != CODETYPE_UTF8)
return -1;
len = strlen(content);
if (len > MAX_SEGMENT_STRING)
return -1;
if (!len)
{
wordbuf[0] = 0;
return 0;
}
if ((num = recognizer_words(Dic, content, words)) < 0)
return -1;
if (!num)
{
wordbuf[0] = 0;
return 0;
}
buflen = 0;
cnt = 0;
p = words;
q = p;
while (*q)
{
if(codetype == CODETYPE_GBK)
clen = GBKCHLEN((unsigned char)*q);
else if(codetype == CODETYPE_UTF8)
clen = UTF8CHLEN((unsigned char)*q);
if (clen > 1)
{
if (*(q + clen) == '\0')
{
break;
}
else
{
q += clen;
continue;
}
}
if (*q == ' ' || *(q + 1) == '\0') //注意最后一个地址元素后面没有分隔符
{
if (*q == ' ')
*q = 0;
pc = strrchr(p, WORD_TYPE_SEPARATOR);
if (pc)
{
*pc = 0;
len = pc - p;
if (buflen + len + 1 >= bufsize)
return -1;
else
{
memcpy(wordbuf + buflen, p, len);
buflen += len;
wordbuf[buflen] = ' ';
buflen++;
cnt++;
}
q++;
p = q;
}
else
{
q++;
p = q;
}
}
else
{
q++;
}
}
if (buflen)
wordbuf[buflen-1] = 0;
else
wordbuf[buflen] = 0;
return cnt;
}
void headword_free(void *Dic)
{
recognizer_free(Dic);
}
#ifdef TEST_HEADWORD
#include "xstring.h"
int main(int argc,char *argv[])
{
void *dic;
char str[1024],words[1024];
if(argc != 3)
{
printf("arguments set error.\n");
return -1;
}
dic = headword_init(argv[1],atoi(argv[2]));
if(!dic)
{
printf("headword_init() error.\n");
return -1;
}
while(fgets(str,1024,stdin))
{
trim(str);
if(strcasecmp(str,"exit") == 0)
break;
if(headword_extract(dic,str,words,1024,atoi(argv[2])) > 0)
{
printf("words: %s\n",words);
}
else
break;
}
headword_free(dic);
return 0;
}
#endif