forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_cws_cmdline.cpp
141 lines (115 loc) · 3.32 KB
/
multi_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
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
/*
* Multi-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 <cstring>
#include <ctime>
#include <vector>
#include <list>
#include <sys/time.h>
#include <sys/types.h>
#include "segment_dll.h"
#include "tinythread.h"
#include "fast_mutex.h"
using namespace std;
using namespace tthread;
const int MAX_LEN = 1024;
double get_time(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec / 1000000.0);
}
class Dispatcher {
public:
Dispatcher( void * model ) {
_model = model;
}
int next(string &sentence) {
sentence = "";
lock_guard<fast_mutex> guard(_mutex);
if (!getline(cin, sentence, '\n')) {
return -1;
}
return 0;
}
void output(const vector<string> &result) {
lock_guard<fast_mutex> guard(_mutex);
for (int i = 0; i < result.size(); ++ i) {
cout << result[i];
cout << (i == result.size() - 1 ? '\n' : '|');
}
return;
}
void * model() {
return _model;
}
private:
fast_mutex _mutex;
void * _model;
string _sentence;
};
void multithreaded_segment( void * args) {
string sentence;
vector<string> result;
Dispatcher * dispatcher = (Dispatcher *)args;
void * model = dispatcher->model();
while (true) {
int ret = dispatcher->next(sentence);
if (ret < 0)
break;
result.clear();
segmentor_segment(model, sentence, result);
dispatcher->output(result);
}
return;
}
int main(int argc, char ** argv) {
if (argc < 1 || (0 == strcmp(argv[1], "-h"))) {
std::cerr << "Example: ./multi_cws_cmdline [model path] [lexicon file]" << std::endl;
std::cerr << std::endl;
std::cerr << "This program recieve input word sequence from stdin." << std::endl;
std::cerr << "One sentence per line." << 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;
}
int num_threads = thread::hardware_concurrency();
std::cerr << "TRACE: Model is loaded" << std::endl;
std::cerr << "TRACE: Running " << num_threads << " thread(s)" << std::endl;
Dispatcher * dispatcher = new Dispatcher( engine );
double tm = get_time();
list<thread *> thread_list;
for (int i = 0; i < num_threads; ++ i) {
thread * t = new thread( multithreaded_segment, (void *)dispatcher );
thread_list.push_back( t );
}
for (list<thread *>::iterator i = thread_list.begin();
i != thread_list.end(); ++ i) {
thread * t = *i;
t->join();
delete t;
}
tm = get_time() - tm;
std::cerr << "TRACE: consume "
<< tm
<< " seconds."
<< std::endl;
return 0;
}