summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-28 14:45:43 +0200
committeralex <[email protected]>2026-07-28 14:45:43 +0200
commit68f62b4fd161f6563d29ac8a8e102218f59fae43 (patch)
treed6cd27a63fceeceeca10870233c589db2a1a56cc
parent152c73d8205a252d4311595ffba562110334fd0f (diff)
downloadsearch-engine-68f62b4fd161f6563d29ac8a8e102218f59fae43.tar.xz
search-engine-68f62b4fd161f6563d29ac8a8e102218f59fae43.zip
added field score calcualtions giving the title a higher weight
-rw-r--r--index/index.go39
-rw-r--r--scoring/field.go10
-rw-r--r--scoring/idf.go2
3 files changed, 40 insertions, 11 deletions
diff --git a/index/index.go b/index/index.go
index 53f9fca..cceabeb 100644
--- a/index/index.go
+++ b/index/index.go
@@ -1,10 +1,14 @@
package index
-import "sync"
+import (
+ "searchEngine/analyzer"
+ "sync"
+)
type Posting struct {
- DocId int
- Count int
+ DocId int
+ Count int
+ TitleCount int
}
type InvertedIndex struct {
Mu sync.RWMutex
@@ -19,11 +23,25 @@ func New() *InvertedIndex {
}
}
-func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
+func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) {
+ titleTokens := analyzer.ProcessText(title)
- tokenCounts := make(map[string]int)
- for _, token := range tokens {
- tokenCounts[token]++
+ type fieldStats struct {
+ body int
+ title int
+ }
+ docStats := make(map[string]fieldStats)
+
+ for _, token := range titleTokens {
+ stats := docStats[token]
+ stats.title++
+ docStats[token] = stats
+ }
+
+ for _, token := range bodyTokens {
+ stats := docStats[token]
+ stats.body++
+ docStats[token] = stats
}
idx.Mu.Lock()
@@ -31,10 +49,11 @@ func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
idx.DocNames[docID] = title
- for token, count := range tokenCounts {
+ for token, stats := range docStats {
idx.Data[token] = append(idx.Data[token], Posting{
- DocId: docID,
- Count: count,
+ DocId: docID,
+ Count: stats.body,
+ TitleCount: stats.title,
})
}
}
diff --git a/scoring/field.go b/scoring/field.go
new file mode 100644
index 0000000..9637950
--- /dev/null
+++ b/scoring/field.go
@@ -0,0 +1,10 @@
+package scoring
+
+import "searchEngine/index"
+
+var TitleWeight = 5.0
+var BodyWeight = 1.0
+
+func FieldScore(p index.Posting) float64 {
+ return (float64(p.TitleCount) * TitleWeight) + (float64(p.Count) * BodyWeight)
+}
diff --git a/scoring/idf.go b/scoring/idf.go
index 6d96089..5c85ba3 100644
--- a/scoring/idf.go
+++ b/scoring/idf.go
@@ -13,5 +13,5 @@ func CalculateIDF(totalDocs int, docFrequency int) float64 {
}
func ScoreTFIDF(p index.Posting, idf float64) float64 {
- return float64(p.Count) * idf
+ return FieldScore(p) * idf
}