-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearner.cpp.old2
More file actions
224 lines (203 loc) · 5.76 KB
/
Copy pathLearner.cpp.old2
File metadata and controls
224 lines (203 loc) · 5.76 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
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
#include "Learner.h"
Learner::Learner(
const Learner::WordList& lexicon
, unsigned contextSize
, unsigned dimensionality)
: wordToWordIndexMap()
, nSphere(dimensionality)
, CONTEXT_SIZE(contextSize)
, DIMENSIONALITY(dimensionality)
, n(0)
{
if (lexicon.size() > this->nSphere.getMaxPoints()) {
throw "Lexicon size exceeds nSphere capacity";
}
if (this->CONTEXT_SIZE <= 0) {
throw "Context size not non-zero";
}
if (this->CONTEXT_SIZE % 2 != 0) {
throw "Context size not even";
}
for (auto i = lexicon.begin(); i != lexicon.end(); i++) {
this->wordToWordIndexMap.emplace(
*i
, this->nSphere.addRandomUnitPoint());
}
this->wordToWordIndexMap.emplace(
this->NIL_WORD
, this->nSphere.getNumPoints());
}
void Learner::decWeight(unsigned wIdx1, unsigned wIdx2, float dec)
{
if (dec <= std::numeric_limits<float>::epsilon()) {
return;
}
float weight = this->nSphere.getWeight(wIdx1, wIdx2);
weight *= pow(0.99, dec);
this->nSphere.setWeight(wIdx1, wIdx2, weight);
}
void Learner::learnFromPage(const Learner::Page& page)
{
this->exportnSphere();
auto wordIndexToContextsMap = Learner::WordIndexToContextsMap();
/* Compute contexts */
std::cout << "Learner::learnFromPage: Computing contexts..." << std::endl;
for (auto s = page.begin(); s != page.end(); ++s) { // s = sentence
unsigned i = 0; // position of `w` in sentence
for (auto w = s->begin(); w != s->end(); ++w, ++i) { // w = word
unsigned wordIndex = this->getWordIndex(*w);
Learner::Context c = this->getContext(*s, i++);
this->addContextToWordIndex(wordIndexToContextsMap, c, wordIndex);
}
}
/* Sort each list of contexts */
std::cout << "Learner::learnFromPage: Sorting contexts..." << std::endl;
for (auto i = wordIndexToContextsMap.begin();
i != wordIndexToContextsMap.end();
i++) {
std::sort(i->second.begin(), i->second.end());
}
/* update the mutual charges between points */
std::cout << "Learner::learnFromPage: Updating charges..." << std::endl;
for (auto i = wordIndexToContextsMap.begin();
i != wordIndexToContextsMap.end();
i++) {
for (auto j = std::next(i); j != wordIndexToContextsMap.end(); j++) {
unsigned wordIndex1 = i->first;
unsigned wordIndex2 = j->first;
Learner::Contexts contexts1 = i->second;
Learner::Contexts contexts2 = j->second;
float dec = this->computeDecrement(contexts1, contexts2);
this->decWeight(wordIndex1, wordIndex2, dec);
}
}
/* Improve the model */
std::cout << "Learner::learnFromPage: Solving n-sphere..." << std::endl;
//for (int i = 0; i < 20; i++)
this->nSphere.solve();
//std::cout << std::endl;
}
float Learner::computeDecrement(Contexts& C1s, Contexts& C2s)
{
int i = 0;
int j = 0;
float dec = 0.0;
while (i < C1s.size() && j < C2s.size()) {
if (C1s[i] < C2s[j]) {
i++;
} else if (C1s[i] > C2s[j]) {
j++;
} else {
int runLengthC1s = this->getRunLength(C1s, i);
int runLengthC2s = this->getRunLength(C2s, j);
dec += sqrt(runLengthC1s * runLengthC2s);
i += runLengthC1s;
j += runLengthC2s;
}
}
return dec;
}
int Learner::getRunLength(Contexts& Cs, int i)
{
int result = 1;
while (i + result < Cs.size()) {
if (Cs[i] == Cs[i + result]) {
result++;
} else {
break;
}
}
return result;
}
float Learner::computeDistance(Word w1, Word w2)
{
if (w1 == NIL_WORD) {
return this->nSphere.getDefaultWeight();
}
if (w2 == NIL_WORD) {
return this->nSphere.getDefaultWeight();
}
if (this->wordToWordIndexMap.find(w1) == this->wordToWordIndexMap.end()) {
return this->nSphere.getDefaultWeight();
}
if (this->wordToWordIndexMap.find(w2) == this->wordToWordIndexMap.end()) {
return this->nSphere.getDefaultWeight();
}
return this->nSphere.computeDistance(
this->getWordIndex(w1)
, this->getWordIndex(w2));
}
/*
* Computes the context of a word in a `sentence` positioned at `wordIndex`. The
* context is given in terms of the indices stored in `this->nSphere`.
*/
Learner::Context Learner::getContext(
const Sentence& sentence
, unsigned wordIndex)
{
auto context = Learner::Context(this->CONTEXT_SIZE);
/* Iterating through words in `sentence`... */
for (int i = (int)wordIndex - (int)this->CONTEXT_SIZE/2;
i <= (int)wordIndex + (int)this->CONTEXT_SIZE/2;
i++) {
if (i == wordIndex) {
continue;
}
if (i < 0 || i >= sentence.size()) {
context.emplace_back(this->getWordIndex(this->NIL_WORD));
} else {
context.emplace_back(this->getWordIndex(sentence[i]));
}
}
return context;
}
void Learner::addContextToWordIndex(
Learner::WordIndexToContextsMap& map
, const Learner::Context& c
, unsigned wIdx)
{
if (map.find(wIdx) == map.end()) {
map.emplace(wIdx, Learner::Contexts());
}
map[wIdx].emplace_back(c);
}
unsigned Learner::getWordIndex(const std::string& word)
{
if (this->wordToWordIndexMap.find(word) == this->wordToWordIndexMap.end()) {
return this->wordToWordIndexMap[this->NIL_WORD];
} else {
return this->wordToWordIndexMap[word];
}
}
Learner::Word Learner::computeNearestWord(Word w)
{
if (w == NIL_WORD) {
return NIL_WORD;
}
if (this->wordToWordIndexMap.find(w) == this->wordToWordIndexMap.end()) {
return NIL_WORD;
}
unsigned wordIndex = this->nSphere.computeNearestPoint(
this->getWordIndex(w));
/* Find word given its `wordIndex`. O(n) operation in wordToWordIndexMap's
* length. */
for (auto i = this->wordToWordIndexMap.begin();
i != this->wordToWordIndexMap.end();
++i) {
if (i->second == wordIndex) {
return i->first;
}
}
/* shouldn't happen */
return "";
}
void Learner::exportnSphere() {
std::ofstream pyDict;
std::stringstream filename;
filename << "/tmp/points/dump-";
filename << std::setw(4) << std::setfill('0') << this->n++;
filename << ".py";
pyDict.open(filename.str());
pyDict << this->nSphere.pointsToPyDict();
pyDict.close();
}