From 251bb0cf9e58caa0d22066681d93ea09639f53eb Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 10 Oct 2026 22:49:19 +0200 Subject: unknown --- core/index/index.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'core/index') 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), } } -- cgit v1.2.3