Check stopwords

This commit is contained in:
2025-09-10 20:06:46 +08:00
parent af81f66f25
commit 45beaced89
3 changed files with 34 additions and 0 deletions

View File

@@ -101,3 +101,14 @@ func SearchResource(keyword string) ([]uint, error) {
return results, nil
}
func IsStopWord(word string) bool {
mapping := bleve.NewIndexMapping()
analyzerName := mapping.DefaultAnalyzer
analyzer := mapping.AnalyzerNamed(analyzerName)
if analyzer == nil {
return false
}
tokens := analyzer.Analyze([]byte(word))
return len(tokens) == 0
}

View File

@@ -163,3 +163,23 @@ func TestSearchResource(t *testing.T) {
})
}
}
func TestIsStopWord(t *testing.T) {
Init()
defer TearDown()
stopWords := []string{"the", "is", "at", "which", "on", "and", "a", "an", "in", "to", "of"}
nonStopWords := []string{"adventure", "mystery", "romance", "sci-fi", "comedy", "action", "horror"}
for _, word := range stopWords {
if !IsStopWord(word) {
t.Errorf("Expected '%s' to be identified as a stop word", word)
}
}
for _, word := range nonStopWords {
if IsStopWord(word) {
t.Errorf("Expected '%s' to not be identified as a stop word", word)
}
}
}