-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.go
More file actions
42 lines (35 loc) · 1.02 KB
/
indexer.go
File metadata and controls
42 lines (35 loc) · 1.02 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
package tinysearch
import (
"bufio"
"io"
)
type Indexer struct {
index *Index
tokenizer *Tokenizer
}
func NewIndexer(tokenizer *Tokenizer) *Indexer {
return &Indexer{
index: NewIndex(),
tokenizer: tokenizer,
}
}
// ドキュメントをインデックスに追加する処理
func (idxr *Indexer) update(docID DocumentID, reader io.Reader) {
scanner := bufio.NewScanner(reader)
scanner.Split(idxr.tokenizer.SplitFunc) // ❶ 分割方法の指定
var position int
for scanner.Scan() {
term := scanner.Text() // ❷ 単語ごとに読み込み
// ポスティングリストの更新
if postingsList, ok := idxr.index.Dictionary[term]; !ok {
// ❸ termをキーとするポスティングリストが存在しない場合は新規作成
idxr.index.Dictionary[term] =
NewPostingsList(NewPosting(docID, position))
} else {
// ❹ ポスティングリストがすでに存在する場合は追加
postingsList.Add(NewPosting(docID, position))
}
position++
}
idxr.index.TotalDocsCount++
}