refactor: index and removed old functions

This commit is contained in:
Alex Wellnitz 2025-03-25 21:38:46 +01:00
parent 69b050fbd6
commit b81990e243

View File

@ -2,8 +2,6 @@ package index
import (
"bufio"
"log"
"os"
"strings"
)
@ -15,53 +13,6 @@ var Index InvertedIndex
var DocFreq DocumentFrequency
var Documents []string
// BuildIndex reads files and builds an inverted index.
func BuildIndex(files []string) (InvertedIndex, DocumentFrequency, error) {
index := make(InvertedIndex)
docFreq := make(DocumentFrequency)
for _, file := range files {
f, err := os.Open(file)
if err != nil {
return nil, nil, err
}
defer f.Close()
seenTerms := make(map[string]bool) // Track terms in this document
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := strings.ToLower(strings.Trim(scanner.Text(), ",.!?"))
if index[word] == nil {
index[word] = make(map[string]int)
}
index[word][file]++
if !seenTerms[word] {
docFreq[word]++
seenTerms[word] = true
}
}
}
return index, docFreq, nil
}
func TestIndex() {
// Index files
files := []string{"data/doc1.txt", "data/doc2.txt", "data/doc3.txt"}
index, docFreq, err := BuildIndex(files)
if err != nil {
log.Fatalf("Error building index: %v", err)
}
Documents = files
Index = index
DocFreq = docFreq
}
func CreateIndex() {
index := make(InvertedIndex)
docFreq := make(DocumentFrequency)