forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordfinder.hh
197 lines (155 loc) · 6.1 KB
/
wordfinder.hh
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
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __WORDFINDER_HH_INCLUDED__
#define __WORDFINDER_HH_INCLUDED__
#include <list>
#include <map>
#include <QObject>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include <QRunnable>
#include "dictionary.hh"
/// This component takes care of finding words. The search is asyncronous.
/// This means the GUI doesn't get blocked during the sometimes lenghtly
/// process of finding words.
class WordFinder: public QObject
{
Q_OBJECT
public:
typedef std::vector< std::pair< QString, bool > > SearchResults; // bool is a "was suggested" flag
private:
SearchResults searchResults;
QString searchErrorString;
bool searchResultsUncertain;
std::list< sptr< Dictionary::WordSearchRequest > > queuedRequests,
finishedRequests;
bool searchInProgress;
QTimer updateResultsTimer;
// Saved search params
bool searchQueued;
QString inputWord;
enum SearchType
{
PrefixMatch,
StemmedMatch,
ExpressionMatch
} searchType;
unsigned long requestedMaxResults;
Dictionary::Features requestedFeatures;
unsigned stemmedMinLength;
unsigned stemmedMaxSuffixVariation;
std::vector< sptr< Dictionary::Class > > const * inputDicts;
std::vector< gd::wstring > allWordWritings; // All writings of the inputWord
struct OneResult
{
gd::wstring word;
int rank;
bool wasSuggested;
};
// Maps lowercased string to the original one. This catches all duplicates
// without case sensitivity. Made as an array and a map indexing that array.
typedef std::list< OneResult > ResultsArray;
typedef std::map< gd::wstring, ResultsArray::iterator > ResultsIndex;
ResultsArray resultsArray;
ResultsIndex resultsIndex;
public:
WordFinder( QObject * parent );
~WordFinder();
/// Do the standard prefix-match search in the given list of dictionaries.
/// Some dictionaries might only support exact matches -- for them, only
/// the exact matches would be found. All search results are put into a single
/// list containing the exact matches first, then the prefix ones. Duplicate
/// matches from different dictionaries are merged together.
/// If a list of features is specified, the search will only be performed in
/// the dictionaries which possess all the features requested.
/// If there already was a prefixMatch operation underway, it gets cancelled
/// and the new one replaces it.
void prefixMatch( QString const &,
std::vector< sptr< Dictionary::Class > > const &,
unsigned long maxResults = 40,
Dictionary::Features = Dictionary::NoFeatures );
/// Do a stemmed-match search in the given list of dictionaries. All comments
/// from prefixMatch() generally apply as well.
void stemmedMatch( QString const &,
std::vector< sptr< Dictionary::Class > > const &,
unsigned minLength = 3,
unsigned maxSuffixVariation = 3,
unsigned long maxResults = 30,
Dictionary::Features = Dictionary::NoFeatures );
/// Do the expression-match search in the given list of dictionaries.
/// Function find exact matches for one of spelling suggestions.
void expressionMatch( QString const &,
std::vector< sptr< Dictionary::Class > > const &,
unsigned long maxResults = 40,
Dictionary::Features = Dictionary::NoFeatures );
/// Returns the vector containing search results from the last operation.
/// If it didn't finish yet, the result is not final and may be changing
/// over time.
SearchResults const & getResults() const
{ return searchResults; }
/// Returns a human-readable error string for the last finished request. Empty
/// string means it finished without any error.
QString const & getErrorString()
{ return searchErrorString; }
/// Returns true if the search was inconclusive -- that is, there may be more
/// results than the ones returned.
bool wasSearchUncertain() const
{ return searchResultsUncertain; }
/// Cancels any pending search operation, if any.
void cancel();
/// Cancels any pending search operation, if any, and makes sure no pending
/// requests exist, and hence no dictionaries are used anymore. Unlike
/// cancel(), this may take some time to finish.
void clear();
signals:
/// Indicates that the search has got some more results, and continues
/// searching.
void updated();
/// Indicates that the search has finished.
void finished();
private slots:
/// Called each time one of the requests gets finished
void requestFinished();
/// Called by updateResultsTimer to update searchResults and signal updated()
void updateResults();
private:
// Starts the previously queued search.
void startSearch();
// Cancels all searches. Useful to do before destroying them all, since they
// would cancel in parallel.
void cancelSearches();
/// Compares results based on their ranks
struct SortByRank
{
bool operator () ( OneResult const & first, OneResult const & second )
{
if ( first.rank < second.rank )
return true;
if ( first.rank > second.rank )
return false;
// Do any sort of collation here in the future. For now we just put the
// strings sorted lexicographically.
return first.word < second.word;
}
};
/// Compares results based on their ranks and lengths
struct SortByRankAndLength
{
bool operator () ( OneResult const & first, OneResult const & second )
{
if ( first.rank < second.rank )
return true;
if ( first.rank > second.rank )
return false;
if ( first.word.size() < second.word.size() )
return true;
if ( first.word.size() > second.word.size() )
return false;
// Do any sort of collation here in the future. For now we just put the
// strings sorted lexicographically.
return first.word < second.word;
}
};
};
#endif