/
grep-morphemes.go
51 lines (48 loc) · 1.09 KB
/
grep-morphemes.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
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/Des-Nerger/go-astisub"
"github.com/asticode/go-astilog"
)
func init() {
astilog.SetLogger(astilog.New( astilog.Configuration{Verbose: true} ))
}
func main() {
re := regexp.MustCompile(os.Args[1])
j := Jumanpp{Path: "/opt/jumanpp/bin/jumanpp"}
j.Start()
defer j.Wait()
for _, arg := range os.Args[2:] {
subs, err := astisub.OpenFile(arg)
if err != nil {
fmt.Fprintln(os.Stdout, err)
continue
}
for _, item := range subs.Items {
for _, line := range item.Lines {
lineString := func() string {
if len(line.Items) > 1 {
var texts []string
for _, lineItem := range line.Items {
texts = append(texts, lineItem.Text)
}
return strings.Join(texts, "")
}
return line.Items[0].Text
} ()
for _, morpheme := range j.AnalyzeLine(lineString) {
switch morpheme[2] {
case "特殊", "未定義語", "0": // Do nothing
default:
if re.MatchString(morpheme[0]) {
fmt.Fprintf(os.Stdout, "%v:\n%v\n\n", arg, lineString)
}
}
}
}
}
}
}