Skip to content

Commit

Permalink
Equip Config with a Stoplist
Browse files Browse the repository at this point in the history
  • Loading branch information
hrs committed May 23, 2023
1 parent aedb7c2 commit 6d73861
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ type Config struct {
NoStoplist bool
OmitQuery bool
ShowScores bool
Stoplist *Stoplist
Verbose bool
}
2 changes: 1 addition & 1 deletion lib/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewDocument(path string, config *Config) (*Document, error) {
word = strings.TrimSuffix(word, "'s")

if word != "" {
if config.NoStoplist || !InStoplist(word) {
if config.NoStoplist || !config.Stoplist.Include(word) {
if config.NoStemming {
termCount[word]++
} else {
Expand Down
1 change: 1 addition & 0 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
NoStoplist: *noStoplistFlag,
OmitQuery: *omitQueryFlag,
ShowScores: *showScoresFlag,
Stoplist: &DefaultStoplist,
Verbose: *verboseFlag,
}

Expand Down
18 changes: 15 additions & 3 deletions lib/stoplist.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package main

func InStoplist(word string) bool {
_, ok := stoplist[word]
type Stoplist map[string]bool

func (stoplist *Stoplist) Include(term string) bool {
_, ok := (*stoplist)[term]
return ok
}

var stoplist = map[string]bool{
func NewStoplist(terms []string) *Stoplist {
stoplist := make(Stoplist)

for _, term := range terms {
stoplist[term] = true
}

return &stoplist
}

var DefaultStoplist = Stoplist{
"'ll": true,
"'ve": true,
"I": true,
Expand Down
28 changes: 26 additions & 2 deletions lib/stoplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "testing"

func TestInStoplist(t *testing.T) {
func TestDefaultStoplist(t *testing.T) {
tests := []struct {
word string
expected bool
Expand All @@ -14,7 +14,31 @@ func TestInStoplist(t *testing.T) {
}

for _, tc := range tests {
got := InStoplist(tc.word)
got := DefaultStoplist.Include(tc.word)

if got != tc.expected {
t.Errorf("got %t, wanted %t", got, tc.expected)
}
}
}

func TestCustomStoplist(t *testing.T) {
stoplist := NewStoplist([]string{
"foo",
"bar",
})

tests := []struct {
word string
expected bool
}{
{"foo", true},
{"bar", true},
{"baz", false},
}

for _, tc := range tests {
got := stoplist.Include(tc.word)

if got != tc.expected {
t.Errorf("got %t, wanted %t", got, tc.expected)
Expand Down

0 comments on commit 6d73861

Please sign in to comment.