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 | |
| parent | b2e05a1d41540586cbaa7c8ee532393501601154 (diff) | |
| download | search-engine-main.tar.xz search-engine-main.zip | |
| -rw-r--r-- | core/index/index.go | 19 | ||||
| -rw-r--r-- | core/scoring/BM25.go | 20 | ||||
| -rw-r--r-- | core/search/search.go | 11 | ||||
| -rw-r--r-- | main.go | 33 | ||||
| -rw-r--r-- | wikipedia_index.gob | bin | 259 -> 0 bytes |
5 files changed, 64 insertions, 19 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), } } diff --git a/core/scoring/BM25.go b/core/scoring/BM25.go new file mode 100644 index 0000000..122c7f3 --- /dev/null +++ b/core/scoring/BM25.go @@ -0,0 +1,20 @@ +package scoring + +import "searchEngine/core/index" + +var k1 float64 = 1.2 +var b float64 = 0.75 + +func ScoreBM25(p index.Posting, idf float64, docLen int, avgDocLen float64) float64 { + tf := float64(len(p.BodyPositions)) + + if tf == 0 { + return 0.0 + } + + numerator := idf * (tf * (k1 + 1)) + + denominator := tf + k1*(1.0-b+b*(float64(docLen)/avgDocLen)) + + return numerator / denominator +} diff --git a/core/search/search.go b/core/search/search.go index cf1054b..af35cae 100644 --- a/core/search/search.go +++ b/core/search/search.go @@ -39,7 +39,7 @@ func Search(idx *index.InvertedIndex, query string) []string { for _, token := range tokenizedQuery { postings, exists := idx.Data[token] if !exists { - return nil + continue } docFrequency := len(postings) @@ -47,16 +47,21 @@ func Search(idx *index.InvertedIndex, query string) []string { for _, p := range postings { docMatchCounts[p.DocId]++ - termScore := scoring.ScoreTFIDF(p, idf) + termScore := scoring.ScoreBM25(p, idf, len(p.BodyPositions), idx.AvgDocLen) + if len(p.TitlePositions) > 0 { + termScore += 5.0 + } scores[p.DocId] += termScore docPostings[p.DocId] = append(docPostings[p.DocId], p) } } + requiredMatches := max(int(float64(len(uniqueTokens))*1.0), 1) + var results []Result for docID, count := range docMatchCounts { - if count == len(uniqueTokens) { + if count >= requiredMatches { results = append(results, Result{DocID: docID, Score: scores[docID]}) } } @@ -17,24 +17,29 @@ func main() { if loadErr != nil { fmt.Printf("Warning: Cache file is corrupted (%v). Re-indexing...\n", loadErr) os.Remove(indexPath) - engine = core.NewEngine() - dumpPath := "C:/Users/bambi15/Documents/wikipedia/simplewiki-2026-07-01-p1p1273731/simplewiki-2026-07-01-p1p1273731.xml" - - fmt.Println("Ingesting and indexing wiki dump...") - err := engine.IndexDump(dumpPath, 4) - if err != nil { - panic(err) - } - - fmt.Println("\nIndexing complete! Saving index to disk for future runs...") - err = engine.Index().Save(indexPath) - if err != nil { - fmt.Printf("Warning: Failed to save index cache: %v\n", err) - } } else { + idx.CalculateAverageDocLength() engine = core.NewEngineWithIndex(idx) fmt.Println("Index loaded successfully!") + } + } + + if engine == nil { + engine = core.NewEngine() + dumpPath := "C:/Users/bambi15/Documents/wikipedia/simplewiki-2026-07-01-p1p1273731/simplewiki-2026-07-01-p1p1273731.xml" + + fmt.Println("Ingesting and indexing wiki dump...") + err := engine.IndexDump(dumpPath, 4) + if err != nil { + panic(err) + } + + fmt.Println("\nIndexing complete! Saving index to disk for future runs...") + engine.Index().CalculateAverageDocLength() + err = engine.Index().Save(indexPath) + if err != nil { + fmt.Printf("Warning: Failed to save index cache: %v\n", err) } } diff --git a/wikipedia_index.gob b/wikipedia_index.gob Binary files differdeleted file mode 100644 index dd392c9..0000000 --- a/wikipedia_index.gob +++ /dev/null |
