-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (106 loc) · 2.65 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path"
"strings"
)
func morph() {
fmt.Println("building morphology base...")
dir := getRootDir()
if err := BuildMorph(
path.Join(dir, "dict.opcorpora.txt"),
path.Join(dir, "morph.bin")); err != nil {
fmt.Printf("failed to build morphology base: %s\n", err)
return
}
fmt.Println("done")
}
func corpus() {
fmt.Println("building text corpus...")
dir := getRootDir()
if err := BuildCorpus(
path.Join(dir, "annot.opcorpora.xml"),
path.Join(dir, "corpus.txt")); err != nil {
fmt.Printf("failed to build text corpus: %s\n", err)
return
}
fmt.Println("done")
}
func nextSentence(index int, sentence string,
matches []ParseMatch, verbose bool) {
if len(matches) > 0 {
fmt.Printf("+%d: %s\n", index, sentence)
if verbose {
json, err := json.Marshal(matches)
if err != nil {
fmt.Printf("failed to marshal parse matches: %s\n", err)
return
}
fmt.Printf("%s\n\n", json)
}
} else {
fmt.Printf("-%d: %s\n", index, sentence)
}
}
func nextStats(succeeded, failed int) {
fmt.Printf("succeeded: %d, failed: %d\n", succeeded, failed)
}
func parse(from, to int, save, verbose bool) {
dir := getRootDir()
if err := ParseCorpus(path.Join(dir, "corpus.txt"),
from, to, path.Join(dir, "corparse.rec"), save, verbose,
nextSentence, nextStats); err != nil {
fmt.Println(err)
}
}
func test(nonterminal, text string) {
if err := initParser(); err != nil {
fmt.Printf("failed to initialize parser: %s\n", err)
}
defer finalizeParser()
matches := Parse(strings.ToLower(text), nonterminal, 0)
json, err := json.Marshal(matches)
if err != nil {
fmt.Printf("failed to marshal parse matches: %s\n", err)
return
}
fmt.Printf("%s\n", json)
}
func printUsage() {
fmt.Printf("usage: idiot (morph | corpus | parse | test)\n")
}
func main() {
if len(os.Args) == 1 {
printUsage()
return
}
switch os.Args[1] {
case "morph":
morph()
return
case "corpus":
corpus()
return
case "parse":
parseFlags := flag.NewFlagSet("parse", flag.ExitOnError)
from := parseFlags.Int("from", 0, "begin of sentence interval")
to := parseFlags.Int("to", 1000000, "end of sentence interval")
save := parseFlags.Bool("save", false, "save result changes")
verbose := parseFlags.Bool("verbose", false, "verbose output")
parseFlags.Parse(os.Args[2:])
parse(*from, *to, *save, *verbose)
return
case "test":
testFlags := flag.NewFlagSet("test", flag.ExitOnError)
nonterm := testFlags.String("nonterm",
"sentence", "non-terminal to parse against")
text := testFlags.String("text", "", "text to parse")
testFlags.Parse(os.Args[2:])
test(*nonterm, *text)
default:
printUsage()
}
}