File: main.go

package info (click to toggle)
golang-github-jdkato-prose 1.1.0%2Bgit20171031.e27abfd-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,848 kB
  • sloc: python: 115; makefile: 55; sh: 21
file content (70 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download
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)
	}
}