-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.cpp
145 lines (107 loc) · 2.82 KB
/
dict.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
/* Class that will contain dict class */
#include "dict.h"
Dict::Dict(int wordLength0) { //initialise Dict
wordLength = wordLength0;
size = 0;
for(int i = 0; i < wordLength; i++) {
displayWord += "_";
}
}//dict
void Dict::getWords() { //Read in words to start
string line; //var to get words
ifstream dict("dictionary"); //file to read in
for(int i = 0; i < 45394; i++) { //loop over all words
getline(dict, line);
if(line.length() == wordLength && !checkForRepeat(line)) { //No repeated letter and correct length
wrds.push_back(line);
size++;
}
}
}//getWords
bool Dict::checkForRepeat(string s) { //check for repeated letters
string temp = s; //temp var to hold word
for(int i = 0; i < wordLength - 1; i++) { //look for repeated letter
char c = s.at(i);
temp = s.substr(i + 1);
if(temp.find(c) != string::npos) {
return true;
}
}
return false;
}//checkForRepeat
void Dict::reduceWords(string s) { //reduce the amount of words left in the dict
int pos = findFreq(s); //find the positon it occurs the most, or not at all
it = wrds.begin(); //initailise iterator
if(pos == wordLength) { //if found in the most remove them words
for(int i = 0; it != wrds.end(); i++) {
string w = *it;
int t = w.find(s);
if(t != string::npos) {
it = wrds.erase(it);
size--;
}
else {
++it;
}
}
}//
else { //otherwise remove any word with that letter
for(int i = 0; it != wrds.end(); i++) {
string w = *it;
int t = w.find(s);
if(t != pos) {
it = wrds.erase(it);
size--;
}
else {
++it;
}
}//
reduceDisplay(s, pos); //now reduce the display word
}
}//reduceWords
int Dict::findFreq(string s) { //find frequency of char in each position
int temp = wordLength + 1;
int pos[temp]; //array to count the position
for(int i = 0; i < temp; i++) {
pos[i] = 0;
}
for(it = wrds.begin(); it != wrds.end(); ++it) { //check words for character position
string w = *it;
int i = w.find(s);
if(i != string::npos) {
pos[i]++; //increment relevant position
}
else {
pos[wordLength]++; //otherwise not there
}
}
int freq = largest(pos); //return largest positon
return freq;
}//findFreq
int Dict::largest(int nums[]) { //returns largest in array
int most = 0;
for(int i = 0; i < wordLength + 1; i++) {
if(nums[i] > most) {
most = i;
}
}
return most;
}//largest
int Dict::reduceDisplay(string c, int p) { //reduce the display word
displayWord.replace(p, 1, c);
}//reduceDisplay
bool Dict::checkDict(string s) { //check the dict for the letter
for(int i = 0; i < size; i++) {
if(wrds[i].find(s) != string::npos) {
return true;
}
}
return false;
}//checkDict
string Dict::sample() { //return a sample word
return wrds[0];
}//sample
void Dict::printWord() { //print the display word
cout << displayWord << endl;
}//printWord