diff options
| author | alex <[email protected]> | 2026-07-28 17:37:06 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-28 17:37:06 +0200 |
| commit | c91c3afd8700138f8eb8a25a4ae422c2c9e46cea (patch) | |
| tree | fd9b5ea4f3b821581b2ceb4e7fa8afc1a84c0eeb /index/index.go | |
| parent | 5858d34b8ab573e18a4f6ddc5ee962b24ea269dc (diff) | |
| download | search-engine-c91c3afd8700138f8eb8a25a4ae422c2c9e46cea.tar.xz search-engine-c91c3afd8700138f8eb8a25a4ae422c2c9e46cea.zip | |
refactored entire code base to be in the package core so i can later implement interfaces to interact with the engine
Diffstat (limited to 'index/index.go')
| -rw-r--r-- | index/index.go | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/index/index.go b/index/index.go deleted file mode 100644 index 60c3d9f..0000000 --- a/index/index.go +++ /dev/null @@ -1,67 +0,0 @@ -package index - -import ( - "searchEngine/analyzer" - "sync" -) - -type Posting struct { - DocId int - BodyCount int - TitleCount int - BodyPositions []int - TitlePositions []int -} -type InvertedIndex struct { - Mu sync.RWMutex - Data map[string][]Posting - DocNames map[int]string -} - -func New() *InvertedIndex { - return &InvertedIndex{ - Data: make(map[string][]Posting), - DocNames: make(map[int]string), - } -} - -func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { - titleTokens := analyzer.ProcessText(title) - - type fieldStats struct { - body int - bodyPositions []int - title int - titlePositions []int - } - docStats := make(map[string]fieldStats) - - for position, token := range titleTokens { - stats := docStats[token] - stats.title++ - stats.titlePositions = append(stats.titlePositions, position) - docStats[token] = stats - } - - for position, token := range bodyTokens { - stats := docStats[token] - stats.body++ - stats.bodyPositions = append(stats.bodyPositions, position) - docStats[token] = stats - } - - idx.Mu.Lock() - defer idx.Mu.Unlock() - - idx.DocNames[docID] = title - - for token, stats := range docStats { - idx.Data[token] = append(idx.Data[token], Posting{ - DocId: docID, - BodyCount: stats.body, - TitleCount: stats.title, - BodyPositions: stats.bodyPositions, - TitlePositions: stats.titlePositions, - }) - } -} |
