-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_perceptron.go
85 lines (70 loc) · 1.97 KB
/
train_perceptron.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
//go:build ignore
// +build ignore
package main
import (
"bufio"
"encoding/json"
"log"
"os"
sabadisambiguator "github.com/syou6162/saba_disambiguator/lib"
twitter2 "github.com/syou6162/saba_disambiguator/twitter"
)
var config *sabadisambiguator.Config
func parseLine(line string) (*twitter2.Tweet, error) {
var tweet twitter2.Tweet
err := json.Unmarshal([]byte(line), &tweet)
return &tweet, err
}
func readExamplesFromFile(fileName string, label sabadisambiguator.LabelType) (sabadisambiguator.Examples, error) {
var examples sabadisambiguator.Examples
fp, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
text := scanner.Text()
t, err := parseLine(text)
if err != nil || t.ID == "" {
continue
}
e := sabadisambiguator.NewExampleWithOptions(t, label, sabadisambiguator.ExtractOptions{
ScreenNames: config.ScreenNames,
})
examples = append(examples, e)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return examples, nil
}
func loadConfig(file string) (*sabadisambiguator.Config, error) {
c, err := sabadisambiguator.GetConfigFromFile(file)
if err != nil {
if os.IsNotExist(err) {
return &sabadisambiguator.Config{}, nil
}
return nil, err
}
return c, nil
}
func main() {
log.SetFlags(0)
c, err := loadConfig("functions/saba_disambiguator/build/config.yml")
if err != nil {
log.Fatalf("failed to load config: %v\n", err)
}
config = c
examplesPos, err := readExamplesFromFile(os.Args[1], sabadisambiguator.POSITIVE)
if err != nil {
log.Fatalf("failed to read %s: %v\n", os.Args[1], err)
}
examplesNeg, err := readExamplesFromFile(os.Args[2], sabadisambiguator.NEGATIVE)
if err != nil {
log.Fatalf("failed to read %s: %v\n", os.Args[2], err)
}
examples := append(examplesPos, examplesNeg...)
p := sabadisambiguator.NewPerceptronClassifier(examples)
sabadisambiguator.WritePerceptron(*p, "functions/saba_disambiguator/build/model.bin")
}