feat: Update Fiber version to v2 and update internal controller and search logic to use new index module
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// needs to be saved in a file or database
|
||||
type InvertedIndex map[string]map[string]int
|
||||
type DocumentFrequency map[string]int
|
||||
|
||||
var Index InvertedIndex
|
||||
var DocFreq DocumentFrequency
|
||||
var Files []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)
|
||||
}
|
||||
|
||||
Files = files
|
||||
Index = index
|
||||
DocFreq = docFreq
|
||||
}
|
@@ -3,6 +3,8 @@ package search
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"git.dev-null.rocks/alexohneander/gosearch/pkg/index"
|
||||
)
|
||||
|
||||
// SearchResult stores the document and its relevance score.
|
||||
@@ -12,7 +14,7 @@ type SearchResult struct {
|
||||
}
|
||||
|
||||
// Search processes different types of queries using TF-IDF scoring.
|
||||
func Search(terms []string, queryType string, index InvertedIndex, docFreq DocumentFrequency, numDocs int) []SearchResult {
|
||||
func Search(terms []string, queryType string, index index.InvertedIndex, docFreq index.DocumentFrequency, numDocs int) []SearchResult {
|
||||
scores := make(map[string]float64)
|
||||
|
||||
if queryType == "AND" {
|
||||
@@ -40,7 +42,7 @@ func Search(terms []string, queryType string, index InvertedIndex, docFreq Docum
|
||||
}
|
||||
|
||||
// Helper function to score a single document based on terms
|
||||
func scoreDoc(terms []string, doc string, index InvertedIndex, docFreq DocumentFrequency, numDocs int) float64 {
|
||||
func scoreDoc(terms []string, doc string, index index.InvertedIndex, docFreq index.DocumentFrequency, numDocs int) float64 {
|
||||
score := 0.0
|
||||
for _, term := range terms {
|
||||
tf := float64(index[term][doc])
|
||||
@@ -52,7 +54,7 @@ func scoreDoc(terms []string, doc string, index InvertedIndex, docFreq DocumentF
|
||||
}
|
||||
|
||||
// Helper function to intersect documents for AND logic
|
||||
func intersectDocs(terms []string, index InvertedIndex) []string {
|
||||
func intersectDocs(terms []string, index index.InvertedIndex) []string {
|
||||
if len(terms) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user