-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfsearch-indexlib.ts
331 lines (289 loc) · 9.7 KB
/
ipfsearch-indexlib.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import * as fs from "fs"
export class Indexer{
invertedindex : Map<string,Token>
index : Map<string,Document> //maps docid to whole doc
constructor(){
this.invertedindex = new Map()
this.index = new Map()
}
/**
* Saves invinx and inx to cwd, and
* @param invinxFilename relative to cwd. If blocksize set, will be appended by number.
* @param indexFilename relative to cwd. If blocksize set, will be appended by number.
* @param baseURL the start of the URL for invinxFilename and indexFilename.
* The meta.json file will be placed in indexFilename + ".meta.json"
* @param blocksize If you don't provide a blocksize, files won't be split up.
*/
persist(invinxFilename : string, indexFilename : string, author : string, indexname : string, baseURL : string, blocksize? : number){
if(blocksize === undefined){
let sortedindex = sortInvertedIndex(mapToArray(this.invertedindex))
saveInvertedIndexToFile(sortedindex, invinxFilename)
// may choke on large indices
fs.writeFileSync(indexFilename, JSON.stringify(sortIndex(mapToArray(this.index))))
}else{
let indexsplits = saveIndexToFiles(sortIndex(mapToArray(this.index)),indexFilename,blocksize)
let invinxsplitmap : Array<string> = []
let invinx = sortInvertedIndex(mapToArray(this.invertedindex))
for(let needle = 0; needle < invinx.length; needle++){
if(needle % blocksize == 0){
invinxsplitmap.push(invinx[needle].name)
}
}
for(let needle = 0; needle < invinxsplitmap.length; needle++){
console.debug("saving index #"+needle+", starts with "+invinxsplitmap[needle])
saveInvertedIndexToFile(invinx.slice(blocksize*needle, blocksize*(needle+1)), invinxFilename + needle)
}
let meta = {
author : author,
name : indexname,
created : Date.now(),
invURLBase : baseURL + invinxFilename,
inxURLBase : baseURL + indexFilename,
inxsplits : indexsplits,
invsplits : invinxsplitmap
}
fs.writeFile(indexFilename+".meta.json",JSON.stringify(meta), (err) => {if(err){console.error(err)}})
}
}
private addDocumentToInvertedIndex(document : Document){
tokenizeAndFilter(document.getText()).forEach(function(tokenname : string) {
this.addTokenToInvertedIndex(new Token(tokenname,[document.id]))
},this)
}
private addTokenToInvertedIndex(token : Token){
if(this.invertedindex.has(token.name)){
token.documents.forEach((docid) => {
this.invertedindex.get(token.name).addDocument(docid)
})
}else{
this.invertedindex.set(token.name, token)
}
}
/**
* Add a document to the index AND invertedindex.
* @param document
*/
addToIndex(document : Document){
this.index.set(document.id, document)
this.addDocumentToInvertedIndex(document)
}
}
/**
* Replace all chars "find" in a string with "replace"
* @param str string
* @param find char
* @param replace char
*/
function replaceAll(str : string, find : string, replace : string) : string{
let newstr = []
for(var i = 0, len = str.length; i < len; i++){
if(str[i] === find){
newstr[i] = replace
}else{
newstr[i] = str[i]
}
}
return newstr.join("")
}
import * as stemmer from "./porterstemmer.js"
/**
* Tokenizes a text, filters out stopwords and stems the tokens.
* shouldn't really be an export function, but for tests, it has to...
* @param text
* @param separators? optional by what chars to split the string while tokenizing
*/
export function tokenizeAndFilter(name : string) : string[]{
let tokens : string[] = name.split(' ').join(',').split('.').join(',').split('(').join(',').split(')').join(',').split('-').join(',').split('_').join(',').split('[').join(',').split(']').join(',').split(',') // super super awful and nasty, but gets the job done.
tokens = tokens.filter(function (token){
if(token){
if(token.toLowerCase() === "the" || token.toLowerCase() === "of" || token.toLowerCase() === "and" || token === "&" || token === "+"){
return false
}else if(token.length <= 1){
return false
}else if(token.startsWith("&") && token.indexOf(';') > -1){
return false
}else{
return true
}
}else{
return false
}
})
tokens.forEach((value,index,array) => {array[index] = value.toLowerCase()})
tokens.forEach((value,index,array) => {array[index] = stemmer.stemmer(value)})
return tokens
}
export class Token{
name : string
documents : Array<string>
constructor(token : string, documents : string[]){
this.name = token
this.documents = documents
}
/**
* Add a document to the token. Silently doesn't add a duplicit one.
* @param documentid
*/
addDocument(documentid : string){
if(this.documents.indexOf(documentid) === -1){
this.documents.push(documentid)
}
}
toString(){
return name
}
}
/**
* Represents a document in the index.
* Subclass this if you require specific fields and override getText method to return all text that should get indexed.
* id will be used for sorting when breaking it up.
* @param id should be UNIQUE across the whole index.
*/
export class Document{
id : string
text : string
constructor(id : string,text : string){
this.id = id
this.text = text
}
/**
* Return text for tokenization and adding to the inverted index.
*/
getText(){
return this.text
}
}
/**
* @param index index in array form
* @return alphabetically sorted index array
*/
export function sortInvertedIndex(index : Array<Token>) : Array<Token>{
index.sort(function(a,b){
if(a.name < b.name){
return -1
}else if(a.name == b.name){
return 0
}else{
return 1
}
})
return index
}
/**
* @param index index in array form
* @return alphabetically sorted index array
*/
export function sortIndex(index : Array<Document>) : Array<Document>{
index.sort(function(a,b){
if(a.id < b.id){
return -1
}else if(a.id == b.id){
return 0
}else{
return 1
}
})
return index
}
/**
* Put all values from a Map into an Array.
* @param map
*/
export function mapToArray(map : Map<string,any>) : Array<any>{
let array = []
for(let key of map.values()){
array.push(key)
}
return array
}
/**
* Save index to files, breaking it up.
* @param index
* @param filenamestub
* @param blocksize How many documents to store in one file
*/
export function saveIndexToFiles(index : Array<Document>, filenamestub : string, blocksize : number) : string[]{
let splitmap : string[] = []
for(let needle = 0; needle < index.length; needle++){
if(needle % blocksize == 0){
splitmap.push(index[needle].id)
}
}
for(let needle = 0; needle < splitmap.length; needle++){
//console.debug("saving index #"+needle+", starts with "+splitmap[needle])
saveIndexToFile(index.slice(blocksize*needle, blocksize*(needle+1)), filenamestub + needle)
}
return splitmap
}
/**
* Intended to be used by saveIndexToFiles()
* @param index
* @param filename
*/
function saveIndexToFile(index : Array<Document>, filename : string){
fs.writeFile(filename,JSON.stringify(index), (err) => {if(err){console.error(err)}})
}
/**
* Save whole inverted index (sorted array) to a file.
*
* Format of the file for inverted index:
* first line is the file format version number (currently 1)
* more lines are in the following format:
* token,documentid,documentid...
*
* Tokens are separated by newlines.
* Tokens are urlencoded.
*
* Async.
* @returns void
*/
export function saveInvertedIndexToFile(invindex: Array<Token>, filename: string) : void {
let writeStream = fs.createWriteStream(filename)
writeStream.on('error',function(err){
console.log('error:' + err)
})
writeStream.write("1\n") //version number
invindex.forEach(function(token){
let string = encodeURIComponent(token.name)
for(let docid of token.documents){
docid = docid.replace(",","%2C")
string = string + "," + docid
}
writeStream.write(string + "\n")
})
}
/**
* Load inverted index from file
* @param filename
* @param callback
*/
export function loadIndexFromFile(filename : string, callback : IndexCallback) : void {
let loadedIndex : Array<Token> = []
var lineReader = require('readline').createInterface({
input: fs.createReadStream(filename)
});
let lineNumber = 0
lineReader.on('line', (line : string) => {
if(lineNumber === 0){
if(parseInt(line) != 2){
throw "Invalid version, must be 2!"
}
lineNumber++
return
}
let cols = line.split(",")
cols = cols.map(function(value){
return value.replace("%2C",",")
})
let tokenname = decodeURIComponent(cols[0])
cols.shift()
loadedIndex.push(new Token(tokenname, cols))
lineNumber++
})
lineReader.on('close', function(){
return callback(loadedIndex)
})
}
export interface IndexCallback {
( index : Array<Token>) : void;
}