-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellcorrect.cc
167 lines (147 loc) · 4.08 KB
/
spellcorrect.cc
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
///
/// @file spellcorrect.cc
/// @author lambert(Liumeng_lambert@163.com)
/// @date 2018-03-16 17:01:17
///
#include <iostream>
#include <functional>
#include "spellcorrect.hpp"
namespace spellCorrect{
WordQuery::WordQuery()
:_query_word(),
_max_query_len(100),
_index("dict/dict_en.txt")
{
}
WordQuery::~WordQuery() {
}
std::string WordQuery::execute(std::string query_word, Cache& cache) {
_query_word = query_word;
//find in cache
std::string ans = cache.query_cache(_query_word);
//not found in cache
if(ans.empty()) {
ans = query_index_table();
std::cout << query_word << " " << ans << std::endl;
cache.add_element(query_word, ans);
}
return ans;
}
std::string WordQuery::query_index_table() {
const std::map<std::string, std::set<int> > &m_index = _index.get_index();
std::set<int> i_set;
for(auto c : _query_word) {
//TODO:deal with input word
if(!isalpha(c))continue;
std::string s(1, c);
/*const,use at instead of []*/
for(auto i : m_index.at(s)) {
i_set.insert(i);
}
}
statistic(i_set);
return _result_que.top()._word;
}
/*caculate from reverse index*/
void WordQuery::statistic(std::set<int> & iset) {
const std::vector<std::pair<std::string, int> > & v_word = _index.get_wordlist();
while(!_result_que.empty()) {
_result_que.pop();
}
for(auto i : iset) {
struct MyResult r;
r._word = v_word[i].first;
r._freq = v_word[i].second;
r._dist = distance(r._word);
_result_que.push(r);
}
}
int WordQuery::distance(const std::string & rhs) {
int lr = rhs.size();
int lq = _query_word.size();
const size_t len = _max_query_len + 1;
int dis[len][len];
for(int i = 0; i < lr; ++i) {
dis[i][0] = i;
}
for(int j = 0; j < lq; ++j) {
dis[0][j] = j;
}
for(int i = 1; i < lr; ++i) {
for(int j = 1; j < lq; ++j) {
int flag = (rhs[i] == _query_word[j])? 0 : 2;
/*insert_cost*/
int in_cost = dis[i-1][j] + 1;
/*delete_cost*/
int de_cost = dis[i][j - 1] + 1;
/*replace_cost*/
int re_cost = dis[i - 1][j - 1] + flag;
dis[i][j] = (in_cost < de_cost)?
in_cost : de_cost;
dis[i][j] = (dis[i][j] < re_cost)?
dis[i][j] : re_cost;
}
}
return dis[lr - 1][lq - 1];
}
TimeThread::TimeThread(std::function<void()> cb)
:_is_running(false),
_cb(cb),
_timer(std::bind(&TimeThread::thread_func, this))
{
}
TimeThread::~TimeThread() {
_is_running = false;
_timer.join();
}
void TimeThread::start() {
_is_running = true;
}
bool TimeThread::is_running() {
return _is_running;
}
void TimeThread::thread_func() {
//while(_is_running) {
while(1) {
sleep(6);
_cb();
}
}
SpellCorrectServer::SpellCorrectServer(const std::string & config_file)
:_conf(std::string("config/config")),
_tcp_server(9009),
_threadpool(),
_query(),
_timer(std::bind(&ThreadPool::update_cache, &_threadpool))
{
_tcp_server.set_connection_callback(std::bind(&SpellCorrectServer::on_connection, this, std::placeholders::_1 ));
_tcp_server.set_message_callback(std::bind(&SpellCorrectServer::on_message, this, std::placeholders::_1 ));
_tcp_server.set_close_callback(std::bind(&SpellCorrectServer::on_close, this, std::placeholders::_1 ));
}
void SpellCorrectServer::start() {
/*
_tcp_server.set_connection_callback(on_connection);
_tcp_server.set_message_callback(on_message);
_tcp_server.set_close_callback(on_close);
*/
std::cout << "server start!" << std::endl;
_threadpool.start();
_tcp_server.start();
_timer.start();
}
void SpellCorrectServer::on_connection(TcpConnectionPtr conn) {
std::cout << "Ready to search!" << std::endl;
}
void SpellCorrectServer::on_message(TcpConnectionPtr conn) {
std::string query_word = conn->receive();
std::cout << "Start to search : " << query_word << std::endl;
_threadpool.add_task(std::bind(&SpellCorrectServer::query_response, this, query_word, conn, std::placeholders::_1));
}
void SpellCorrectServer::on_close(TcpConnectionPtr conn) {
std::cout << "Ready to close " << conn->to_string() << std::endl;
}
void SpellCorrectServer::query_response(std::string query_word, TcpConnectionPtr& conn, Cache &cache) {
std::string result = _query.execute(query_word, cache);
conn->send(result);
}
}