diff options
| author | alex <[email protected]> | 2026-10-10 22:49:19 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-10-10 22:49:19 +0200 |
| commit | 251bb0cf9e58caa0d22066681d93ea09639f53eb (patch) | |
| tree | 0529e74c94942508b77bea7df3cc8dc2e02b3d58 /core/index | |
| parent | b2e05a1d41540586cbaa7c8ee532393501601154 (diff) | |
| download | search-engine-main.tar.xz search-engine-main.zip | |
Diffstat (limited to 'core/index')
| -rw-r--r-- | core/index/index.go | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/core/index/index.go b/core/index/index.go index b00f524..27fbc1d 100644 --- a/core/index/index.go +++ b/core/index/index.go @@ -19,12 +19,27 @@ type InvertedIndex struct { Data map[string][]Posting DocNames map[int]string DocLengths map[int]int + AvgDocLen float64 } +func (idx *InvertedIndex) CalculateAverageDocLength() { + if len(idx.DocLengths) == 0 { + idx.AvgDocLen = 0.0 + return + } + + totalLength := 0 + for _, length := range idx.DocLengths { + totalLength += length + } + + idx.AvgDocLen = float64(totalLength) / float64(len(idx.DocLengths)) +} func New() *InvertedIndex { return &InvertedIndex{ - Data: make(map[string][]Posting), - DocNames: make(map[int]string), + Data: make(map[string][]Posting), + DocNames: make(map[int]string), + DocLengths: make(map[int]int), } } |
