-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTask.h
153 lines (125 loc) · 3.03 KB
/
MyTask.h
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
///
/// @file MyTask.h
/// @author sgzed([email protected])
/// @date 2018-03-27 15:17:36
///
#ifndef __WD_MYTASK_H__
#define __WD_MYTASK_H__
#include "EditDistance.h"
#include "MyConf.h"
#include "MemCache.h"
#include "MemCacheManager.h"
#include <muduo/net/TcpConnection.h>
#include "IndexProducer.h"
#include <muduo/base/CurrentThread.h>
#include <utility>
#include <string>
#include <set>
#include <queue>
#include <vector>
using std::vector;
using std::string;
using std::priority_queue;
using std::make_pair;
int str2int(const string& str);
struct MyResult
{
string _word;
size_t _iFreq;
size_t _iDist;
};
struct MyCompare
{
bool operator()(const MyResult& lhs,const MyResult& rhs)
{
if(lhs._iDist > rhs._iDist)
return true;
else if(lhs._iDist==rhs._iDist && lhs._iFreq<rhs._iFreq)
return true;
else if(lhs._iDist==rhs._iDist && lhs._iFreq==rhs._iFreq &&
lhs._word > rhs._word)
return true;
else
return false;
}
};
class MyTask
{
public:
MyTask(const string& query,MemCacheManager& cacheManager)
:_query(query)
,_cacheManager(cacheManager)
{
}
priority_queue<MyResult,vector<MyResult>,MyCompare> getPriorityQueue()
{
return _priqueue;
}
void process(const muduo::net::TcpConnectionPtr& conn)
{
cout << "subThread :" << muduo::CurrentThread::t_threadName << endl;
size_t idx = str2int(muduo::CurrentThread::t_threadName)-1;
MemCache& cache = _cacheManager[idx];
string s = cache.query(_query);
if(s!="")
conn->send(s);
else
getFromIndex(conn,cache);
}
void getFromIndex(const muduo::net::TcpConnectionPtr& conn,MemCache& cache)
{
// std::unordered_map<string,std::set<int>> mapIndex = IndexProducer::getInstance()->getIndex();
// vector<pair<string,int>> dict = IndexProducer::getInstance()->getDict();
trie indexTrie = IndexProducer::getInstance()->getTrie();
string ch;
vector<string> seach;
for(size_t idx = 0; idx < _query.size();)
{
size_t nBytes = nBytesCode(_query[idx]);
ch = _query.substr(idx,nBytes);
vector<string> tmp = indexTrie.get_str_pre(ch);
seach.insert(seach.end(),tmp.begin(),tmp.end());
idx+=nBytes;
}
for(auto it : seach)
{
cout << "query = " << _query << " " << "it = "<< it <<endl;
size_t distance = editDistance(_query,it);
//cout << "the pos in vector is " << it << " and distance is " << distance << endl;
if(distance <=3 )
{
MyResult result ;
result._word = it;
result._iFreq = indexTrie.search_str(it)->count;
result._iDist = distance;
_priqueue.push(result);
}
}
if(_priqueue.empty())
{
string msg = "no answer!\n" ;
conn->send(msg);
}
else
{
int cnt = 3 ;
string res;
while(!_priqueue.empty() && cnt>0)
{
res +=_priqueue.top()._word;
res += " ";
_priqueue.pop();
--cnt;
}
res += "\n";
cache.addCache(make_pair(_query,res));
cache.reNew();
conn->send(res);
}
}
private:
string _query;
MemCacheManager& _cacheManager;
priority_queue<MyResult,vector<MyResult>,MyCompare> _priqueue;
};
#endif