summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authoralex <[email protected]>2026-10-10 22:49:19 +0200
committeralex <[email protected]>2026-10-10 22:49:19 +0200
commit251bb0cf9e58caa0d22066681d93ea09639f53eb (patch)
tree0529e74c94942508b77bea7df3cc8dc2e02b3d58 /core
parentb2e05a1d41540586cbaa7c8ee532393501601154 (diff)
downloadsearch-engine-main.tar.xz
search-engine-main.zip
unknownHEADmain
Diffstat (limited to 'core')
-rw-r--r--core/index/index.go19
-rw-r--r--core/scoring/BM25.go20
-rw-r--r--core/search/search.go11
3 files changed, 45 insertions, 5 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]})
}
}