-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsummaryBot.js
193 lines (173 loc) · 5.94 KB
/
summaryBot.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
//SUMMARY BOT
//CAMDEN KO
//use summaryBot.
/*eslint-env node*/
/*eslint-env es6*/
//constructor for a summaryBot
//filter out common words?
const Vertex = require('./summaryBotVertex.js')
const stopWords = require('./stopWords.js')
function summaryBot() {
this.verticies = []
this.dampeningFactor = .85
this.numVerticies = 0
this.originalSentences = []
this.filteredSentences = []
this.errorThreshold = .01
this.unconnectedVerticies = new Set()
this.infoOnLastRun = {}
}
summaryBot.prototype.summary = function () {
if (Object.keys(this.infoOnLastRun).length) {
let origNumWords = this.originalSentences.join(' ').split(' ').length
return (`
Original Number of Lines: ${this.originalSentences.length}
Original Number of Words: ${origNumWords}
Condensed Number of Lines: ${this.infoOnLastRun.numLines}
Condensed Number of Words: ${this.infoOnLastRun.numWords}
Words kept: ${Math.round(100 * this.infoOnLastRun.numWords / origNumWords * 100) / 100}%
Error: ${Math.round(10000 * this.infoOnLastRun.err) / 100}%
`)
}
}
summaryBot.prototype.run = function (text, numReturnSentences, testStatistics = false) {
if (typeof (text) !== 'string') {
throw new TypeError('ensure that you pass valild values into summaryBot.prototype.run')
}
this._proccessString(text)
this._initialize()
let lastV0
let curV0
let errorLevel = 10
while (errorLevel > this.errorThreshold) {
this._updateAllVertexWeights()
if (!curV0) {
curV0 = this.verticies[0].weight
} else {
lastV0 = curV0
curV0 = this.verticies[0].weight
errorLevel = Math.abs(curV0 - lastV0)
}
}
if(!numReturnSentences) {
numReturnSentences = this._findBestNumSentences()
}
let output = this.getTopSentences(numReturnSentences)
this.infoOnLastRun = {
err: errorLevel,
numLines: numReturnSentences,
numWords: output.split(' ').length
}
if (testStatistics) {
output += this.summary()
}
return output
}
summaryBot.prototype._findBestNumSentences = function() {
if(this.verticies.length <= 3) {
return this.verticies.length
}
return Math.round(1.3 * Math.log(this.originalSentences.length))
}
//returns the error between last run
summaryBot.prototype.getTopSentences = function (numSentences) {
let out
this._sortVerticiesByWeight()
let topSentencesArr = []
if (numSentences > this.numVerticies) {
return this.originalSentences.join('. ')
} else {
for (let sentenceOut = 0; sentenceOut < numSentences; sentenceOut++) {
topSentencesArr.push(this.verticies[sentenceOut])
}
topSentencesArr.sort(function (a, b) {
if (a.name < b.name) return -1
return 1
})
out = topSentencesArr.map(function (vert) {
return this.originalSentences[vert.name]
}.bind(this))
}
if (topSentencesArr.some(vert => vert.name === this.originalSentences.length - 1)) {
return out.join('. ')
}
return out.join('. ') + '.'
}
//will properly create graph
summaryBot.prototype._proccessString = function (text) {
this.originalSentences = text.split(/[.|?|!] /)
for (let sentence = 0; sentence < this.originalSentences.length; sentence++) {
this.filteredSentences[sentence] = this.originalSentences[sentence].replace(/[^a-zA-Z0-9 \']/g, " ")
}
this.filteredSentences = this.filteredSentences.filter(function (content) {
return content != '';
})
for (let sentence = 0; sentence < this.filteredSentences.length; sentence++) {
this._addVertex(this.filteredSentences[sentence].toLowerCase().split(' ').filter((word) => word && word.length !== 1 && !stopWords.has(word)))
}
}
//adds vertex
summaryBot.prototype._addVertex = function (wordArr) {
this.verticies.push(new Vertex(this.numVerticies++, wordArr))
}
//sets the similarity edge weights for each node
summaryBot.prototype._initialize = function () {
//update edge weight
let similarity
for (let vertexNum = 0; vertexNum < this.verticies.length - 1; vertexNum++) {
for (let innerVertex = vertexNum + 1; innerVertex < this.verticies.length; innerVertex++) {
similarity = this._findSimilarity(this.verticies[vertexNum], this.verticies[innerVertex])
this.verticies[vertexNum].connection(innerVertex, similarity)
this.verticies[innerVertex].connection(vertexNum, similarity)
}
}
}
summaryBot.prototype._updateAllVertexWeights = function () {
let totalEdgeWeights = this._getEdgeTotals()
var tempSum = 0
for (let vertexNum = 0; vertexNum < this.numVerticies; vertexNum++) {
for (let otherVertex = 0; otherVertex < this.numVerticies; otherVertex++) {
if (otherVertex !== vertexNum && !this.unconnectedVerticies.has(otherVertex)) {
tempSum += this.verticies[vertexNum].edge[otherVertex] / totalEdgeWeights[otherVertex] * this.verticies[otherVertex].weight
}
}
this.verticies[vertexNum].weight = (1 - this.dampeningFactor) + this.dampeningFactor * tempSum
tempSum = 0
}
}
summaryBot.prototype._getEdgeTotals = function () {
let out = []
let tempSum = 0
for (let vertexNum = 0; vertexNum < this.numVerticies; vertexNum++) {
tempSum = 0
for (let edgeN in this.verticies[vertexNum].edge) {
tempSum += this.verticies[vertexNum].edge[edgeN]
}
//if this node is unrelated set weight to 0
if (!tempSum) {
this.verticies[vertexNum].weight = 0
this.unconnectedVerticies.add(vertexNum)
}
out.push(tempSum)
}
return out
}
//calculates the similarity between two verticies (edge weight)
//in: vertex obj
summaryBot.prototype._findSimilarity = function (vert1, vert2) {
let overlap = 0
for (let word in vert1.words){
if (vert2.words[word]){
overlap += Math.pow(vert1.words[word] * vert2.words[word], 0.7)
}
}
return overlap / (Math.log(vert1.numWords) + Math.log(vert2.numWords))
}
summaryBot.prototype._sortVerticiesByWeight = function () {
this.verticies.sort(function (a, b) {
if (a.weight < b.weight) return 1
if (a.weight > b.weight) return -1
return 0
})
}
module.exports = summaryBot