-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
72 lines (54 loc) · 1.48 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
// Copyright 2016 The Citar Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/gob"
"flag"
"fmt"
"io"
"os"
"github.com/danieldk/citar/cmd/common"
"github.com/danieldk/citar/model"
"github.com/danieldk/conllx"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] config input.conllx\n\n", os.Args[0])
flag.PrintDefaults()
}
}
var closedClassFilename = flag.String("closed-class", "", "file with closed-class tags")
func main() {
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
os.Exit(1)
}
config := common.MustParseConfig(flag.Arg(0))
closedClass := common.MustLoadClosedClass(*closedClassFilename)
f, err := os.Open(flag.Arg(1))
common.ExitIfError("Cannot open training data", err)
defer f.Close()
out, err := os.Create(config.Model)
common.ExitIfError("Cannot open model for writing", err)
defer out.Close()
bufOut := bufio.NewWriter(out)
defer bufOut.Flush()
reader := conllx.NewReader(bufio.NewReader(f))
fc := model.NewFrequencyCollector()
for {
sent, err := reader.ReadSentence()
if err == io.EOF {
break
}
common.ExitIfError("Cannot read sentence", err)
err = fc.Process(sent)
common.ExitIfError("Cannot process sentence", err)
}
model := fc.ModelWithClosedClass(closedClass)
enc := gob.NewEncoder(bufOut)
err = enc.Encode(model)
common.ExitIfError("Cannot encode model", err)
}