-
Notifications
You must be signed in to change notification settings - Fork 0
/
verb.go
289 lines (241 loc) · 6.67 KB
/
verb.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package neng
import (
"slices"
"strings"
)
/* Returns gerund form of a verb. */
func gerund(verb string) string {
if slices.Contains([]string{"quiz", "up"}, verb) {
return doubleFinal(verb, "ing")
}
if len(verb) <= 2 {
return verb + "ing"
}
wi := getWordInfo(verb)
switch verb[len(verb)-1] {
case 'e':
switch verb[len(verb)-2] {
case 'u': // ue
return verb[:len(verb)-1] + "ing"
case 'i': // ie
return verb[:len(verb)-2] + "ying"
}
if strings.HasSuffix(wi.sequence, "cv") && !strings.HasSuffix(verb, "ye") && verb != "ante" {
// Remove final 'e' if previous letter is consonant other than 'y' and the verb is not 'ante'
return verb[:len(verb)-1] + "ing"
}
case 'y', 'h', 'w', 'x':
return verb + "ing"
case 'r':
return handleR(verb, "ing")
case 'l':
if strings.HasSuffix(wi.sequence, "vvc") {
return handleVVL(verb, "ing")
}
case 's':
if strings.HasSuffix(verb, "gas") {
// Double the ending of 'gas' and its derivatives
return doubleFinal(verb, "ing")
}
return verb + "ing"
}
if strings.HasSuffix(verb, "it") {
return handleIt(verb, "ing", wi)
}
if strings.HasSuffix(wi.sequence, "cvc") {
return handleCVC(verb, "ing", wi, []string{
"beget", "begin", "beset", "forget", "inset", "offset", "overrun",
"reset", "sublet", "typeset", "underrun", "upset",
})
}
return verb + "ing"
}
/*
Handles past tense and gerund transformations for verbs ending with consonant-vowel-consonant sequence. Takes a number of arguments:
- tenseEnding: '-ing' or '-ed'
- wi: wordInfo created during earlier processing steps
- tenseExceptions: tense-specific words whose endings are doubled, regardless of transformation rules based on syllable count and verb endings
*/
func handleCVC(verb, tenseEnding string, wi wordInfo, tenseExceptions []string) string {
if strings.HasSuffix(verb, "c") && verb != "sic" {
if strings.HasSuffix(verb, "lyric") {
return verb + tenseEnding
}
// If final letter is 'c', add 'k' before tenseEnding
return verb + "k" + tenseEnding
}
commonSingleExceptions := []string{
"batik", "kayak", "orphan", "pyramid", "wedel",
}
commonDoubledExceptions := []string{
"abet", "anagram", "curvet", "regret", "revet", "underpin", "unpin",
}
if slices.Contains(commonSingleExceptions, verb) {
return verb + tenseEnding
}
if slices.Contains(commonDoubledExceptions, verb) {
// Double the final consonant for exceptions that are common for past forms and gerund
return doubleFinal(verb, tenseEnding)
}
// nil-safe, omitting check
if slices.Contains(tenseExceptions, verb) {
// Double the final consonant for exceptions specific to the caller
return doubleFinal(verb, tenseEnding)
}
if strings.HasSuffix(verb, "l") {
return doubleFinal(verb, tenseEnding)
}
if wi.sylCount == 2 {
if endsWithAny(verb, []string{"en", "et", "in", "om", "on"}) {
// Do not double the final consonant of bisyllabic verbs with specific endings
return verb + tenseEnding
}
}
if wi.sylCount > 2 {
// Do not double the final consonant of verbs consisting of more than 2 syllables
return verb + tenseEnding
}
// Double the final consonant of any other verb
return doubleFinal(verb, tenseEnding)
}
/* Handles transformation of verbs ending with '-it'. */
func handleIt(verb, tenseEnding string, wi wordInfo) string {
if strings.HasSuffix(wi.sequence, "vvc") {
if strings.HasSuffix(verb, "quit") {
// The case of 'acquit' and 'quit'
return doubleFinal(verb, tenseEnding)
}
return verb + tenseEnding
}
if wi.sylCount == 1 {
return doubleFinal(verb, tenseEnding)
}
if endsWithAny(verb, []string{"fit", "mit", "wit"}) {
if !slices.Contains([]string{"limit", "profit"}, verb) {
return doubleFinal(verb, tenseEnding)
}
}
return verb + tenseEnding
}
/* Handles transformation of verbs ending with '-r'. */
func handleR(verb, tenseEnding string) string {
doubled := []string{
"abhor", "bar", "bestir", "blur", "bur", "char", "concur",
"confer", "debar", "demur", "deter", "disbar", "disinter",
"incur", "jar", "mar", "occur", "par", "prefer", "recur",
"refer", "scar", "slur", "spar", "spur", "star", "stir", "tar",
"transfer", "unbar", "war",
}
if slices.Contains(doubled, verb) {
return doubleFinal(verb, tenseEnding)
}
return verb + tenseEnding
}
/* Handles transformation of verbs ending with vowel-vowel-l sequence. */
func handleVVL(verb, tenseEnding string) string {
if strings.HasSuffix(verb, "uel") || slices.Contains([]string{"victual", "vitriol"}, verb) {
return doubleFinal(verb, tenseEnding)
}
return verb + tenseEnding
}
/* Returns Past Participle form of a verb. */
func pastParticiple(verb string, verbsIrr [][]string) string {
if verb == "be" {
return "been"
}
verbLine := findIrregular(verb, verbsIrr)
if verbLine != nil {
return verbLine[2]
}
return pastRegular(verb)
}
/* Appends past tense suffix to a regular verb. */
func pastRegular(verb string) string {
if slices.Contains([]string{"quiz", "up"}, verb) {
return doubleFinal(verb, "ed")
}
wi := getWordInfo(verb)
switch verb[len(verb)-1] {
case 'e', 'u':
return verb + "d"
case 'r':
return handleR(verb, "ed")
case 'h', 'w', 'x':
return verb + "ed"
case 'l':
if strings.HasSuffix(wi.sequence, "vvc") {
return handleVVL(verb, "ed")
}
case 'y':
if strings.HasSuffix(wi.sequence, "v") {
return verb[:len(verb)-1] + "ied"
}
return verb + "ed"
case 's':
if strings.HasSuffix(verb, "gas") {
// Double the ending of 'gas' and its derivatives
return doubleFinal(verb, "ed")
}
return verb + "ed"
case 'o', 'a', 'i':
return verb + "ed"
}
if strings.HasSuffix(verb, "it") {
return handleIt(verb, "ed", wi)
}
if strings.HasSuffix(wi.sequence, "cvc") {
return handleCVC(verb, "ed", wi, nil)
}
return verb + "ed"
}
/* Returns Past Simple form of a verb. */
func pastSimple(verb string, verbsIrr [][]string, plural bool) string {
if verb == "be" {
if plural {
return "were"
}
return "was"
}
verbLine := findIrregular(verb, verbsIrr)
if verbLine != nil {
return verbLine[1]
}
return pastRegular(verb)
}
/* Returns Present Simple form of a verb. */
func presentSimple(verb string, plural bool) string {
if plural {
if verb == "be" {
return "are"
}
return verb
}
switch verb {
case "be":
return "is"
case "have":
return "has"
}
seq := getSequence(verb)
switch verb[len(verb)-1] {
case 'y':
if strings.HasSuffix(seq, "v") {
return verb[:len(verb)-1] + "ies"
}
case 's', 'x':
return verb + "es"
case 'o':
if strings.HasSuffix(seq, "cv") {
return verb + "es"
}
case 'z':
if verb == "quiz" {
return doubleFinal(verb, "es")
}
return verb + "es"
}
if endsWithAny(verb, []string{"ch", "sh"}) {
return verb + "es"
}
return verb + "s"
}