@@ -43,14 +43,14 @@ class InvertedIndex {
43
43
44
44
/**
45
45
* 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
46
48
* @method checkBookObj
47
49
* @static
48
50
* @param {Object } book -A book object in the JSON Array
49
51
* @returns {Boolean } -true if object is valid, false otherwise
50
52
*/
51
53
static checkBookObj ( book ) {
52
- /* fileObj should have two keys - title and text
53
- title and text should be non-empty strings */
54
54
if ( typeof book !== 'object' ) {
55
55
return false ;
56
56
}
@@ -75,9 +75,7 @@ class InvertedIndex {
75
75
*/
76
76
static analyse ( book ) {
77
77
const bookText = book . text ;
78
- // change all non-word characters including '_' to ' '
79
78
const normalizedBookText = this . normalize ( bookText ) ;
80
- // tokenize; split normalized book text to word units
81
79
const bookTokens = normalizedBookText . split ( / \s + / ) ;
82
80
return bookTokens ;
83
81
}
@@ -120,22 +118,17 @@ class InvertedIndex {
120
118
*/
121
119
static multiTermSearch ( index , chainedTerms ) {
122
120
const termsArray = chainedTerms . split ( ' ' ) ;
123
- // Accumulate indexes of all terms
124
121
const indicesArray = termsArray . reduce ( ( acc , term ) => {
125
122
const indices = index [ term ] || [ ] ;
126
123
acc . push ( ...indices ) ;
127
124
return acc ;
128
125
} , [ ] ) ;
129
126
const indexCount = { } ;
130
- // count the number of occurence of each index in indicesArray
131
127
indicesArray . forEach ( ( val ) => {
132
128
Object . prototype . hasOwnProperty . call ( indexCount , val ) ?
133
129
indexCount [ val ] += 1 : indexCount [ val ] = 1 ;
134
130
} ) ;
135
131
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 */
139
132
Object . keys ( indexCount ) . forEach ( ( bookIndex ) => {
140
133
if ( indexCount [ bookIndex ] === termsArray . length ) {
141
134
bookContainsAll . push ( parseInt ( bookIndex , 10 ) ) ;
@@ -152,16 +145,13 @@ class InvertedIndex {
152
145
* @returns {Object.<array> } -A map from words to array of book indices
153
146
*/
154
147
createIndex ( fileName , fileContent ) {
155
- // Check if file is valid
156
148
const nameIsValid = ( typeof fileName === 'string'
157
149
&& / ^ \w + .j s o n $ / i. test ( fileName ) ) ;
158
150
const contentIsValid = InvertedIndex . validateFileContent ( fileContent ) ;
159
151
if ( nameIsValid && contentIsValid [ 0 ] ) {
160
152
const index = { } ;
161
153
fileContent . forEach ( ( book , bookIndex ) => {
162
- // for each book in the file, analyse
163
154
InvertedIndex . analyse ( book ) . forEach ( ( token ) => {
164
- // for each token in analysed book text, check and update `indexes`
165
155
if ( Object . prototype . hasOwnProperty . call ( index , token ) ) {
166
156
if ( ! index [ token ] . includes ( bookIndex ) ) {
167
157
index [ token ] . push ( bookIndex ) ;
0 commit comments