-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordlist.cpp
More file actions
157 lines (138 loc) · 4.83 KB
/
wordlist.cpp
File metadata and controls
157 lines (138 loc) · 4.83 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
/*
* Author: Huayin Zhou
* Date: 01/28/2015
*/
#include "wordlist.h"
#include <unistd.h>
#include <QDebug>
#include <fstream>
#include <algorithm>
using std::vector;
using std::string;
WordList::WordList(QWidget *parent) : QListWidget(parent) {
mainWindow = (MainWindow *)(parent->parentWidget()->parentWidget()
->parentWidget()->parentWidget());
connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(mouseClickClearItems(QListWidgetItem *)));
trie = new DictionaryTrie();
// Read the dictionary into DictTrie
std::ifstream in;
in.open(mainWindow->getDictionaryPath().c_str());
if (!in.is_open()) {
mainWindow->dictNotLoaded();
} else {
Utils U;
U.load_dict(*trie, in);
in.close();
}
}
WordList::~WordList() {
delete trie;
}
// Select the next item in drop down menu. The selected text will be sent to
// input bar, but the original input can be retrieved later.
void WordList::selectNext() {
int currRow = currentRow();
if (currRow == count() - 1) {
currRow = -1;
} else {
currRow++;
}
setCurrentRow(currRow);
if (currentRow() != -1) {
mainWindow->textFieldSetText(currentItem()->text());
} else {
mainWindow->textFieldSetText(mainWindow->getTextFieldOriginalString());
}
}
// Select the prev item in drop down menu. The selected text will be sent to
// input bar, but the original input can be retrieved later.
void WordList::selectPrev() {
int currRow = currentRow();
if (currRow == -1) {
currRow = count() - 1;
} else {
currRow--;
}
setCurrentRow(currRow);
if (currentRow() != -1) {
mainWindow->textFieldSetText(currentItem()->text());
} else {
mainWindow->textFieldSetText(mainWindow->getTextFieldOriginalString());
}
}
// Populate the drop down menu by searching prediction of word in input bar
void WordList::setItems(const QString &newString) {
clear();
mainWindow->clearStatusBarText();
if (!newString.isEmpty()) {
vector<string> prefixString;
vector<string> searchString;
string originString = newString.toUtf8().constData();
string trailingSpaces = string();
// Get the trailing spaces
while (originString.length() > 0
&& originString.find_last_of(' ') == originString.length() - 1) {
originString.pop_back();
trailingSpaces += " ";
}
int spacePos = originString.length();
// Construct postfixes
for (int i = 0; i < MAX_POSTFIX_TO_SEARCH; i++) {
spacePos = originString.find_last_of(' ', spacePos - 1);
prefixString.push_back(originString.substr(0, spacePos + 1));
searchString.push_back(
originString.substr(spacePos + 1) + trailingSpaces);
if (spacePos == string::npos) break;
}
/*
for (int i = searchString.size() - 1; i >= 0; i--) {
qDebug() << "Prefix " << i << ": " << QString::fromUtf8(prefixString[i].c_str());
qDebug() << "Search " << i << ": " << QString::fromUtf8(searchString[i].c_str());
qDebug() << "Trail :" << trailingSpaces.length() << endl;
}
*/
// Search each postfix
mainWindow->setStatusBarText(tr("Searching..."));
vector<string> final;
for (int i = searchString.size() - 1; i >= 0; i--) {
vector<string> v =
trie->predictCompletions(searchString[i], MAX_DISPLAY);
for(vector<string>::iterator it = v.begin();
it != v.end(); ++it) {
if(std::find(final.begin(),final.end(),
prefixString[i]+(*it)) == final.end())
final.push_back(prefixString[i]+(*it));
if (final.size() >= MAX_DISPLAY) break;
}
if (final.size() >= MAX_DISPLAY) break;
}
for(vector<string>::iterator it = final.begin();
it != final.end(); ++it) {
addItem(QString::fromUtf8(it->c_str()));
}
mainWindow->setStatusBarText(tr("Finish Searching"), 2000);
}
// Resize drop down menu
if (count() > 0) {
setVisible(true);
resize(width(), rectForIndex(
indexFromItem(item(0))).height()*count() + 5);
} else {
resize(width(), 0);
}
}
// Clear the drop down menu BUT RETAIN the content in input bar
void WordList::clearItems() {
mainWindow->textFieldSaveCurrTextAsOriginal();
clear();
resize(width(), 0);
}
// Clear the drop down menu, replace the content in input bar with
// the clicked item
void WordList::mouseClickClearItems(QListWidgetItem * item) {
mainWindow->textFieldSetText(item->text());
mainWindow->textFieldSaveCurrTextAsOriginal();
clear();
resize(width(), 0);
}