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

Commit

Permalink
installed flag
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Oct 1, 2019
1 parent 0adb536 commit 3b09ede
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
30 changes: 28 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package main

import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)

const dataPathEnv = "NEXTWORD_DATA_PATH"
// default env value for the data directory path.
const envDataPath = "NEXTWORD_DATA_PATH"

// flags
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")

func main() {
os.Exit(run())
}

func run() int {
if !parseArgs() {
flag.Usage()
return 1
}

if err := serve(); err != nil {
fmt.Fprintf(os.Stderr, "serve error: %v", err)
return 1
Expand All @@ -22,7 +34,7 @@ func run() int {
}

func serve() error {
sg := NewSuggester(os.Getenv(dataPathEnv), 100)
sg := NewSuggester(*dataPath, *maxCandidatesNum)

sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
Expand All @@ -39,3 +51,17 @@ func serve() error {

return nil
}

func parseArgs() bool {
flag.Parse()
if *helpFlag {
return false
}
if *maxCandidatesNum < 1 {
return false
}
if *dataPath == "" {
return false
}
return true
}
34 changes: 17 additions & 17 deletions suggester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
)

var EnvDataPath = os.Getenv("NEXTWORD_DATA_PATH")
var EnvDataPathTest = os.Getenv("NEXTWORD_DATA_PATH")

func TestSuggester_Suggest(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestSuggester_Suggest(t *testing.T) {
}

for idx, test := range tests {
sg := NewSuggester(EnvDataPath, test.candidatesLen)
sg := NewSuggester(EnvDataPathTest, test.candidatesLen)

cand, err := sg.Suggest(test.query)
if err != nil {
Expand All @@ -103,7 +103,7 @@ func TestSuggester_Suggest(t *testing.T) {
}

func BenchmarkSuggester_Suggest(b *testing.B) {
sg := NewSuggester(EnvDataPath, 100)
sg := NewSuggester(EnvDataPathTest, 100)

for i := 0; i < b.N; i++ {
sg.Suggest("The quick brown fox ju")
Expand Down Expand Up @@ -195,17 +195,17 @@ func TestSuggester_SuggestNgram(t *testing.T) {
nil,
},
{
[]string{"0000000000"},
[]string{"0000000000 11111111 "},
nil,
},
{
[]string{"🤔"},
[]string{"🤔 🤗 "},
nil,
},
}

for idx, test := range tests {
sg := NewSuggester(EnvDataPath, 100)
sg := NewSuggester(EnvDataPathTest, 100)
cand, err := sg.suggestNgram(test.words)
if err != nil {
t.Errorf("[%d] unexpected error: %v", idx, err)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestSuggester_Suggest1gram(t *testing.T) {
}

for idx, test := range tests {
sg := NewSuggester(EnvDataPath, test.candidatesLen)
sg := NewSuggester(EnvDataPathTest, test.candidatesLen)

cand, err := sg.suggest1gram(test.prefix)
if !reflect.DeepEqual(test.err, err) {
Expand Down Expand Up @@ -361,27 +361,27 @@ func TestSuggester_BinSearch(t *testing.T) {
offset int64
}{
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
"A",
0,
},
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
"zł",
11346418,
},
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
"Recu",
4901265,
},
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
"Latemaa",
3303588,
},
{
filepath.Join(EnvDataPath, "2gram-e.txt"),
filepath.Join(EnvDataPathTest, "2gram-e.txt"),
"ELSE",
24641,
},
Expand Down Expand Up @@ -422,17 +422,17 @@ func TestSuggester_FindHeadOfLine(t *testing.T) {
head int64
}{
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
0,
0,
},
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
3,
2,
},
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
30749,
30734,
},
Expand Down Expand Up @@ -468,12 +468,12 @@ func TestSuggester_ReadLine(t *testing.T) {
line string
}{
{
filepath.Join(EnvDataPath, "1gram.txt"),
filepath.Join(EnvDataPathTest, "1gram.txt"),
0,
"A",
},
{
filepath.Join(EnvDataPath, "2gram-e.txt"),
filepath.Join(EnvDataPathTest, "2gram-e.txt"),
13617,
"EDUCA\tTION",
},
Expand Down

0 comments on commit 3b09ede

Please sign in to comment.