-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsummary.js
214 lines (186 loc) · 8 KB
/
summary.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
//Array of stop words within the english language
const stopwordsArr = ["a", "about", "above", "above", "across", "after",
"afterwards", "again", "against", "all", "almost", "alone",
"along", "already", "also","although","always","am","among",
"amongst", "amoungst", "amount", "an", "and", "another",
"any","anyhow","anyone","anything","anyway", "anywhere",
"are", "around", "as", "at", "back","be",
"became", "because","become","becomes", "becoming",
"been", "before", "beforehand", "behind", "being",
"below", "beside", "besides", "between", "beyond",
"bill", "both", "bottom","but", "by", "call", "can",
"cannot", "cant", "co", "con", "could", "couldnt",
"cry", "de", "describe", "detail", "do", "done",
"down", "due", "during", "each", "eg", "eight", "either",
"eleven","else", "elsewhere", "empty", "enough", "etc", "even",
"ever", "every", "everyone", "everything", "everywhere", "except",
"few", "fifteen", "fify", "fill", "find", "fire", "first", "five",
"for", "former", "formerly", "forty", "found", "four", "from", "front",
"full", "further", "get", "give", "go", "had", "has", "hasnt", "have",
"he", "he's", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon",
"hers", "herself", "him", "himself", "his", "how", "however", "hundred",
"ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its",
"itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made",
"many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover",
"most", "mostly", "move", "much", "must", "my", "myself", "name", "namely",
"neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none",
"noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often",
"on", "once", "one", "only", "onto", "or", "other", "others", "otherwise",
"our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps",
"please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming",
"seems", "serious", "several", "she", "she's", "should", "show", "side", "since", "sincere",
"six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes",
"somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them",
"themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon",
"these", "they", "thick", "thin", "third", "this", "those", "though", "three", "through", "throughout",
"thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under",
"until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence",
"whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which",
"while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would",
"yet", "you", "your", "yours", "yourself", "yourselves", "the"];
const abbExceptions= [{abb: "Dr.", fix: "Dr "},
{abb: "Mr\.", fix: "Mr"},{abb: "Sen\.", fix: "Senator"},
{abb: "Mrs\.", fix: "Mrs"}, {abb: "Ms\.", fix: "Ms"},
{abb: "PHD\.", fix: "PHD"}, {abb: "U\.S", fix: "US"},
{abb: "U\.S\.A", fix: "USA"}, {abb: "U\.S\.", fix: "US"},
{abb: "Gen\.", fix: "General"}, {abb: "Col\.", fix: "Colonel"}];
const wordReg = /\w+(?:'\w{1,2})?/g;
const sentenceReg = /(\.+|\!|\?)(\"*|\'*|\)*|}*|]*)(\s|\n|\r|\r\n)/gm;
/**
* Creates the Sentence object
* @Author Lian Duan
* @Param string string value of sentence
* @Param index chronological index of function
*/
function Sentence(string, index, score){
this.content = string;
this.sentenceIndex = index;
this.score = score;
}
function Summary(text, keywordsArr, characterSummed, characterOrig){
this.keywords = keywordsArr;
this.text = text;
this.characterSummed = characterSummed;
this.characterOrig = characterOrig;
this.reductionfactor = 100-(characterSummed/characterOrig)*100;
}
/**
* Sanitizes the text of certain strings
* @Author Lian Duan
* @Param text the article in question
* @Param string the string that we are sanitizing
* @return a sanitized string
*/
function sanitizeString(text, string){
//This uniquely works to get rid of the strings
//eg. santizeString("The cow jumped over the fence", "the) => "cow jumped over fence"
var pattern = " "+string+" ";
var regex = new RegExp(pattern, "gi");
var sanitized = text.replace(regex, " ");
return sanitized;
}
function sanitizeForSpecificValue(text, value){
var pattern = " "+value.abb+" ";
var regex = new RegExp(pattern, "gi");
var sanitized = text.replace(regex, " "+value.fix+" ");
return sanitized;
}
/**
* Returns an array of keywords
* @Author Lian Duan
* @Param text the article in question
* @Param n number of keywords requested
* @return an array of keywords
*/
function getKeywords(text, n) {
var words = {};
var matches;
//Sanitizes the string from having any sort of stop words.
stopwordsArr.forEach( (string) => {
text = sanitizeString(text, string);
});
while ((matches = wordReg.exec(text)) != null) {
var word = matches[0].toLowerCase();
if (typeof words[word] == "undefined") {
words[word] = 1;
} else {
words[word]++;
}
}
var wordList = [];
for (var word in words) {
if (words.hasOwnProperty(word)) {
wordList.push([word, words[word]]);
}
}
wordList.sort(function (a, b) {
return b[1] - a[1];
});
var topWords = [];
for (var i = 0; i < (n && wordList.length); i++) {
topWords.push(wordList[i][0]);
}
console.log(topWords);
return topWords
}
//splits the string into substrings by sentence
function splitStringIntoSentenceArray(text) {
var inputArray = text.replace(sentenceReg, "$1|").split("|");;
return inputArray;
}
function scoreSentence(sentence, keywords){
stopwordsArr.forEach( (stopword) => {
if (sentence.content.includes(" "+stopword+" ")){
sentence.score = sentence.score - 0.25;
}
});
keywords.forEach( (keyword) => {
if (sentence.content.includes(" "+keyword+" ")){
sentence.score = sentence.score + 2;
}
});
return sentence;
}
/**
* Summarizes based on parameters
* @Author Lian Duan
* @Param text the article in question
* @Param sentences the amount of sentences we want
* @Param the amount of keywords we want
* @return Summary summary based on specifications
*/
function summarize(text, sentences, keywordsInt){
var textCount = text.split(' ').length;
var keywords = getKeywords(text, keywordsInt);
//Quickly sanitizes the string for exceptions
abbExceptions.forEach( (value) => {
text = sanitizeForSpecificValue(text, value);
});
//Split into sentences values
var sentence_arr = splitStringIntoSentenceArray(text);
var sentenceObj_Arr = [];
var index = 0;
//Create Sentence objects
sentence_arr.forEach( (sentence) => {
sentenceObj_Arr.push(new Sentence(sentence, index, 0));
index++;
});
//Scores all sentences
for (var i = 0; i <sentenceObj_Arr.length; i++){
sentenceObj_Arr[i] = scoreSentence(sentenceObj_Arr[i], keywords);
}
//Sorts the array by lowest to largest values
sentenceObj_Arr.sort((a, b) => parseFloat(a.score) - parseFloat(b.score));
sentenceObj_Arr = sentenceObj_Arr.slice(sentenceObj_Arr.length - sentences, sentenceObj_Arr.length);
sentenceObj_Arr.sort((a, b) => parseFloat(a.sentenceIndex) - parseFloat(b.sentenceIndex));
//Delete useless vars
delete sentence_arr;
delete index;
console.log(sentenceObj_Arr);
var summarytext = "";
sentenceObj_Arr.forEach( (sentence) => {
summarytext = summarytext+sentence.content+"\n";
});
var summary = new Summary(summarytext,keywords, summarytext.split(' ').length, textCount);
return summary;
}