diff options
Diffstat (limited to 'core/index/index.go')
| -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), } } |
