-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (152 loc) · 5.23 KB
/
main.cpp
File metadata and controls
175 lines (152 loc) · 5.23 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "WordTree.hpp"
#include "rlutil.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
std::shared_ptr<WordTree> readDictionary(std::string filename)
{
auto wordTree = std::make_shared<WordTree>();
std::ifstream inFile = std::ifstream(filename, std::ios::in);
while (!inFile.eof())
{
std::string word;
std::getline(inFile, word);
// Need to consume the carriage return character for some systems, if it exists
if (!word.empty() && word[word.size() - 1] == '\r')
{
word.erase(word.end() - 1);
}
// Keep only if everything is an alphabetic character -- Have to send isalpha an unsigned char or
// it will throw exception on negative values; e.g., characters with accent marks.
if (std::all_of(word.begin(), word.end(), [](unsigned char c)
{
return std::isalpha(c);
}))
{
std::transform(word.begin(), word.end(), word.begin(), [](char c)
{
return static_cast<char>(std::tolower(c));
});
wordTree->add(word);
}
}
return wordTree;
}
int main()
{
std::shared_ptr<WordTree> dictTree = readDictionary("dictionary.txt");
// Clear the screen.
rlutil::cls();
int xPos = 1;
std::string word;
bool done = false;
// This vector 'phrase' will contain what we have types so far. If there is a space, it is added to the phrase.
// In my implementation, I have spaces in the phrase, so if we are currently on a space, there is no given prediction.
std::vector<std::string> phrase;
phrase.push_back("");
while (!done)
{
rlutil::locate(xPos, 1);
int key = rlutil::getkey();
xPos = 1;
switch (key)
{
case rlutil::KEY_ESCAPE:
done = true;
break;
case rlutil::KEY_BACKSPACE:
if (phrase.size() > 1)
{
if (word == "")
{
// pop back of vector and set word to previous word in phrase
phrase.pop_back();
word = phrase.at(phrase.size() - 1);
}
else
{
// if current word is not blank, pop back end of word
word = phrase.at(phrase.size() - 1);
word.pop_back();
phrase.pop_back();
phrase.push_back(word);
}
}
else
{
if (word != "")
{
word = phrase.at(phrase.size() - 1);
word.pop_back();
phrase.pop_back();
phrase.push_back(word);
}
}
rlutil::locate(xPos, 1);
std::cout << "";
break;
case rlutil::KEY_SPACE:
// add blank word to vector
word = "";
phrase.push_back(word);
break;
default:
// add key to current word
phrase.pop_back();
word.push_back(static_cast<char>(key));
phrase.push_back(word);
break;
}
rlutil::locate(xPos, 1);
// print out the current phrase
for (std::string tempWord : phrase)
{
rlutil::locate(xPos, 1);
for (char letter : tempWord)
{
rlutil::setChar(letter);
xPos += 1;
rlutil::locate(xPos, 1);
}
rlutil::setChar(' ');
xPos += 1;
rlutil::locate(xPos, 1);
}
xPos -= 1;
// Initialize new vector for giving blank words
std::vector<std::string> tempWords;
unsigned int yPos = 3;
unsigned int tempX = 1;
int rows = rlutil::trows();
// Longest word I could find was 20 ish in length, so I have 25 spaces
for (int i = 0; i < rows - 4; i++)
{
tempWords.push_back(" ");
}
std::vector<std::string> words = dictTree->predict(word, static_cast<std::uint8_t>(rows - 4));
// Add predicted words to tempWords vector, and add spaces at end to delete 'extra' characters that would be left over
for (unsigned int j = 0; j < words.size(); j++)
{
tempWords.at(j) = words.at(j);
tempWords.at(j) += " ";
}
rlutil::locate(tempX, yPos);
std::cout << "--- prediction ---";
yPos += 1;
rlutil::locate(tempX, yPos);
// Print out the predicted words
for (std::string tempWord : tempWords)
{
rlutil::locate(tempX, yPos);
std::cout << tempWord;
tempX = 1;
rlutil::locate(tempX, yPos);
yPos += 1;
}
rlutil::locate(xPos, yPos);
std::cout.flush();
}
return 0;
}