-
Notifications
You must be signed in to change notification settings - Fork 3
/
identify.go
191 lines (171 loc) · 4.61 KB
/
identify.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package japanese
import "strings"
// possible verb forms
const (
TeForm = iota
Infinitive
PresentIndicative
Presumptive
Imperative
PastIndicative
PastPresumptive
PresentProgressive
PastProgressive
Provisional
Conditional
Potential
Causative
Unknown
)
// politeness levels
const (
Plain = iota
Polite
)
// positive / negative
const (
Positive = iota
Negative
)
// verb classes
const (
Ichidan = iota
Godan
Irregular
)
// verb endings
const (
Te = "て"
Teiru = "ている"
Teimasu = "ています"
Teru = "てる"
Temasu = "てます"
Teita = "ていた"
Teimashita = "ていました"
Teta = "てた"
Temashita = "てました"
Ru = "る"
U = "う"
Masu = "ます"
Nai = "ない"
Masen = "ません"
Ta = "た"
Mashita = "ました"
Katta = "かった"
Nakatta = "なかった"
MasenDeshita = "ませんでした"
You = "よう"
Darou = "だろう"
Naidarou = "ないだろう"
Mashou = "ましょう"
Deshou = "でしょう"
Naideshou = "ないでしょう"
Ro = "ろ"
Kudasai = "ください" // te-kudasai
Naidekudasai = "ないでください"
Na = "な"
Tarou = "たろう"
Tadarou = "ただろう"
Nakattadarou = "なかっただろう"
Tadeshou = "たでしょう"
Nakattadeshou = "なかったでしょう"
Reba = "れば"
Eba = "えば"
Keba = "けば"
Geba = "げば"
Seba = "せば"
Teba = "てば"
Neba = "ねば"
Beba = "べば"
Meba = "めば"
Kereba = "ければ"
Deareba = "であれば"
Nakereba = "なければ"
)
type FormEnding struct {
form int
endings []string
}
var (
PresentIndicativeEnding = FormEnding{PresentIndicative, []string{Ru, U, Masu, Nai, Masen}}
PresumptiveEnding = FormEnding{Presumptive, []string{You, Darou, Naidarou, Mashou, Deshou, Naideshou}}
ImperativeEnding = FormEnding{Imperative, []string{Ro, Kudasai, Naidekudasai, Na}}
PastIndicativeEnding = FormEnding{PastIndicative, []string{Ta, Mashita, Katta, Nakatta, MasenDeshita}}
PastPresumptiveEnding = FormEnding{PastPresumptive, []string{Tarou, Tadarou, Nakattadarou, Tadeshou, Nakattadeshou}}
PresentProgressiveEnding = FormEnding{PresentProgressive, []string{Teiru, Teimasu, Teru, Temasu}}
PastProgressiveEnding = FormEnding{PastProgressive, []string{Teita, Teimashita, Teta, Temashita}}
ProvisionalEnding = FormEnding{Provisional, []string{Reba, Eba, Keba, Geba, Seba, Teba, Neba, Beba, Meba, Kereba, Deareba, Nakereba}}
ConditionalEnding = FormEnding{}
PotentialEnding = FormEnding{}
CausativeEnding = FormEnding{}
InfinitiveEnding = FormEnding{}
TeFormEnding = FormEnding{TeForm, []string{Te}}
)
var EndingForms = []FormEnding{
TeFormEnding,
InfinitiveEnding,
PresentIndicativeEnding,
PresumptiveEnding,
ImperativeEnding,
PastIndicativeEnding,
PastPresumptiveEnding,
PresentProgressiveEnding,
PastProgressiveEnding,
ProvisionalEnding,
ConditionalEnding,
PotentialEnding,
CausativeEnding,
}
// hasAnySuffix
func hasAnySuffix(s string, endings ...string) bool {
for _, e := range endings {
if strings.HasSuffix(s, e) {
return true
}
}
return false
}
// IdentifyEnding gets the longest ending for the verb
// from the list of known verb endings.
func IdentifyEnding(verb string) (ending string) {
// Should make this a trie at some point, but... MVP
for _, ef := range EndingForms {
for _, e := range ef.endings {
if strings.HasSuffix(verb, e) && len(e) > len(ending) {
ending = e
}
}
}
return ending
}
// IdentifyForm tries to identify the verb form.
// It returns the integer form constant
func IdentifyForm(verb string) (form int) {
switch {
case hasAnySuffix(verb, Teiru, Teimasu, Teru, Temasu):
return PresentProgressive
case hasAnySuffix(verb, Teita, Teimashita, Teta, Temashita):
return PastProgressive
case hasAnySuffix(verb, Ru, U, Masu, Nai, Masen):
return PresentIndicative
case hasAnySuffix(verb, Ta, Mashita, Nakatta, MasenDeshita):
return PastIndicative
case hasAnySuffix(verb, Reba, Eba, Keba, Geba, Seba, Teba, Neba, Beba, Meba, Kereba, Deareba, Nakereba):
return Provisional
default:
return Unknown
}
}
// IdentifyPositivity determines whether the verb is
// in affirmative or negative form.
func IdentifyPositivity(verb string) (positivity int) {
if hasAnySuffix(verb, Nai, Masen, Nakatta, MasenDeshita) {
return Negative
}
return Positive
}
// // IdentifyGroup tries to identify the verb class
// func IdentifyGroup(verb string) (class int) {
// // TODO
// return class
// }