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

Commit

Permalink
change NewNextword args.
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Dec 30, 2019
1 parent bc9fe71 commit 1241e71
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const nextwordDataPath = "NEXTWORD_DATA_PATH"
var dataPath = flag.String("data", os.Getenv(nextwordDataPath), "path to the data directory")
var candidateNum = flag.Int("candidate-num", 100, "max candidates number")
var helpFlag = flag.Bool("h", false, "show this message")
var greedyFlag = flag.Bool("greedy", false, "show as many result as possible")
var greedyFlag = flag.Bool("Greedy", false, "show as many result as possible")

func main() {
if err := run(); err != nil {
Expand All @@ -35,10 +35,11 @@ func run() error {

// new nextword
params := &NextwordParams{
DataPath: *dataPath,
CandidateNum: *candidateNum,
Greedy: *greedyFlag,
}
nw, err := NewNextword(*dataPath, params)
nw, err := NewNextword(params)
if err != nil {
return err
}
Expand Down
54 changes: 29 additions & 25 deletions nextword.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ import (
// ReadLineBufSize is buffer size for Nextword.ReadLine
var ReadLineBufSize = 1024 // 1024 is fast (?)

// NextwordParams is a Nextword parameter.
type NextwordParams struct {
DataPath string
CandidateNum int // Number of candidates
Greedy bool // If true, Nextword suggests words from all n-gram data.
}

// Nextword suggests next English words.
type Nextword struct {
DataPath string
ReadLineBufSize int
Params *NextwordParams
params *NextwordParams
readLineBufSize int
}

// NewNextword returns new Nextword. If dataPath is not valid, err will be not nil.
func NewNextword(dataPath string, params *NextwordParams) (*Nextword, error) {
// dataPath
fi, err := os.Stat(dataPath)
// NewNextword returns new Nextword. If params is not valid, err will be not nil.
func NewNextword(params *NextwordParams) (*Nextword, error) {
// nil check
if params == nil {
return nil, errors.New("invalid params")
}

// DataPath
fi, err := os.Stat(params.DataPath)
if err != nil {
return nil, errors.New(`"NEXTWORD_DATA_PATH" environment variable is not set`)
}
Expand All @@ -32,13 +43,12 @@ func NewNextword(dataPath string, params *NextwordParams) (*Nextword, error) {

// candidate-num
if params.CandidateNum <= 0 {
return nil, errors.New("candidate-num must be positive intager")
return nil, errors.New("candidate-num must be a positive integer")
}

return &Nextword{
DataPath: dataPath,
ReadLineBufSize: ReadLineBufSize,
Params: params,
params: params,
readLineBufSize: ReadLineBufSize,
}, nil
}

Expand All @@ -63,10 +73,10 @@ func (nw *Nextword) Suggest(input string) (candidates []string, err error) {
candidates = nw.mergeCandidates(candidates, cand)

// end condition
if len(candidates) > nw.Params.CandidateNum {
candidates = candidates[:nw.Params.CandidateNum]
if len(candidates) > nw.params.CandidateNum {
candidates = candidates[:nw.params.CandidateNum]
}
if !nw.Params.Greedy && len(candidates) > 0 {
if !nw.params.Greedy && len(candidates) > 0 {
return
}
}
Expand All @@ -77,8 +87,8 @@ func (nw *Nextword) Suggest(input string) (candidates []string, err error) {
return
}
candidates = nw.mergeCandidates(candidates, cand)
if len(candidates) > nw.Params.CandidateNum {
candidates = candidates[:nw.Params.CandidateNum]
if len(candidates) > nw.params.CandidateNum {
candidates = candidates[:nw.params.CandidateNum]
}

return
Expand Down Expand Up @@ -116,7 +126,7 @@ func (nw *Nextword) searchNgram(ngram []string) (candidates []string, err error)
}

// open
path := filepath.Join(nw.DataPath, fname)
path := filepath.Join(nw.params.DataPath, fname)
f, err := os.Open(path)
if err != nil {
return
Expand Down Expand Up @@ -167,7 +177,7 @@ func (nw *Nextword) searchOneGram(prefix string) (candidates []string, err error
}

// open
path := filepath.Join(nw.DataPath, "1gram.txt")
path := filepath.Join(nw.params.DataPath, "1gram.txt")
f, err := os.Open(path)
if err != nil {
return
Expand Down Expand Up @@ -260,7 +270,7 @@ func (nw *Nextword) readLine(r io.ReaderAt, offset int64) (string, error) {
strBuilder := new(strings.Builder)

for {
buf := make([]byte, nw.ReadLineBufSize)
buf := make([]byte, nw.readLineBufSize)
n, err := r.ReadAt(buf, offset)
if err == io.EOF && n == 0 {
return "", err
Expand Down Expand Up @@ -322,9 +332,3 @@ func (*Nextword) filterCandidates(cand []string, prefix string) []string {

return res
}

// NextwordParams is a Nextword parameter.
type NextwordParams struct {
CandidateNum int // Number of candidates
Greedy bool // If true, Nextword suggests words from all n-gram data.
}
67 changes: 52 additions & 15 deletions nextword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,31 @@ func setNextwordTestDataPath() error {
return nil
}

func TestNextword(t *testing.T) {
func TestNewNextword(t *testing.T) {
tests := []struct {
path string
ok bool
params *NextwordParams
ok bool
}{
{NextwordTestDataPath, true},
{"", false},
{"/invalid/invalid/invalid/invalid/invalid/invalid", false},
{
&NextwordParams{NextwordTestDataPath, 10, false},
true,
},
{
&NextwordParams{"", 10, false},
false,
},
{
&NextwordParams{"/invalid/invalid/invalid/invalid/invalid/invalid", 10, false},
false,
},
{
&NextwordParams{NextwordTestDataPath, 0, false},
false,
},
}

for idx, test := range tests {
_, err := NewNextword(test.path, nil)
_, err := NewNextword(test.params)
if (err == nil) != test.ok {
t.Errorf("[%d] unexpected error: %v", idx, err)
}
Expand All @@ -55,6 +68,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"misera",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -66,6 +80,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"by thermodynamic ",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -77,6 +92,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"with the English a",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 5,
Greedy: false,
},
Expand All @@ -88,6 +104,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"young ",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -100,6 +117,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"the consumptions ",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: true,
},
Expand All @@ -111,6 +129,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"gur bjf ",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -122,6 +141,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"gurbjf",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -133,6 +153,7 @@ func TestNextword_Suggest(t *testing.T) {
{
"zzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzz",
&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
Greedy: false,
},
Expand All @@ -142,7 +163,7 @@ func TestNextword_Suggest(t *testing.T) {
}

for idx, test := range tests {
nw, err := NewNextword(NextwordTestDataPath, test.params)
nw, err := NewNextword(test.params)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -170,10 +191,11 @@ func BenchmarkNextword_Suggest(b *testing.B) {
}

params := &NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 100000,
Greedy: true,
}
nw, err := NewNextword(NextwordTestDataPath, params)
nw, err := NewNextword(params)
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -213,7 +235,10 @@ func TestNextword_ParseInput(t *testing.T) {
}

for idx, test := range tests {
nw, err := NewNextword(NextwordTestDataPath, nil)
nw, err := NewNextword(&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -259,7 +284,10 @@ func TestNextword_SearchNgram(t *testing.T) {
}

for idx, test := range tests {
nw, err := NewNextword(NextwordTestDataPath, nil)
nw, err := NewNextword(&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -308,7 +336,10 @@ func TestNextword_SearchOneGram(t *testing.T) {
}

for idx, test := range tests {
nw, err := NewNextword(NextwordTestDataPath, nil)
nw, err := NewNextword(&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -382,7 +413,10 @@ func TestNextword_BinarySearch(t *testing.T) {
}

for idx, test := range tests {
nw, err := NewNextword(NextwordTestDataPath, nil)
nw, err := NewNextword(&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -461,12 +495,15 @@ func TestNextword_ReadLine(t *testing.T) {

for idx, test := range tests {
func() {
nw, err := NewNextword(NextwordTestDataPath, nil)
nw, err := NewNextword(&NextwordParams{
DataPath: NextwordTestDataPath,
CandidateNum: 10,
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}

nw.ReadLineBufSize = test.bufSize
nw.readLineBufSize = test.bufSize

path := filepath.Join(NextwordTestDataPath, test.fname)
f, err := os.Open(path)
Expand Down

0 comments on commit 1241e71

Please sign in to comment.