-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (68 loc) · 2.76 KB
/
script.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
var textarea = $('#flesch-text');
var button = $('#flesch-submit');
var flesch = $('#flesch-value');
var Sentiment = require('sentiment');
var sentiment = new Sentiment();
button.click(function () {
var answer = textarea.val();
let chars = answer.split("");
for (let i = 0; i < chars.length - 1; i++) {
chars[i] += " ";
}
let words = answer.split(" ");
for (let i = 0; i < words.length - 1; i++) {
words[i] += " ";
}
if(!answer.includes(' ')){
return answer.length;
};
function calculateFlesch(totalSentences, totalWords, totalSyllables, ) {
return (0.39 * (totalWords/totalSentences) + 11.8 * (totalSyllables/totalWords)-15.59)
}
function getSyllables(word) {
word = word.toLowerCase();
if(word.length <= 3) { return 1; }
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
word = word.replace(/^y/, '');
let syl = word.match(/[aeiouy]{1,2}/g);
return (syl && syl.length) || 0;
}
let totalSentences = 0, totalWords = 0, totalSyllables = 0;
let text = answer;
let sentences = text.split(/[\\.!\?]/);
totalSentences = sentences.length;
sentences.forEach(function (sentence) {
var word = "";
for (let i = 0; i < sentence.length; i++) {
word += sentence[i];
if(sentence[i] == " ") {
totalWords++;
totalSyllables += getSyllables(word);
word = "";
}
}
if(word.length > 0) {
totalWords++;
totalSyllables += getSyllables(word);
word = ""
}
});
let charsLen = chars.length;
let wordsLen = words.length;
console.log("Number of words: ", wordsLen);
console.log("Number of characters: ", charsLen);
const average = (charsLen - wordsLen + 1) / wordsLen;
console.log("Avarage word length: ",Math.round(average));
let findDuplicates = arr => arr.filter((item, index) => arr.lastIndexOf(item) != index)
let duplicateTest = ([...new Set(findDuplicates(words))]) // Unique duplicates
console.log("Most common word in the text: ",duplicateTest[0]);
console.log("Reading level of the text: ",Math.round(calculateFlesch(totalSentences, totalWords, totalSyllables)));
let result = sentiment.analyze(answer);
console.log("Positive words from the text: ",);
result.positive.forEach(element => console.log(element));
console.log("Negative words from the text: ",);
result.negative.forEach(element => console.log(element));
rl.close();
console.log('Flesch: ', console.log("Number of words: ", wordsLen));
flesch.html(_.console.log("Number of words: ", wordsLen));
});