Skip to content

Commit ad0fe79

Browse files
committed
[docs]: Remove inline comments and update JS Doc
1 parent 77e09fb commit ad0fe79

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

src/inverted-index.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class InvertedIndex {
4343

4444
/**
4545
* Checks if a book object is valid
46+
* A book object should have two keys; title and text
47+
* The title and text should be non-empty strings
4648
* @method checkBookObj
4749
* @static
4850
* @param {Object} book -A book object in the JSON Array
4951
* @returns {Boolean} -true if object is valid, false otherwise
5052
*/
5153
static checkBookObj(book) {
52-
/* fileObj should have two keys - title and text
53-
title and text should be non-empty strings */
5454
if (typeof book !== 'object') {
5555
return false;
5656
}
@@ -75,9 +75,7 @@ class InvertedIndex {
7575
*/
7676
static analyse(book) {
7777
const bookText = book.text;
78-
// change all non-word characters including '_' to ' '
7978
const normalizedBookText = this.normalize(bookText);
80-
// tokenize; split normalized book text to word units
8179
const bookTokens = normalizedBookText.split(/\s+/);
8280
return bookTokens;
8381
}
@@ -120,22 +118,17 @@ class InvertedIndex {
120118
*/
121119
static multiTermSearch(index, chainedTerms) {
122120
const termsArray = chainedTerms.split(' ');
123-
// Accumulate indexes of all terms
124121
const indicesArray = termsArray.reduce((acc, term) => {
125122
const indices = index[term] || [];
126123
acc.push(...indices);
127124
return acc;
128125
}, []);
129126
const indexCount = {};
130-
// count the number of occurence of each index in indicesArray
131127
indicesArray.forEach((val) => {
132128
Object.prototype.hasOwnProperty.call(indexCount, val) ?
133129
indexCount[val] += 1 : indexCount[val] = 1;
134130
});
135131
const bookContainsAll = [];
136-
/* Each index can only occur once for a term
137-
if an index frequency === number of search terms
138-
===> the book at that index has all terms */
139132
Object.keys(indexCount).forEach((bookIndex) => {
140133
if (indexCount[bookIndex] === termsArray.length) {
141134
bookContainsAll.push(parseInt(bookIndex, 10));
@@ -152,16 +145,13 @@ class InvertedIndex {
152145
* @returns {Object.<array>} -A map from words to array of book indices
153146
*/
154147
createIndex(fileName, fileContent) {
155-
// Check if file is valid
156148
const nameIsValid = (typeof fileName === 'string'
157149
&& /^\w+.json$/i.test(fileName));
158150
const contentIsValid = InvertedIndex.validateFileContent(fileContent);
159151
if (nameIsValid && contentIsValid[0]) {
160152
const index = {};
161153
fileContent.forEach((book, bookIndex) => {
162-
// for each book in the file, analyse
163154
InvertedIndex.analyse(book).forEach((token) => {
164-
// for each token in analysed book text, check and update `indexes`
165155
if (Object.prototype.hasOwnProperty.call(index, token)) {
166156
if (!index[token].includes(bookIndex)) {
167157
index[token].push(bookIndex);

0 commit comments

Comments
 (0)