-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-freq-by-mecab.go
83 lines (80 loc) · 2.38 KB
/
count-freq-by-mecab.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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"github.com/Des-Nerger/go-astisub"
"github.com/asticode/go-astilog"
//"github.com/miiton/kanaconv"
"github.com/shogo82148/go-mecab"
)
func init() {
astilog.SetLogger(astilog.New( astilog.Configuration{Verbose: true} ))
}
func main() {
panicCheck := func(err error) {if err != nil {panic(err)}}
freq := map[/*[2]*/string]uint{}
var occurrencesTotal uint64
func() {
tagger, err := mecab.New(map[string]string{"eos-format": "\x00"}); panicCheck(err); defer tagger.Destroy()
filenames := []string(nil)
if len(os.Args) <= 1 {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
filenames = append(filenames, sc.Text())
}
} else {
filenames = os.Args[1:]
}
for _, filename := range filenames {
fmt.Fprintln(os.Stdout, filename)
subs, err := astisub.OpenFile(filename)
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
} ()
result, err := tagger.Parse(lineString); panicCheck(err)
for _, word := range strings.FieldsFunc(result, func(r rune)bool{return r=='\n'}) {
const sep = ",\t"
if strings.HasPrefix(word, sep) {continue}
fields := strings.FieldsFunc(word, func(r rune)bool{return strings.ContainsRune(sep, r)})
/*r:=fields[len(fields)-2]; if r=="*"{continue}; r=kanaconv.KatakanaToHiragana(r)
w, d, i := fields[0], fields[len(fields)-3], 0
for ; i<len(w) && i<len(d) && w[i]==d[i]; i++ {}
freq[[...]string{d, r[:len(r)-(len(w)-i)]+d[i:]}]++*/
freq[fields[len(fields)-3]]++
occurrencesTotal++
}
}
}
}
} ()
type pair struct{uint; s /*[2]*/string}
freqPairs := make([]pair, len(freq))
{
i := 0
for s, count := range freq {
freqPairs[i] = pair{uint:count, s:s}
i++
}
}
sort.Slice(freqPairs, func(i, j int) bool {return freqPairs[j].uint < freqPairs[i].uint})
for _, pair := range freqPairs {
fmt.Fprintf(os.Stderr, "%v\t%v"/*+"\t%v"*/+"\n", pair.uint, pair.s/*[0], pair.s[1]*/)
}
fmt.Fprintf(os.Stdout, "counted morphemes occurrences total: %v\n", occurrencesTotal)
}