This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mykytea.cpp
173 lines (142 loc) · 5.16 KB
/
mykytea.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// mykytea.cpp
#include <iostream>
#include "mykytea.hpp"
const int MAX_LEN = 256;
int split_argv(char* input, const char* configs[]){
int len;
char *cp;
const char *delim = " ";
cp = input;
for(len = 0; len < MAX_LEN; len++){
if((configs[len] = strtok(cp, delim)) == NULL )
break;
cp = NULL;
}
return len;
}
Mykytea::Mykytea(char* str)
{
const char* configs[MAX_LEN];
int len = split_argv(str, configs);
config = new KyteaConfig();
config->parseRunCommandLine(len, configs);
kytea = new Kytea(config);
kytea->readModel(config->getModelFile().c_str());
util = kytea->getStringUtil();
}
Mykytea::~Mykytea()
{
if(kytea != NULL) delete kytea;
// chanted the order, so that there is no double-freeing problem;
// it should not, but given the JNI and C++ ownership, this could happen.
if(config) delete config;
if(util) delete util;
}
/** the inference local to each thread */
vector<string>* Mykytea::getWS(string str){
vector<string>* vec = new vector<string>;
KyteaChar spaceChar = util->mapChar(" ");
KyteaString ks = util->mapString(str), buff(ks.length());
int len = ks.length();
KyteaSentence * pSentence = new KyteaSentence();
int charLen = 0;
for(int j = 0; j < len; j++) {
int bpos = 0;
for( ; j < len && ks[j] != spaceChar; j++)
buff[bpos++] = ks[j];
if(bpos == 0) {
if(ks[j] == spaceChar)
continue;
}
KyteaString word_str = buff.substr(0,bpos);
KyteaWord word(word_str, util->normalize(word_str));
charLen += bpos;
pSentence->words.push_back(word);
}
pSentence->surface = KyteaString(charLen);
pSentence->norm = KyteaString(charLen);
unsigned pos = 0;
for(KyteaSentence::Words::const_iterator tit = pSentence->words.begin(); tit != pSentence->words.end(); tit++) {
pSentence->surface.splice(tit->surface, pos);
pSentence->norm.splice(tit->norm, pos);
unsigned nextPos = pos + tit->surface.length() - 1;
while(pos++ < nextPos)
pSentence->wsConfs.push_back(0.0);
pSentence->wsConfs.push_back(100.0);
}
if(pSentence->wsConfs.size() > 0)
pSentence->wsConfs.pop_back();
///compute word segmentation, not read only
kytea->calculateWS(*pSentence);
const KyteaSentence::Words & words = pSentence->words;
for(int i = 0; i < (int) words.size(); i++) {
(*vec).push_back(util->showString(words[i].surface));
}
if(pSentence) delete pSentence;
return vec;
}
vector<Tags>* Mykytea::getTags(string str){
vector<Tags>* ret_words = new vector<Tags>;
KyteaString surface_string = util->mapString(str);
KyteaSentence sentence(surface_string, util->normalize(surface_string));
kytea->calculateWS(sentence);
for(int i = 0; i < config->getNumTags(); i++)
kytea->calculateTags(sentence,i);
const KyteaSentence::Words & words = sentence.words;
for(int i = 0; i < (int)words.size(); i++) {
tags vec_tag;
for(int j = 0; j < (int)words[i].tags.size(); j++) {
vector< pair<string, double> > vec_tmp;
for(int k = 0; k < 1; k++) {
vec_tmp.push_back( make_pair(util->showString(words[i].tags[j][k].first), words[i].tags[j][k].second) );
}
vec_tag.push_back( vec_tmp );
}
struct Tags t = { util->showString(words[i].surface), vec_tag };
(*ret_words).push_back( t );
}
return ret_words;
}
vector<Tags>* Mykytea::getAllTags(string str){
vector<Tags>* ret_words = new vector<Tags>;
KyteaString surface_string = util->mapString(str);
KyteaSentence sentence(surface_string, util->normalize(surface_string));
kytea->calculateWS(sentence);
for(int i = 0; i < config->getNumTags(); i++)
kytea->calculateTags(sentence,i);
const KyteaSentence::Words & words = sentence.words;
for(int i = 0; i < (int)words.size(); i++) {
tags vec_tag;
for(int j = 0; j < (int)words[i].tags.size(); j++) {
vector< pair<string, double> > vec_tmp;
for(int k = 0; k < (int)words[i].tags[j].size(); k++) {
vec_tmp.push_back( make_pair(util->showString(words[i].tags[j][k].first), words[i].tags[j][k].second) );
}
vec_tag.push_back( vec_tmp );
}
struct Tags t = { util->showString(words[i].surface), vec_tag };
(*ret_words).push_back( t );
}
return ret_words;
}
string Mykytea::getTagsToString(string str)
{
KyteaString surface_string = util->mapString(str);
KyteaSentence sentence(surface_string, util->normalize(surface_string));
kytea->calculateWS(sentence);
for(int i = 0; i < config->getNumTags(); i++)
kytea->calculateTags(sentence,i);
const KyteaSentence::Words & words = sentence.words;
string ret_str;
for(int i = 0; i < (int)words.size(); i++) {
ret_str += util->showString(words[i].surface);
for(int j = 0; j < (int)words[i].tags.size(); j++) {
for(int k = 0; k < 1; k++) {
ret_str += "/";
ret_str += util->showString(words[i].tags[j][k].first);
}
}
ret_str += " ";
}
return ret_str;
}