-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseUtil.cpp
More file actions
275 lines (255 loc) · 8.7 KB
/
parseUtil.cpp
File metadata and controls
275 lines (255 loc) · 8.7 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "parseUtil.h"
using namespace std;
ParseUtil* ParseUtil::_pInstance = new ParseUtil();
//http://api.ltp-cloud.com/analysis/?api_key=X2O2n1y6WQtQHTrGklygCLkMUPHNagFE9edsYgRD&text=dsada&pattern=pos&format=json
//api_key = X2O2n1y6WQtQHTrGklygCLkMUPHNagFE9edsYgRD yzl350206931@126.com
//api_key = r2m491K77ANn9dqsdPFZpobLDxHS4rYRQpTn8t3C 350206931@qq.com
ParseUtil::ParseUtil(){
url = "http://api.ltp-cloud.com/analysis/?";
api_key = "r2m491K77ANn9dqsdPFZpobLDxHS4rYRQpTn8t3C";
format = "json";
pattern = "pos";
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"ParseUtil Init");
}
ParseUtil* ParseUtil::GetInstance(){
if(_pInstance == NULL)
_pInstance = new ParseUtil();
return _pInstance;
}
bool ParseUtil::MakeUrl(const string &text,string &urlwww){
urlwww += url;
urlwww += "api_key=" + api_key;
urlwww += "&text=" + text;
urlwww += "&pattern=" + pattern;
urlwww += "&format=" + format;
return true;
}
string ParseUtil::GetResponseByHttp(const string &text)
{
string urlwww;
MakeUrl(text,urlwww);
string response;
if(HttpUtil::GetInstance()->Get(urlwww,response))
return response;
return "";
}
bool ParseUtil::SubParseWord(const vector<Status> &statusList)
{
//用于进行词汇的解析
Json::Value root;
Json::Reader reader;
int record = 0;
string content;
for(unsigned int i=0;i<statusList.size();i++){
content += statusList[i].cont + ",";
record++;
if(record == 10 || i == statusList.size()-1)
{
cout<<"parse get http"<<endl;
content = GetResponseByHttp(content);
if(!reader.parse(content,root))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"reader parse fail");
cout<<"reader parse fail"<<endl;
record = 0;
root.clear();
content.clear();
continue;
}
if(root.type() != Json::arrayValue)
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"root type is not arrayValue");
cout<<"root type is not arrayValue"<<endl;
record = 0;
root.clear();
content.clear();
continue;
}
int L = root.size();
for(unsigned int l = 0;l<L;l++){
int M = root[l].size();
for(unsigned int m =0;m<M;m++){
int N = root[l][m].size();
for(unsigned int n =0;n<N;n++){
string cont = root[l][m][n]["cont"].asString();
string pos = root[l][m][n]["pos"].asString();
wordMap[cont] = pos;
}
}
}
root.clear();
record = 0;
content.clear();
}
}
return true;
}
bool ParseUtil::ParseWord()
{
int ret;
int wid = 0;
int record =0;
int totalStatus = 190000;
int interval = 1000;
vector<Status> statusV;
map<string,string>::iterator iter;
//获取微博的总个数
string query = "select count(*) from statusH";
if(DbUtil::GetInstance()->DbQueryCount(query,totalStatus)){
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"totalStatus:"<<totalStatus);
return false;
}
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"totalStatus:"<<totalStatus);
query = "select sid,userid,weibocontent from statusH limit ";
while(record <= totalStatus)
{
cout<<"record:"<<record<<endl;
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"record:"<<record);
string startPos = StrUtil::GetInstance()->Int2String(record);
string endPos = StrUtil::GetInstance()->Int2String(interval);
string query2 = query + startPos + ",";
query2 += endPos;
if(DbUtil::GetInstance()->DbQueryStatus(query2,statusV,1)){
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbQueryStatus fail");
return false;
}
//解析数据到wordMap中
SubParseWord(statusV);
statusV.clear();
record += interval;
//插入数据到数据库中
string query3 = "insert into word(wid,cont,pos) values(";
for(iter = wordMap.begin();iter != wordMap.end();iter++)
{
string query4 = query3 + StrUtil::GetInstance()->Int2String(wid) + ",'";
query4 += iter->first + "','";
query4 += iter->second + "')";
if(DbUtil::GetInstance()->DbInsert(query4)){
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbInsert fail");
cout<<"DbInsert fail"<<endl;
}
wid++;
}
wordMap.clear();
}
return true;
}
bool ParseUtil::DealWord()
{
string query = "select cont,wid,pos from word";
vector<Word> wordV;
if(DbUtil::GetInstance()->DbQueryWord(query,wordV))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbQueryWord fail");
return false;
}
query = "insert into wordH(wid,cont,pos) values(";
for(unsigned int i=0;i<wordV.size();i++)
{
string query2;
query2 += query + StrUtil::GetInstance()->Int2String(i) + ",'";
query2 += wordV[i].cont +"','";
query2 += wordV[i].pos + "')";
if(DbUtil::GetInstance()->DbInsert(query2))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbInsert fail");
return false;
}
cout<<"wid:"<<i<<endl;
}
return true;
}
bool ParseUtil::ParseStatusTag(){
vector<Status> statusV;
int ret;
string query = "select sid,userid,weibocontent,time from statusH";
Json::Value root;
Json::Reader reader;
string query2;
ret = DbUtil::GetInstance()->DbQueryStatus(query,statusV,1,1);
if(ret)
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"ret:"<<ret);
return false;
}
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"statusV size:"<<statusV.size());
for(unsigned int i = 0;i<statusV.size();i++){
long sid = statusV[i].sid;
string cont = GetResponseByHttp(statusV[i].cont);
if(!reader.parse(cont,root))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"reader parse fail");
root.clear();
continue;
}
if(root.type() != Json::arrayValue)
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"root type is not arrayValue");
root.clear();
continue;
}
int size = root[0U][0U].size();
string widList;
for(unsigned int j = 0;j<size;j++)
{
string keyWord = root[0U][0U][j]["cont"].asString();
int wid = CacheUtil::GetInstance()->FindIdByWord(keyWord);
cout<<"find"<<endl;
if(wid != -1)
widList += StrUtil::GetInstance()->Int2String(wid) + ",";
}
query2 = "insert into tagstatus(sid,widlist,time) values(";
query2 += StrUtil::GetInstance()->Int2String(sid) + ",'";
query2 += widList +"','";
query2 += statusV[i].datetime + "')";
if(DbUtil::GetInstance()->DbInsert(query2))
{
LOG4CPLUS_ERROR(LogUtil::GetInstance()->GetLogger()
,"DbInsert fail");
return false;
}
cout<<"status:"<<i<<endl;
root.clear();
}
return true;
}
void ParseUtil::Test()
{
LOG4CPLUS_INFO(LogUtil::GetInstance()->GetLogger()
,"Test Start");
//string query = "select sid,userid,weibocontent from status";
//ParseWord(query);
//string query = "select sid,userid,weibocontent,time from status";
//ParseStatusTag(query);
//ParseWord();
//DealWord();
ParseStatusTag();
/*
DbUtil::GetInstance()->DbQueryStatus(query,statusV,1);
string content;
for(unsigned int i=0;i<20;i++){
content += statusV[i].cont + ",";
}
cout<<"content:"<<content<<endl;
content = GetResponseByHttp(content);
cout<<"parse result:"<<content<<endl;
*/
}
ParseUtil::~ParseUtil(){
if(ParseUtil::_pInstance)
delete ParseUtil::_pInstance;
}