-
Notifications
You must be signed in to change notification settings - Fork 0
/
petrovich.go
223 lines (179 loc) · 4.77 KB
/
petrovich.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
package petrovich
import (
"bytes"
"strings"
"unicode/utf8"
"github.com/yalosev/petrovich/rules"
)
//go:generate git submodule update --recursive --remote
//go:generate go run cmd/generator.go
//go:generate go fmt ./...
// Gender specifies Person`s gender - Male, Female, Androgynous
type Gender = rules.Gender
// Case specifies grammatical case (падежи)
type Case = rules.Case
const (
// Androgynous gender
Androgynous = rules.Androgynous
// Male gender
Male = rules.Male
// Female gender
Female = rules.Female
// cases
// Nominative именительный
Nominative = rules.Nominative
// Genitive родительный
Genitive = rules.Genitive
// Dative дательный
Dative = rules.Dative
// Accusative винительный
Accusative = rules.Accusative
// Instrumental творительный
Instrumental = rules.Instrumental
// Prepositional предложный
Prepositional = rules.Prepositional
)
// Person contains firstname, lastname, middlename and gender
type Person struct {
Gender Gender
FirstName string
MiddleName string
LastName string
}
// NewPerson returns Person. Grammary cases based on next sequence: gender -> middlename -> firstname
func NewPerson(lastname, firstname, middlename string, gender ...Gender) Person {
p := Person{
FirstName: firstname,
MiddleName: middlename,
LastName: lastname,
}
if len(gender) > 0 {
p.Gender = gender[0]
}
return p
}
// String returns full person`s name
func (p Person) String() string {
var buf bytes.Buffer
if len(p.LastName) > 0 {
buf.WriteString(strings.Title(p.LastName))
}
if len(p.FirstName) > 0 {
buf.WriteString(" ")
buf.WriteString(strings.Title(p.FirstName))
if len(p.MiddleName) > 0 {
buf.WriteString(" ")
buf.WriteString(strings.Title(p.MiddleName))
}
}
return strings.TrimSpace(buf.String())
}
// Short returns person`s lastname and firstname/middlename cutted to the first letter
func (p Person) Short() string {
var buf bytes.Buffer
if len(p.LastName) > 0 {
buf.WriteString(strings.Title(p.LastName))
}
if len(p.FirstName) > 0 {
buf.WriteString(" ")
buf.WriteString(string([]rune(strings.Title(p.FirstName))[:1]))
buf.WriteString(".")
if len(p.MiddleName) > 0 {
buf.WriteString(string([]rune(strings.Title(p.MiddleName))[:1]))
buf.WriteString(".")
}
}
return strings.TrimSpace(buf.String())
}
// Transform person credentials to a given grammatical case
func Transform(person Person, cas rules.Case) Person {
if person.Gender == 0 && (len(person.MiddleName) > 0 || len(person.FirstName) > 0) {
person.Gender = person.detectGender()
}
newPerson := Person{
Gender: person.Gender,
LastName: inflect(person.LastName, person.Gender, cas, rules.AllRules.Lastname),
MiddleName: inflect(person.MiddleName, person.Gender, cas, rules.AllRules.Middlename),
FirstName: inflect(person.FirstName, person.Gender, cas, rules.AllRules.Firstname),
}
return newPerson
}
func (p Person) detectGender() Gender {
gender := p.detectGenderByMiddlename()
if gender != Androgynous {
return gender
}
return p.detectGenderByName()
}
func (p Person) detectGenderByMiddlename() Gender {
if len(p.MiddleName) == 0 {
return Androgynous
}
ending := strings.ToLower(p.MiddleName)
if strings.HasSuffix(ending, "ич") {
return Male
} else if strings.HasSuffix(ending, "на") {
return Female
}
return Androgynous
}
func (p Person) detectGenderByName() Gender {
val, ok := rules.AllNames[strings.ToLower(p.FirstName)]
if !ok {
return Androgynous
}
return val
}
func inflect(name string, g Gender, c rules.Case, rg rules.RulesGroup) string {
if len(name) == 0 {
return ""
}
if c == rules.Nominative {
return name
}
ns := strings.Split(name, "-")
r := make([]string, len(ns))
for i, n := range ns {
if e := checkException(n, g, c, rg); e != "" {
r[i] = e
} else {
r[i] = find(n, g, c, rg)
}
}
return strings.Join(r, "-")
}
func find(name string, g Gender, c rules.Case, rg rules.RulesGroup) string {
for _, r := range rg.Suffixes {
if r.Gender == Androgynous || r.Gender == g {
for _, t := range r.Test {
if strings.HasSuffix(name, t) && len(r.Mods) > int(c) {
return applyRule(name, r.Mods[c])
}
}
}
}
return name
}
func checkException(name string, g Gender, c rules.Case, rg rules.RulesGroup) string {
ln := strings.ToLower(name)
for _, r := range rg.Exceptions {
if r.Gender == Androgynous || r.Gender == g {
for _, t := range r.Test {
if t == ln && len(r.Mods) > int(c) {
return applyRule(name, r.Mods[c])
}
}
}
}
return ""
}
func applyRule(name, mod string) string {
if mod == "." {
return name
}
if i := strings.LastIndex(mod, "-"); i >= 0 {
l := utf8.RuneCountInString(name) - i - 1
return string([]rune(name)[:l]) + mod[i+1:]
}
return name + mod
}