forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcws_cmdline.cpp
70 lines (60 loc) · 1.73 KB
/
cws_cmdline.cpp
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
/*
* Single-threaded segmentor test program. The user input a line
* of Chinese sentence an the program will output its segment
* result.
*
* @dependency package: tinythread - a portable c++ wrapper for
* multi-thread library.
* @author: LIU, Yijia
* @data: 2013-09-24
*
* This program is special designed for UNIX user, for get time
* is not compilable under MSVC
*/
#include <iostream>
#include <ctime>
#include <string>
#include <sys/time.h>
#include <sys/types.h>
#include "segment_dll.h"
double get_time(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec / 1000000.0);
}
int main(int argc, char * argv[]) {
if (argc < 2) {
std::cerr << "cws [model path] [lexicon_file]" << std::endl;
return 1;
}
void * engine = 0;
if (argc == 2) {
engine = segmentor_create_segmentor(argv[1]);
} else if (argc == 3) {
engine = segmentor_create_segmentor(argv[1], argv[2]);
}
if (!engine) {
return -1;
}
std::vector<std::string> words;
std::string sentence;
std::cerr << "TRACE: Model is loaded" << std::endl;
double tm = get_time();
while (std::getline(std::cin, sentence, '\n')) {
words.clear();
if (sentence.size() == 0) { continue; }
int len = segmentor_segment(engine, sentence, words);
for (int i = 0; i < len; ++ i) {
std::cout << words[i];
if (i+1 == len) std::cout <<std::endl;
else std::cout<< "|";
}
}
segmentor_release_segmentor(engine);
tm = get_time() - tm;
std::cerr << "TRACE: consume "
<< tm
<< " seconds."
<< std::endl;
return 0;
}