-
Notifications
You must be signed in to change notification settings - Fork 18
/
demo.c
97 lines (82 loc) · 2.6 KB
/
demo.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
#include <stdio.h>
#include <stdlib.h>
#include "lib/jieba.h"
#include <string.h>
const char* DICT_PATH = "./dict/jieba.dict.utf8";
const char* HMM_PATH = "./dict/hmm_model.utf8";
const char* USER_DICT = "./dict/user.dict.utf8";
const char* IDF_PATH = "./dict/idf.utf8";
const char* STOP_WORDS_PATH = "./dict/stop_words.utf8";
void CutDemo() {
printf("CutDemo:\n");
// init will take a few seconds to load dicts.
Jieba handle = NewJieba(DICT_PATH, HMM_PATH, USER_DICT, IDF_PATH, STOP_WORDS_PATH);
const char* s = "南京市长江大桥";
size_t len = strlen(s);
CJiebaWord* words = Cut(handle, s, len);
CJiebaWord* x;
for (x = words; x && x->word; x++) {
printf("%*.*s\n", x->len, x->len, x->word);
}
FreeWords(words);
FreeJieba(handle);
}
void CutWithoutTagNameDemo() {
printf("CutWithoutTagNameDemo:\n");
// init will take a few seconds to load dicts.
Jieba handle = NewJieba(DICT_PATH, HMM_PATH, USER_DICT, IDF_PATH, STOP_WORDS_PATH);
const char* s = "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。";
size_t len = strlen(s);
CJiebaWord* words = CutWithoutTagName(handle, s, len, "x");
CJiebaWord* x;
for (x = words; x->word; x++) {
printf("%*.*s\n", x->len, x->len, x->word);
}
FreeWords(words);
FreeJieba(handle);
}
void ExtractDemo() {
printf("ExtractDemo:\n");
// init will take a few seconds to load dicts.
Extractor handle = NewExtractor(DICT_PATH,
HMM_PATH,
IDF_PATH,
STOP_WORDS_PATH,
USER_DICT);
const char* s = "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。";
size_t top_n = 5;
CJiebaWord* words = Extract(handle, s, strlen(s), top_n);
CJiebaWord* x;
for (x = words; x && x->word; x++) {
printf("%*.*s\n", x->len, x->len, x->word);
}
FreeWords(words);
FreeExtractor(handle);
}
void UserWordDemo()
{
printf("UserWordDemo:\n");
Jieba handle = NewJieba(DICT_PATH, HMM_PATH, USER_DICT, IDF_PATH, STOP_WORDS_PATH);
const char* s = "人艰不拆";
size_t len = strlen(s);
CJiebaWord* words = Cut(handle, s, len);
CJiebaWord* x;
for (x = words; x->word; x++) {
printf("%*.*s\n", x->len, x->len, x->word);
}
FreeWords(words);
JiebaInsertUserWord(handle, "人艰不拆");
words = Cut(handle, s, len);
for (x = words; x->word; x++) {
printf("%*.*s\n", x->len, x->len, x->word);
}
FreeWords(words);
FreeJieba(handle);
}
int main(int argc, char** argv) {
CutDemo();
CutWithoutTagNameDemo();
ExtractDemo();
UserWordDemo();
return 0;
}