Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
added greedy flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Oct 6, 2019
1 parent 10150a5 commit 16cdeca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const envDataPath = "NEXTWORD_DATA_PATH"
var dataPath = flag.String("data", os.Getenv(envDataPath), "path to data directory")
var maxCandidatesNum = flag.Int("candidates-num", 100, "max candidates num (positive int)")
var helpFlag = flag.Bool("h", false, "show this message")
var greedyFlag = flag.Bool("greedy", false, "show as many result as possible")

func main() {
os.Exit(run())
Expand Down Expand Up @@ -46,7 +47,7 @@ environment value "$NEXTWORD_DATA_PATH". `
}

func serve() error {
sg := NewSuggester(*dataPath, *maxCandidatesNum)
sg := NewSuggester(*dataPath, *maxCandidatesNum, *greedyFlag)

sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
Expand Down
20 changes: 18 additions & 2 deletions suggester.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import (
type Suggester struct {
dataPath string
candidateLen int
greedy bool
}

func NewSuggester(dataPath string, candidateLen int) *Suggester {
return &Suggester{dataPath: dataPath, candidateLen: candidateLen}
func NewSuggester(dataPath string, candidateLen int, greedy bool) *Suggester {
return &Suggester{
dataPath: dataPath,
candidateLen: candidateLen,
greedy: greedy,
}
}

func (*Suggester) fileName(n int, prefix string) string {
Expand Down Expand Up @@ -44,6 +49,17 @@ func (sg *Suggester) Suggest(query string) (candidates []string, err error) {
return
}
candidates = append(candidates, cand...)

// return when non greedy with non empty candidates
if !sg.greedy {
candidates = sg.filterCandidates(candidates, prefix)
if len(candidates) > sg.candidateLen {
candidates = candidates[:sg.candidateLen]
}
if len(candidates) > 0 {
return
}
}
}

// search 1gram
Expand Down

0 comments on commit 16cdeca

Please sign in to comment.