-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
64 lines (54 loc) · 2.16 KB
/
main.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
#include <iostream>
#include <vector>
#include "ClassifyResult.h"
#include "BasicTextClassifier.h"
#include "LinearBigramChineseTextClassifier.h"
#include "pthread.h"
using namespace std;
using namespace thunlp;
#define THREADS_NUM 10
struct Parameter{
string text;
int topN;
BasicTextClassifier* classifier;
};
void *classifyText(void* args) {
Parameter* p = (Parameter*) args;
vector<ClassifyResult> results = p->classifier->classifyText(p->text, p->topN);
for (int i = 0; i < p->topN; ++i) {
printf("%d\t\t%lf\n", results[i].label_, results[i].probability_);
}
pthread_exit(nullptr);
}
BasicTextClassifier* initClassifier(const string& category, const string& model){
BasicTextClassifier* classifier = new BasicTextClassifier();
classifier->loadCategoryListFromFile(category);
classifier->setTextClassifier(new LinearBigramChineseTextClassifier(classifier->getCategorySize()));
classifier->getTextClassifier()->loadModel(model);
return classifier;
}
int main() {
//新建分类对象
BasicTextClassifier* classifier = initClassifier("/Users/Fan/Downloads/tencent_model_100W/category",
"/Users/Fan/Downloads/tencent_model_100W");
//使用分类器进行分类
string text = "再次回到世锦赛的赛场上,林丹终于变回了以前的那个超级丹.";
int topN = 3;
//单线程
// vector<ClassifyResult> results = classifier->classifyText(text, topN);
// for (int i = 0; i < topN; ++i) {
// printf("%d\t\t%lf\n", results[i].label_, results[i].probability_);
// }
//pthread多线程测试
pthread_t threads[THREADS_NUM];
int rc;
Parameter para[THREADS_NUM];
for (int i = 0; i < THREADS_NUM; ++i) {
printf("main() : creating thread, %d\n", i);
// BasicTextClassifier* tempclass = initClassifier("/Users/Fan/Downloads/tencent_model_100W/category",
// "/Users/Fan/Downloads/tencent_model_100W");
para[i] = {text, topN, classifier};
rc = pthread_create(&threads[i], NULL, classifyText, (void *)¶[i]);
}
pthread_exit(nullptr);
}