-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cpp
261 lines (238 loc) · 7.91 KB
/
demo.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// main.cpp - A Command-Line based demo using ncurses / curses
// OpenScribe
//
// Created by Prateek R Patil on 5/26/15.
// Copyright (c) 2015 Prateek R Patil. All rights reserved.
//
#include <iostream>
#include <deque>
#include <iomanip>
#include <string>
#include <fstream>
#include <regex>
#include <sstream>
#include <memory>
#include <bitset>
#include <thread>
#include <ncurses.h>
#include <map>
using namespace std;
class CharacterNode {
protected:
shared_ptr<map<char, shared_ptr<CharacterNode>>> nextCharacters = nullptr;
CharacterNode* parent = nullptr;
unsigned int count = 0;
char currentPath = 0;
public:
CharacterNode(){
}
CharacterNode(int count){
this->count = count;
}
int getCount(){
return this->count;
}
void setCount(int count){
this->count = count;
}
map<char, shared_ptr<CharacterNode>>* getNextCharacters(){
return this->nextCharacters.get();
}
CharacterNode* readTo(string path){
CharacterNode* position = this;
for (unsigned int i = 0; i < path.length(); i++) {
if (position->getNextCharacters() == nullptr) {
position->nextCharacters = shared_ptr<map<char, shared_ptr<CharacterNode>>>(new map<char, shared_ptr<CharacterNode>>);
}
if (position->getNextCharacters()->count(path[i]) == 0) {
position->getNextCharacters()->insert(make_pair(path[i], nullptr));
}
if (position->getNextCharacters()->at(path[i]).get() == nullptr) {
position->getNextCharacters()->at(path[i]) = shared_ptr<CharacterNode>(new CharacterNode);
}
position->getNextCharacters()->at(path[i])->parent = position;
position = position->getNextCharacters()->at(path[i]).get();
position->currentPath = path[i];
}
return position;
}
void readInWord(string word){
readTo(word)->count++;
}
deque<CharacterNode*> getSearchResults(){
deque<CharacterNode*> nodes;
if(this->nextCharacters.get() != nullptr){
for (auto& cn : *this->nextCharacters) {
if (cn.second != nullptr) {
if (cn.second->count > 0) {
nodes.push_back(cn.second.get());
}
if (cn.second->nextCharacters.get() != nullptr) {
deque<CharacterNode*> childNodes = cn.second->getSearchResults();
while (!childNodes.empty()) {
nodes.push_back(childNodes.front());
childNodes.pop_front();
}
}
}
}
}
return nodes;
}
deque<CharacterNode*> getSearchResultsBF(){
deque<CharacterNode*> nodes;
if(this->nextCharacters.get() != nullptr){
deque<CharacterNode*> toDive;
for (auto& cn : *this->nextCharacters) {
if (cn.second != nullptr) {
if (cn.second->count > 0) {
nodes.push_back(cn.second.get());
}
if (cn.second->nextCharacters.get() != nullptr) {
toDive.push_back(cn.second.get());
}
}
}
for (auto cn : toDive) {
if (cn->nextCharacters.get() != nullptr) {
if (cn->nextCharacters.get() != nullptr) {
deque<CharacterNode*> childNodes = cn->getSearchResultsBF();
while (!childNodes.empty()) {
nodes.push_back(childNodes.front());
childNodes.pop_front();
}
}
}
}
}
return nodes;
}
deque<CharacterNode*> getSearchResultsFrom(string rootPath){
return this->readTo(rootPath)->getSearchResults();
}
deque<CharacterNode*> getSearchResultsFromBF(string rootPath){
return this->readTo(rootPath)->getSearchResultsBF();
}
string print(){
string r = &this->currentPath;
if (this->parent != nullptr) {
r = this->parent->print() + r;
}
return r;
}
friend ostream& operator << (ostream& o, CharacterNode& cn){
o << cn.print() << " : " << cn.count;
return o;
}
static CharacterNode MakeNodeFromFile(string filepath){
CharacterNode newNode;
ifstream file;
file.open(filepath);
string line;
while (getline(file, line)) {
newNode.readInWord(line);
}
file.close();
return newNode;
}
static void SortByFrequency(deque<CharacterNode*>& results){
bool isSorted = false;
while (!isSorted) {
int sCount = 0;
for (int i = 1; i < results.size(); i++) {
if (results.at(i - 1)->count < results.at(i)->count) {
sCount++;
CharacterNode* temp = results.at(i);
results.at(i) = results.at(i - 1);
results.at(i - 1) = temp;
}
}
if (sCount == 0) {
isSorted = true;
}
}
}
static void SaveToFile(CharacterNode* node, string filePath){
ofstream outputFile;
outputFile.open(filePath);
deque<CharacterNode*> results = node->getSearchResultsBF();
while (!results.empty()) {
int tempCount = results.front()->count;
while (tempCount > 0) {
outputFile << results.front()->print() << endl;
tempCount--;
}
results.pop_front();
}
outputFile.close();
}
};
class DictionaryManager {
protected:
string folderPath;
CharacterNode defaultDictionary;
CharacterNode userDictionary;
public:
DictionaryManager(string folderPath){
this->folderPath = folderPath;
}
};
int main() {
CharacterNode main = CharacterNode::MakeNodeFromFile("/Users/PRP/Documents/Work/OpenScribe/superdictionary.txt");
initscr();
raw();
refresh();
string response = "";
string actualText = "";
deque<CharacterNode*> results;
while (response != "exit") {
printw((actualText + "|\n").c_str());
if (response.length() > 1) {
//printw("Searching...\n");
results = main.getSearchResultsFromBF(response);
//printw("done.\nSorting...\n");
CharacterNode::SortByFrequency(results);
//printw("done.\n");
} else {
results.clear();
}
if (!results.empty()) {
for (int i = 0; i < results.size() && i < 8; i++) {
printw((results.at(i)->print() + "\n").c_str());
}
}
char tempChar = getch();
if (tempChar != '\t') {
if (tempChar != 127 && tempChar != 8) {
actualText += tempChar;
if (isalnum(tempChar)) {
response += tempChar;
} else {
main.readInWord(response);
response.clear();
}
} else {
if (!actualText.empty()) {
actualText.pop_back();
}
if (!response.empty()) {
response.pop_back();
}
}
} else {
if (!results.empty()) {
string wordToFill = results.front()->print();
while (isalnum(actualText.back())) {
actualText.pop_back();
}
actualText += wordToFill;
main.readInWord(wordToFill);
}
}
clear();
}
endwin();
exit(EXIT_SUCCESS);
return 0;
}