-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellcorrect.hpp
80 lines (73 loc) · 1.71 KB
/
spellcorrect.hpp
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
///
/// @file spellcorrect.hpp
/// @author lambert(Liumeng_lambert@163.com)
/// @date 2018-03-11 17:24:56
///
#ifndef _SPELL_CORRECT_FUNCTION_H_
#define _SPELL_CORRECT_FUNCTION_H_
#include <iostream>
#include <thread>
#include "configuration.hpp"
#include "tcpserver.hpp"
#include "threadPool.hpp"
#include "index.hpp"
namespace spellCorrect{
struct MyResult{
std::string _word;
//appear frequency
int _freq;
//distance between this word and the input word
int _dist;
bool operator < (const MyResult & rhs) const{
if(_dist == rhs._dist){
return _freq > rhs._freq;
}
return _dist > rhs._dist;
}
};
class WordQuery{
public:
WordQuery();
~WordQuery();
std::string execute(std::string query_word, Cache& cache);
private:
Index _index;
std::string _query_word;
const size_t _max_query_len;
std::priority_queue<MyResult,
std::vector<MyResult> > _result_que;
std::string query_index_table();
/*caculate from reverse index*/
void statistic(std::set<int> & iset);
/*get edit-distance*/
int distance(const std::string & rhs);
};
class TimeThread {
public:
TimeThread(std::function<void()> cb);
~TimeThread();
bool is_running();
void start();
private:
void thread_func();
bool _is_running;
std::function<void()> _cb;
std::thread _timer;
};
class SpellCorrectServer {
public:
SpellCorrectServer(const std::string & config_file);
void start();
void on_connection(TcpConnectionPtr conn);
void on_message(TcpConnectionPtr conn);
void on_close(TcpConnectionPtr conn);
private:
Configuration _conf;
TcpServer _tcp_server;
ThreadPool _threadpool;
WordQuery _query;
TimeThread _timer;
void query_response(std::string query_word, TcpConnectionPtr& conn, Cache &cache);
};
}
#endif