-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPredictor.h
177 lines (137 loc) · 3.3 KB
/
Predictor.h
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
#ifndef PREDICTOR_H
#define PREDICTOR_H
#include <fstream>
#include <sstream>
#include "Trie.h"
using std::string;
using std::vector;
using std::stringstream;
class Predictor
{
Trie *uniGrams, *biGrams;
unsigned int uniNoOfWords, uniTotalFreq, biNoOfWords, biTotalFreq;
unsigned int strToUint(string str);
void readTextCorpus();
string freqPredict(string word1, string word2);
bool tryLoadingTries();
public:
Predictor();
~Predictor();
string Predict(string word1, string word2);
};
Predictor::Predictor()
{
if (!tryLoadingTries())
{
readTextCorpus();
uniGrams->saveTrie("uniTrie.txt");
biGrams->saveTrie("biTrie.txt");
}
}
Predictor::~Predictor()
{
delete uniGrams;
delete biGrams;
}
void Predictor::readTextCorpus()
{
std::ifstream uniCorpusFile("uniCorpus.txt");
vector<ValidChar> words;
string word;
std::getline(uniCorpusFile, word);
uniCorpusFile >> word;
uniNoOfWords = strToUint(word);
uniCorpusFile >> word;
uniTotalFreq = strToUint(word);
for (int i = 0; i < uniNoOfWords; i++)
{
uniCorpusFile >> word;
unsigned int f = strToUint(word);
uniCorpusFile >> word;
words.push_back(ValidChar(' ', word, f));
}
uniGrams = new Trie(words);
std::ifstream biCorpusFile("biCorpus.txt");
words.clear();
string biWord;
std::getline(biCorpusFile, word);
biCorpusFile >> word;
biNoOfWords = strToUint(word);
biCorpusFile >> word;
biTotalFreq = strToUint(word);
for (int i = 0; i < biNoOfWords; i++)
{
biCorpusFile >> word;
unsigned int f = strToUint(word);
biCorpusFile >> word;
biCorpusFile >> biWord;
words.push_back(ValidChar(' ', word + " " + biWord, f));
}
biGrams = new Trie(words);
}
unsigned int Predictor::strToUint(string str)
{
stringstream ss(str);
unsigned int num;
ss >> num;
return num;
}
string Predictor::Predict(string word1, string word2)
{
ValidChar uniPrediction, biPrediction;
if (word1 == "")
{
uniPrediction = uniGrams->searchWord(word2);
return uniPrediction.getString();
}
else
{
uniPrediction = uniGrams->searchWord(word2);
biPrediction = biGrams->searchWord(word1 + " " + word2);
if (biPrediction.getString() == "")
return uniPrediction.getString();
string resultStr;
resultStr = biPrediction.getString().substr(word1.size() + 1, biPrediction.getString().size());
return resultStr;
}
}
string Predictor::freqPredict(string word1, string word2)
{
ValidChar uniPrediction, biPrediction;
uniPrediction = uniGrams->searchWord(word2);
biPrediction = biGrams->searchWord(word1 + " " + word2);
/*
if (biPrediction.getString()==" ")
return uniPrediction.getString();
else
return biPrediction.getString().substr(word1.size()+1,biPrediction.getString().size());
*/
if ((uniPrediction.getFreq() / (double)uniTotalFreq) > (biPrediction.getFreq() / (double)biTotalFreq))
return uniPrediction.getString();
else
return biPrediction.getString().substr(word1.size() + 1, biPrediction.getString().size());
}
bool Predictor::tryLoadingTries()
{
std::ifstream inputFile("uniTrie.txt");
if (inputFile.is_open())
{
delete uniGrams;
uniGrams = new Trie();
uniGrams->loadTrie(inputFile);
}
else
return false;
inputFile.close();
inputFile.open("biTrie.txt");
if (inputFile.is_open())
{
delete biGrams;
biGrams = new Trie();
biGrams->loadTrie(inputFile);
}
else
return false;
inputFile.close();
}
#endif