forked from jdkato/prose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (63 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/jdkato/prose/tag"
"github.com/urfave/cli"
)
// Version is the semantic version number
var Version string
func main() {
var file string
var text []byte
var err error
app := cli.NewApp()
app.Name = "aptag"
app.Usage = "A command-line POS tagger (for testing purposes only!)"
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "path",
Usage: "read `path` as source input instead of stdin",
Destination: &file,
},
}
app.Action = func(c *cli.Context) error {
if file != "" {
text, err = ioutil.ReadFile(file)
if err != nil {
panic(err)
}
} else {
stat, serr := os.Stdin.Stat()
if serr != nil {
panic(err)
} else if (stat.Mode() & os.ModeCharDevice) == 0 {
reader := bufio.NewReader(os.Stdin)
text, err = ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
}
}
if len(text) > 0 {
tagger := tag.NewPerceptronTagger()
tags := tagger.Tag(strings.Split(string(text), " "))
b, jerr := json.Marshal(tags)
if jerr != nil {
return jerr
}
fmt.Println(string(b))
}
return err
}
if app.Run(os.Args) != nil {
os.Exit(1)
} else {
os.Exit(0)
}
}