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

Commit

Permalink
add main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Nov 28, 2019
1 parent f7b3137 commit bc9fe71
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

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

// environmental variable
const nextwordDataPath = "NEXTWORD_DATA_PATH"

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

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
flag.Parse()

// help
if *helpFlag {
showHelpMessage()
return nil
}

// new nextword
params := &NextwordParams{
CandidateNum: *candidateNum,
Greedy: *greedyFlag,
}
nw, err := NewNextword(*dataPath, params)
if err != nil {
return err
}

// loop
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
candidates, err := nw.Suggest(sc.Text())
if err != nil {
return err
}
fmt.Println(strings.Join(candidates, " "))
}
if sc.Err() != nil {
return sc.Err()
}

return nil
}

func showHelpMessage() {
fmt.Fprintln(os.Stderr, "Nextword prints the most likely English words that follow the stdin sentence.")
fmt.Fprintln(os.Stderr)
flag.Usage()
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, `You need to install nextword-data and set "NEXTWORD_DATA_PATH" environment variable.`)
fmt.Fprintln(os.Stderr, `It available at https://github.com/high-moctane/nextword-data`)
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, `The result depends on whether the input string ends with a space character.`)
fmt.Fprintln(os.Stderr, `If the string does not end with a space, nextword will print candidate words which`)
fmt.Fprintln(os.Stderr, `begin the last word in the sentence.`)
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, `Example:`)
fmt.Fprintln(os.Stderr, ` input: "Alice was "`)
fmt.Fprintln(os.Stderr, ` output: "not a the in still born very so to beginning too at sitting ..."`)
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, ` input: "Alice w"`)
fmt.Fprintln(os.Stderr, ` output: "was would were went with will who wrote when wants ..."`)
}

0 comments on commit bc9fe71

Please sign in to comment.