-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_article.js
141 lines (136 loc) · 4.69 KB
/
parse_article.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
var article_text = "Article text here";
/*
validate_period checks for extraneous cases at the end of a sentence
(Meaning that a period does not denote the end of a sentence or
different punctuation at the end of sentences).
This function mostly includes conditionals to check certain cases in
which is not a valid end of sentence, in this case, returning 0
indicating that it is not a valid end of sentence.
Otherwise, return 1.
This function is used to determine whether to add a
constructed sentence structure into the sentence array.
*/
validate_period = function(word) {
if (word.includes("?")) {
return 1;
} else if (word.includes("!")) {
return 1;
} else if (word.includes("Mr.")) {
return 0;
} else if (word.includes("Mrs.")) {
return 0;
} else if (word.includes("Ms.")) {
return 0;
} else if (word.includes("St.")) {
return 0;
} else if (word.includes("Prof.")) {
return 0;
} else if (word.includes("Co.")) {
return 0;
} else if (word.includes("Apt.")) {
return 0;
} else if (word.includes("Ph.")) {
return 0;
} else if (word.includes("Dr.")) {
return 0;
} else if (word.includes("Jr.")) {
return 0;
} else if (word.includes("Blvd.")) {
return 0;
} else if (word.includes("Sen.")) {
return 0;
} else if (word.includes("Sr.")) {
return 0;
} else if (word.includes("Ave.")) {
return 0;
} else if (word.includes("Pl.")) {
return 0;
} else if (word.includes ("Rd.")) {
return 0;
} else if (word.includes ("Cpt.")) {
return 0;
}
/*
Checks if the character before the period is uppercase, if yes, then
return 0. Otherwise, return 1.
*/
var periodChar = word.indexOf(".");
periodChar = periodChar - 1;
var charBeforePeriod = word.charAt(periodChar);
periodChar = periodChar + 1;
var charAfterPeriod = word.charAt(periodChar);
var numberquestion = parseInt(charBeforePeriod);
var number2question = parseInt(word.charAt(periodChar + 1));
if (charAfterPeriod == '"') {
return 2;
}
if (numberquestion != NaN && numberquestion < 10) {
if (number2question != NaN && number2question < 10) {
return 0;
}
return 1;
}
if (charBeforePeriod == charBeforePeriod.toUpperCase()) {
return 0;
}
if (periodChar != word.length - 1) {
return 3;
}
return 1;
}
/*
make_sent first parses article text into a word array. Secondly, analyzes periods
and puts sentences in a sentence array.
*/
make_sent = function(a) {
/*
Splitting into array of words based on whitespace.
*/
var word_array = a.split(" ");
var sentence_array = [];
var sentence = "";
for (word in word_array) {
/*
If word does not contain a period, add to sentence and a whitespace.
*/
if (!word_array[word].includes(".")) {
if (word_array[word].includes("?") || word_array[word].includes("!")) {
sentence = sentence + word_array[word];
sentence_array.push(sentence);
sentence = "";
} else {
sentence = sentence + word_array[word] + " ";
}
} else {
/*
Otherwise, test validity of sentence. If valid, puts in sentence
array and refreshes sentence string. Otherwise continue adding onto
the sentence.
*/
var validity = validate_period(word_array[word]);
if (validity == 1) {
sentence = sentence + word_array[word];
sentence_array.push(sentence);
sentence = "";
} else if (validity == 2) {
var periodChar = word_array[word].indexOf(".");
var afterPeriod = periodChar + 1;
var with_period = word_array[word].substring(0, afterPeriod+2);
var second_part = word_array[word].substring(afterPeriod + 2, word_array[word].length);
sentence = sentence + with_period;
sentence_array.push(sentence);
sentence = second_part + " ";
} else if (validity == 0) {
sentence = sentence + word_array[word] + " ";
} else {
var with_period = word_array[word].substring(0, word_array[word].indexOf(".")+ 1);
var second_part = word_array[word].substring(word_array[word].indexOf(".") + 1, word_array[word].length)
sentence = sentence + with_period;
sentence_array.push(sentence);
sentence = second_part + " ";
}
}
}
return sentence_array;
}
l.