-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSearchString.js
More file actions
163 lines (120 loc) · 5.86 KB
/
SearchString.js
File metadata and controls
163 lines (120 loc) · 5.86 KB
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
//Load a book from disk
function loadBook(filename, displayName) {
let currentBook = "";
let url = "books/" + filename;
//reset our UI
document.getElementById("fileName").innerHTML = displayName;
document.getElementById("searchstat").innerHTML = "";
document.getElementById("keyword").value = "";
//create a server a request to load our book
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
currentBook = xhr.responseText;
getDocStats(currentBook);
//remove line breaks and carriage returns and replace with a <br>
currentBook = currentBook.replace(/(?:\r\n|\r|\n)/g, '<br>');
document.getElementById("fileContent").innerHTML = currentBook;
var elmnt = document.getElementById("fileContent");
elmnt.scrollTop = 0;
}
};
}
//get the stats for the book
function getDocStats(fileContent) {
var docLength = document.getElementById("docLength");
var wordCount = document.getElementById("wordCount");
var charCount = document.getElementById("charCount");
let text = fileContent.toLowerCase();
let wordArray = text.match(/\b\S+\b/g);
let wordDictionary = {};
var uncommonWords = [];
//filter out the uncommon words
uncommonWords = filterStopWords(wordArray);
//Count every word in the wordArray
for (let word in uncommonWords) {
let wordValue = uncommonWords[word];
if (wordDictionary[wordValue] > 0) {
wordDictionary[wordValue] += 1;
} else {
wordDictionary[wordValue] = 1;
}
}
//sort the array
let wordList = sortProperties(wordDictionary);
//Return the top 5 words
var top5Words = wordList.slice(0, 6);
//return the least 5 words
var least5Words = wordList.slice(-6, wordList.length);
//Write the values to the page
ULTemplate(top5Words, document.getElementById("mostUsed"));
ULTemplate(least5Words, document.getElementById("leastUsed"));
docLength.innerText = "Document Length: " + text.length;
wordCount.innerText = "Word Count: " + wordArray.length;
}
function ULTemplate(items, element) {
let rowTemplate = document.getElementById('template-ul-items');
let templateHTML = rowTemplate.innerHTML;
let resultsHTML = "";
for (i = 0; i < items.length - 1; i++) {
resultsHTML += templateHTML.replace('{{val}}', items[i][0] + " : " + items[i][1] + " time(s)");
}
element.innerHTML = resultsHTML;
}
function sortProperties(obj) {
//first convert the object to an array
let rtnArray = Object.entries(obj);
//Sort the array
rtnArray.sort(function (first, second) {
return second[1] - first[1];
});
return rtnArray;
}
//filter out stop words
function filterStopWords(wordArray) {
var commonWords = getStopWords();
var commonObj = {};
var uncommonArr = [];
for (i = 0; i < commonWords.length; i++) {
commonObj[commonWords[i].trim()] = true;
}
for (i = 0; i < wordArray.length; i++) {
word = wordArray[i].trim().toLowerCase();
if (!commonObj[word]) {
uncommonArr.push(word);
}
}
return uncommonArr;
}
//a list of stop words we don't want to include in stats
function getStopWords() {
return ["a", "able", "about", "across", "after", "all", "almost", "also", "am", "among", "an", "and", "any", "are", "as", "at", "be", "because", "been", "but", "by", "can", "cannot", "could", "dear", "did", "do", "does", "either", "else", "ever", "every", "for", "from", "get", "got", "had", "has", "have", "he", "her", "hers", "him", "his", "how", "however", "i", "if", "in", "into", "is", "it", "its", "just", "least", "let", "like", "likely", "may", "me", "might", "most", "must", "my", "neither", "no", "nor", "not", "of", "off", "often", "on", "only", "or", "other", "our", "own", "rather", "said", "say", "says", "she", "should", "since", "so", "some", "than", "that", "the", "their", "them", "then", "there", "these", "they", "this", "tis", "to", "too", "twas", "us", "wants", "was", "we", "were", "what", "when", "where", "which", "while", "who", "whom", "why", "will", "with", "would", "yet", "you", "your", "ain't", "aren't", "can't", "could've", "couldn't", "didn't", "doesn't", "don't", "hasn't", "he'd", "he'll", "he's", "how'd", "how'll", "how's", "i'd", "i'll", "i'm", "i've", "isn't", "it's", "might've", "mightn't", "must've", "mustn't", "shan't", "she'd", "she'll", "she's", "should've", "shouldn't", "that'll", "that's", "there's", "they'd", "they'll", "they're", "they've", "wasn't", "we'd", "we'll", "we're", "weren't", "what'd", "what's", "when'd", "when'll", "when's", "where'd", "where'll", "where's", "who'd", "who'll", "who's", "why'd", "why'll", "why's", "won't", "would've", "wouldn't", "you'd", "you'll", "you're", "you've"];
}
//highlight the words in search
function performMark() {
//read the keyword
var keyword = document.getElementById("keyword").value;
var display = document.getElementById("fileContent");
var newContent = "";
//find all the currently marked items
let spans = document.querySelectorAll('mark');
//<mark>Harry</mark>
//Harry
for (var i = 0; i < spans.length; i++) {
spans[i].outerHTML = spans[i].innerHTML;
}
var re = new RegExp(keyword, "gi");
var replaceText = "<mark id='markme'>$&</mark>";
var bookContent = display.innerHTML;
//add the mark to the book content
newContent = bookContent.replace(re, replaceText);
display.innerHTML = newContent;
var count = document.querySelectorAll('mark').length;
document.getElementById("searchstat").innerHTML = "found " + count + " matches";
if (count > 0) {
var element = document.getElementById("markme");
element.scrollIntoView();
};
}