-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgomamayo.go
More file actions
executable file
·121 lines (98 loc) · 2.09 KB
/
gomamayo.go
File metadata and controls
executable file
·121 lines (98 loc) · 2.09 KB
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 gomamayo
import (
"github.com/ikawaha/kagome-dict/uni"
"github.com/ikawaha/kagome/v2/tokenizer"
"github.com/jugesuke/gomamayo/pkg/nihongo"
)
type (
// Result has a result of IsDajare function.
Result struct {
// If it is Dajare, this field is True, else False.
IsGomamayo bool
Ary int // n項
Degree int // n次
Sentence string
Reading string
Tokens []string
}
GoMamayo struct {
*tokenizer.Tokenizer
readPos int
}
)
// Set dictionary. You must do this first.
// Set dictionary you like. You can use a Kagome Dictionary.
// https://github.com/ikawaha/kagome#dictionaries
func Init() (*GoMamayo, error) {
g := new(GoMamayo)
g.readPos = 6
dic := uni.Dict()
if t, err := tokenizer.New(dic, tokenizer.OmitBosEos()); err != nil {
return nil, err
} else {
g.Tokenizer = t
// g.Analyze("")
return g, nil
}
}
// Analyze checks if a sentence is Dajare.
func (g *GoMamayo) Analyze(s string) *Result {
s, _ = nihongo.Normalize(s, nihongo.WithNeologdn)
tokens := g.Tokenize(s)
r := new(Result)
for _, t := range tokens {
f := t.Features()
if len(f) <= g.readPos {
return r
}
r.Tokens = append(r.Tokens, f[g.readPos])
}
for position := 0; position < len(r.Tokens)-1; position += 1 {
former := r.Tokens[position]
formerMora := nihongo.DivideMora(former)
formerLen := len(formerMora)
later := r.Tokens[position+1]
laterMora := nihongo.DivideMora(later)
laterLen := len(laterMora)
deg := 0
isGomamayo := false
for maxDeg := 0; maxDeg <= min(formerLen, laterLen); maxDeg += 1 {
flag := true
for searchPos := 0; searchPos < maxDeg; searchPos += 1 {
if formerMora[formerLen-maxDeg+searchPos] == laterMora[searchPos] {
deg += 1
} else {
flag = false
deg = 0
break
}
}
if flag && deg != 0 {
isGomamayo = true
r.Degree = max(deg, r.Degree)
}
deg = 0
}
if isGomamayo {
r.Ary += 1
}
}
if r.Ary > 0 {
r.IsGomamayo = true
}
return r
}
func min(a, b int) int {
if a > b {
return b
} else {
return a
}
}
func max(a, b int) int {
if a < b {
return b
} else {
return a
}
}