package inflection
import (
)
type inflection struct {
regexp *regexp.Regexp
replace string
}
type Regular struct {
find string
replace string
}
type Irregular struct {
singular string
plural string
}
type RegularSlice []Regular
type IrregularSlice []Irregular
var pluralInflections = RegularSlice{
{"([a-z])$", "${1}s"},
{"s$", "s"},
{"^(ax|test)is$", "${1}es"},
{"(octop|vir)us$", "${1}i"},
{"(octop|vir)i$", "${1}i"},
{"(alias|status)$", "${1}es"},
{"(bu)s$", "${1}ses"},
{"(buffal|tomat)o$", "${1}oes"},
{"([ti])um$", "${1}a"},
{"([ti])a$", "${1}a"},
{"sis$", "ses"},
{"(?:([^f])fe|([lr])f)$", "${1}${2}ves"},
{"(hive)$", "${1}s"},
{"([^aeiouy]|qu)y$", "${1}ies"},
{"(x|ch|ss|sh)$", "${1}es"},
{"(matr|vert|ind)(?:ix|ex)$", "${1}ices"},
{"^(m|l)ouse$", "${1}ice"},
{"^(m|l)ice$", "${1}ice"},
{"^(ox)$", "${1}en"},
{"^(oxen)$", "${1}"},
{"(quiz)$", "${1}zes"},
}
var singularInflections = RegularSlice{
{"s$", ""},
{"(ss)$", "${1}"},
{"(n)ews$", "${1}ews"},
{"([ti])a$", "${1}um"},
{"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$", "${1}sis"},
{"(^analy)(sis|ses)$", "${1}sis"},
{"([^f])ves$", "${1}fe"},
{"(hive)s$", "${1}"},
{"(tive)s$", "${1}"},
{"([lr])ves$", "${1}f"},
{"([^aeiouy]|qu)ies$", "${1}y"},
{"(s)eries$", "${1}eries"},
{"(m)ovies$", "${1}ovie"},
{"(c)ookies$", "${1}ookie"},
{"(x|ch|ss|sh)es$", "${1}"},
{"^(m|l)ice$", "${1}ouse"},
{"(bus)(es)?$", "${1}"},
{"(o)es$", "${1}"},
{"(shoe)s$", "${1}"},
{"(cris|test)(is|es)$", "${1}is"},
{"^(a)x[ie]s$", "${1}xis"},
{"(octop|vir)(us|i)$", "${1}us"},
{"(alias|status)(es)?$", "${1}"},
{"^(ox)en", "${1}"},
{"(vert|ind)ices$", "${1}ex"},
{"(matr)ices$", "${1}ix"},
{"(quiz)zes$", "${1}"},
{"(database)s$", "${1}"},
}
var irregularInflections = IrregularSlice{
{"person", "people"},
{"man", "men"},
{"child", "children"},
{"sex", "sexes"},
{"move", "moves"},
{"mombie", "mombies"},
}
var uncountableInflections = []string{"equipment", "information", "rice", "money", "species", "series", "fish", "sheep", "jeans", "police"}
var compiledPluralMaps []inflection
var compiledSingularMaps []inflection
func () {
compiledPluralMaps = []inflection{}
compiledSingularMaps = []inflection{}
for , := range uncountableInflections {
:= inflection{
regexp: regexp.MustCompile("^(?i)(" + + ")$"),
replace: "${1}",
}
compiledPluralMaps = append(compiledPluralMaps, )
compiledSingularMaps = append(compiledSingularMaps, )
}
for , := range irregularInflections {
:= []inflection{
inflection{regexp: regexp.MustCompile(strings.ToUpper(.singular) + "$"), replace: strings.ToUpper(.plural)},
inflection{regexp: regexp.MustCompile(strings.Title(.singular) + "$"), replace: strings.Title(.plural)},
inflection{regexp: regexp.MustCompile(.singular + "$"), replace: .plural},
}
compiledPluralMaps = append(compiledPluralMaps, ...)
}
for , := range irregularInflections {
:= []inflection{
inflection{regexp: regexp.MustCompile(strings.ToUpper(.plural) + "$"), replace: strings.ToUpper(.singular)},
inflection{regexp: regexp.MustCompile(strings.Title(.plural) + "$"), replace: strings.Title(.singular)},
inflection{regexp: regexp.MustCompile(.plural + "$"), replace: .singular},
}
compiledSingularMaps = append(compiledSingularMaps, ...)
}
for := len(pluralInflections) - 1; >= 0; -- {
:= pluralInflections[]
:= []inflection{
inflection{regexp: regexp.MustCompile(strings.ToUpper(.find)), replace: strings.ToUpper(.replace)},
inflection{regexp: regexp.MustCompile(.find), replace: .replace},
inflection{regexp: regexp.MustCompile("(?i)" + .find), replace: .replace},
}
compiledPluralMaps = append(compiledPluralMaps, ...)
}
for := len(singularInflections) - 1; >= 0; -- {
:= singularInflections[]
:= []inflection{
inflection{regexp: regexp.MustCompile(strings.ToUpper(.find)), replace: strings.ToUpper(.replace)},
inflection{regexp: regexp.MustCompile(.find), replace: .replace},
inflection{regexp: regexp.MustCompile("(?i)" + .find), replace: .replace},
}
compiledSingularMaps = append(compiledSingularMaps, ...)
}
}
func () {
compile()
}
func (, string) {
pluralInflections = append(pluralInflections, Regular{, })
compile()
}
func (, string) {
singularInflections = append(singularInflections, Regular{, })
compile()
}
func (, string) {
irregularInflections = append(irregularInflections, Irregular{, })
compile()
}
func ( ...string) {
uncountableInflections = append(uncountableInflections, ...)
compile()
}
func () RegularSlice {
:= make(RegularSlice, len(pluralInflections))
copy(, pluralInflections)
return
}
func () RegularSlice {
:= make(RegularSlice, len(singularInflections))
copy(, singularInflections)
return
}
func () IrregularSlice {
:= make(IrregularSlice, len(irregularInflections))
copy(, irregularInflections)
return
}
func () []string {
:= make([]string, len(uncountableInflections))
copy(, uncountableInflections)
return
}
func ( RegularSlice) {
pluralInflections =
compile()
}
func ( RegularSlice) {
singularInflections =
compile()
}
func ( IrregularSlice) {
irregularInflections =
compile()
}
func ( []string) {
uncountableInflections =
compile()
}
func ( string) string {
for , := range compiledPluralMaps {
if .regexp.MatchString() {
return .regexp.ReplaceAllString(, .replace)
}
}
return
}
func ( string) string {
for , := range compiledSingularMaps {
if .regexp.MatchString() {
return .regexp.ReplaceAllString(, .replace)
}
}
return
}