forked from JulianMH/NounPhraseJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nounphrasejs.js
executable file
·283 lines (233 loc) · 13 KB
/
nounphrasejs.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"use strict";
define(['convnet-min'],
function(){
// Constants for parsing the noun phrase labels
var NOUN_PHRASE_BEGIN = 1;
var NOUN_PHRASE_INSIDE = 2;
var NOUN_PHRASE_NONE = 0;
// Every word in the dictionary is prefixed with a few letters, to make sure there are no collisions between property names.
var WORD_PREFIX = "abc";
// Helper function to get the contents of a text file.
function readTextFile(file, doneReading) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
var allText = rawFile.responseText;
doneReading(allText);
}
}
};
rawFile.send(null);
};
function Word(index, actualWord, isCapitalised, nounPhrase) {
this.index = index;
this.actualWord = actualWord;
this.isCapitalised = isCapitalised;
this.nounPhrase = nounPhrase;
};
Word.prototype.toString = function () {
return "Word: " + this.index + ", isCapitalised: " + this.isCapitalised + ", nounPhrase: " + this.nounPhrase;
};
function Dictionary(wordFeatureCount, rareWordMaxCount, words) {
this.wordFeatureCount = (typeof wordFeatureCount === 'undefined') ? 50 : wordFeatureCount;
this.rareWordMaxCount = (typeof rareWordMaxCount === 'undefined') ? 1 : rareWordMaxCount;
this.wordToIndices = new Object();
this.wordIndicesToVols = new Array();
this.wordCounts = new Array();
this.rareWordIndex = 0;
this.rareVol = new convnetjs.Vol(this.wordFeatureCount, 1, 1);
this.paddingVol = new convnetjs.Vol(this.wordFeatureCount, 1, 1);
this.wordIndicesToVols[this.rareWordIndex] = this.rareVol;
var thisDictionary = this;
if(typeof words !== 'undefined')
words.forEach(function (word, wordIndex) {
var index = wordIndex + 1;
var dictionaryWord = WORD_PREFIX + word.toLowerCase();
thisDictionary.wordToIndices[dictionaryWord] = index;
thisDictionary.wordIndicesToVols[index] = new convnetjs.Vol(thisDictionary.wordFeatureCount, 1, 1);
thisDictionary.wordCounts[index] = 0;
});
}
// Helper function to restore dictionary functionality after parsing it from JSON.
function reviveDictionary(jsonObject) {
jsonObject.paddingVol = reviveVol(jsonObject.paddingVol);
jsonObject.rareVol = reviveVol(jsonObject.rareVol);
for(var i = 0; i < jsonObject.wordIndicesToVols.length; ++i)
jsonObject.wordIndicesToVols[i] = reviveVol(jsonObject.wordIndicesToVols[i]);
return jsonObject;
}
// Helper function to restore convnetjs.Vol functionality after parsing it from JSON.
function reviveVol(jsonObject) {
var vol = new convnetjs.Vol(jsonObject.sx, jsonObject.sy, jsonObject.depth);
vol.w = jsonObject.w;
vol.dw = jsonObject.dw;
return vol;
}
// Reads a TextCorpus from a ConLL2000 formatted file.
function parseTextCorpus(text, dictionary, allowModifyDictionary) {
var lines = text.split("\n");
var sentences = new Array();
var currentSentence = new Array();
var currentNewWordIndex = dictionary.wordIndicesToVols.length;
var wordCount = 0;
lines.forEach(function (line, i, a) {
if (line === ""|| line === "\r") // If the sentence is over, start new sentence
{
if(currentSentence.length > 0)
{
sentences[sentences.length] = currentSentence;
currentSentence = new Array();
}
}
else {
var data = line.replace("\r", "").split(" ");
var word = WORD_PREFIX + data[0].toLowerCase(); //prefix every word with abc to make sure no keywords/ exisiting properties are used
// determine the word index
var wordIndex = currentNewWordIndex;
if (dictionary.wordToIndices.hasOwnProperty(word)) {
wordIndex = dictionary.wordToIndices[word];
if (allowModifyDictionary) {
dictionary.wordCounts[wordIndex] += 1;
if (dictionary.wordCounts[wordIndex] >= dictionary.rareWordMaxCount)
dictionary.wordIndicesToVols[wordIndex] = new convnetjs.Vol(dictionary.wordFeatureCount, 1, 1);
}
}
else if (allowModifyDictionary) {
dictionary.wordToIndices[word] = wordIndex;
dictionary.wordIndicesToVols[wordIndex] = dictionary.rareVol;
dictionary.wordCounts[wordIndex] = 1;
currentNewWordIndex++;
} else {
wordIndex = dictionary.rareWordIndex;
}
// determine the noun phrase info
var wordNounPhrase = NOUN_PHRASE_NONE;
if ((data[2] === "B-NP") || (data[2] === "B-TI"))
wordNounPhrase = NOUN_PHRASE_BEGIN;
else if ((data[2] === "I-NP") || (data[2] === "I-TI"))
wordNounPhrase = NOUN_PHRASE_INSIDE;
// determine if first char is upper case
var firstChar = data[0].charAt(0);
var isCapitalised = (firstChar === firstChar.toUpperCase());
// add to current sentence
currentSentence[currentSentence.length] = new Word(wordIndex, data[0], isCapitalised, wordNounPhrase);
++wordCount;
}
});
if (sentences[sentences.length - 1].length === 0)
sentences.pop();
return new TextCorpus(sentences, dictionary, wordCount);
}
function TextCorpus(sentences, dictionary, wordCount) {
this.sentences = sentences;
this.dictionary = dictionary;
this.wordCount = wordCount;
};
function NetworkConfiguration(network, trainer, getFeatureVector, trainFeatureVectorFromTrainingSample, dictionary, options) {
this.network = network;
this.trainer = trainer;
this.options = options;
this.getFeatureVector = getFeatureVector;
this.trainFeatureVectorFromTrainingSample = trainFeatureVectorFromTrainingSample;
this.dictionary = dictionary;
};
// Train the neural network using the given dataset.
NetworkConfiguration.prototype.train = function(trainCorpus, iterations, progressFunction) {
var currentTrainStartTime = new Date().getTime();
for (var i = 0; i < iterations; ++i) {
var sentenceIndex = Math.floor(Math.random() * trainCorpus.sentences.length);
var sentence = trainCorpus.sentences[sentenceIndex];
var wordIndex = Math.floor(Math.random() * sentence.length);
var word = sentence[wordIndex];
var trainData = this.getFeatureVector(trainCorpus, sentenceIndex, wordIndex);
var stats = this.trainer.train(trainData, word.nounPhrase);
this.trainFeatureVectorFromTrainingSample(trainCorpus, sentenceIndex, wordIndex, trainData);
if (typeof progressFunction !== 'undefined')
{
var newTrainStartTime = new Date().getTime();
progressFunction(i, stats, newTrainStartTime- currentTrainStartTime);
currentTrainStartTime = newTrainStartTime;
}
}
};
// Test the neural network using the given dataset.
NetworkConfiguration.prototype.test = function(testCorpus, progressFunction) {
var totalCount = 0;
var correctLabels = 0;
var currentTestStartTime = new Date().getTime();
var networkConfiguration = this;
testCorpus.sentences.forEach(function (sentence, sentenceIndex, sentences) {
sentence.forEach(function (word, wordIndex, s) {
var result = networkConfiguration.network.forward(
networkConfiguration.getFeatureVector(testCorpus, sentenceIndex, wordIndex));
var guess = 0;
var currentMaximum = result.w[0];
for (var index = 1; index < result.w.length; ++index) {
if (result.w[index] > currentMaximum) {
guess = index;
currentMaximum = result.w[index];
}
};
if (guess === word.nounPhrase)
++correctLabels;
++totalCount;
if (!(typeof progressFunction === 'undefined'))
{
var newTestStartTime = new Date().getTime();
progressFunction(totalCount, correctLabels, guess, word.nounPhrase, result.w, newTestStartTime - currentTestStartTime);
currentTestStartTime = newTestStartTime;
}
});
});
return correctLabels;
};
// Classify the words of a given sentence with the neural networks.
NetworkConfiguration.prototype.classifySentence = function(sentenceWordList, outputForWord) {
var words = new Array();
var networkConfiguration = this;
sentenceWordList.forEach(function (word) {
var dictionaryWord = WORD_PREFIX + word.toLowerCase(); //prefix every word with abc to make sure no keywords/ exisiting properties are used
var wordIndex = networkConfiguration.dictionary.rareWordIndex;
if (networkConfiguration.dictionary.wordToIndices.hasOwnProperty(dictionaryWord)) {
wordIndex = networkConfiguration.dictionary.wordToIndices[dictionaryWord];
}
var firstChar = word.charAt(0);
var isCapitalised = (firstChar === firstChar.toUpperCase());
words.push(new Word(wordIndex, word, isCapitalised, NOUN_PHRASE_BEGIN));
});
var corpus = new TextCorpus([words], networkConfiguration.dictionary, sentenceWordList.length);
words.forEach(function (word, wordIndex) {
var result = networkConfiguration.network.forward(
networkConfiguration.getFeatureVector(corpus, 0, wordIndex));
var guess = 0;
var currentMaximum = result.w[0];
for (var index = 1; index < 3; ++index) {
if (result.w[index] > currentMaximum) {
guess = index;
currentMaximum = result.w[index];
}
};
outputForWord(sentenceWordList[wordIndex], wordIndex, guess, result.w);
});
};
// Convert a whole NetworkConfiguration to JSON.
NetworkConfiguration.prototype.toJSON = function() {
return JSON.stringify({dictionary: this.dictionary, netJSON: this.network.toJSON(), options: this.options});
};
return {
NetworkConfiguration: NetworkConfiguration,
TextCorpus: TextCorpus,
Dictionary: Dictionary,
reviveDictionary: reviveDictionary,
readTextFile: readTextFile,
parseTextCorpus: parseTextCorpus,
Word: Word,
NOUN_PHRASE_BEGIN: NOUN_PHRASE_BEGIN,
NOUN_PHRASE_INSIDE: NOUN_PHRASE_INSIDE,
NOUN_PHRASE_NONE: NOUN_PHRASE_NONE,
WORD_PREFIX: WORD_PREFIX,
};
}
);