Golang : Takes a plural word and makes it singular
Here is a program that will convert a plural word into its singular form and at the same time it will check to see if the word is countable or not. If the word pluralized form is the same as the singular, the program's functions will avoid singularizing the given word.
By all means, the maps are not complete. Feel free to add in the missing words.
Can be useful in enhancing chat bot or simply just to follow the rule of grammar.
NOTES: This program purposely avoided using regular expression.
Here you go!
package main
import (
"fmt"
"strings"
)
func Singular(input string) string {
if !IsCountable(input) {
return input
}
var singularDictionary = map[string]string{
"are": "is",
"analyses": "analysis",
"alumni": "alumnus",
"aliases": "alias",
"axes": "axis",
//"alumni": "alumnae", // for female - cannot have duplicate in map
"genii": "genius",
"data": "datum",
"atlases": "atlas",
"appendices": "appendix",
"barracks": "barrack",
"beefs": "beef",
"buses": "bus",
"brothers": "brother",
"cafes": "cafe",
"corpuses": "corpus",
"campuses": "campus",
"cows": "cow",
"crises": "crisis",
"ganglions": "ganglion",
"genera": "genus",
"graffiti": "graffito",
"loaves": "loaf",
"matrices": "matrix",
"monies": "money",
"mongooses": "mongoose",
"moves": "move",
"movies": "movie",
"mythoi": "mythos",
"lice": "louse",
"niches": "niche",
"numina": "numen",
"octopuses": "octopus",
"opuses": "opus",
"oxen": "ox",
"penises": "penis",
"vaginas": "vagina",
"vertices": "vertex",
"viruses": "virus",
"shoes": "shoe",
"sexes": "sex",
"testes": "testis",
"turfs": "turf",
"teeth": "tooth",
"feet": "foot",
"cacti": "cactus",
"children": "child",
"criteria": "criterion",
"news": "news",
"deer": "deer",
"echoes": "echo",
"elves": "elf",
"embargoes": "embargo",
"foes": "foe",
"foci": "focus",
"fungi": "fungus",
"geese": "goose",
"heroes": "hero",
"hooves": "hoof",
"indices": "index",
"knifes": "knife",
"leaves": "leaf",
"lives": "life",
"men": "man",
"mice": "mouse",
"nuclei": "nucleus",
"people": "person",
"phenomena": "phenomenon",
"potatoes": "potato",
"selves": "self",
"syllabi": "syllabus",
"tomatoes": "tomato",
"torpedoes": "torpedo",
"vetoes": "veto",
"women": "woman",
"zeroes": "zero",
"natives": "native",
"hives": "hive",
"quizzes": "quiz",
"bases": "basis",
"diagnostic": "diagnosis",
"parentheses": "parenthesis",
"prognoses": "prognosis",
"synopses": "synopsis",
"theses": "thesis",
}
result := singularDictionary[strings.ToLower(input)]
if result == "" {
// to handle words like apples, doors, cats
if len(input) > 2 {
if string(input[len(input)-1]) == "s" {
return string(input[:len(input)-1])
}
}
return input
} else {
return result
}
}
func IsCountable(input string) bool {
// dictionary of word that has no plural version
toCheck := strings.ToLower(input)
var nonCountable = []string{
"audio",
"bison",
"chassis",
"compensation",
"coreopsis",
"data",
"deer",
"education",
"emoji",
"equipment",
"fish",
"furniture",
"gold",
"information",
"knowledge",
"love",
"rain",
"money",
"moose",
"nutrition",
"offspring",
"plankton",
"pokemon",
"police",
"rice",
"series",
"sheep",
"species",
"swine",
"traffic",
"wheat",
}
for _, v := range nonCountable {
if toCheck == v {
return false
}
}
return true
}
func main() {
fmt.Println("Are :", Singular("Are"))
fmt.Println("Equipment :", Singular("Equipment"))
fmt.Println("is :", Singular("is"))
fmt.Println("Apples :", Singular("Apples"))
fmt.Println("oranges :", Singular("oranges"))
fmt.Println("bases :", Singular("bases"))
fmt.Println("Doors :", Singular("Doors"))
}
Output:
Are : is
Equipment : Equipment
is : is
Apples : Apple
oranges : orange
bases : basis
Doors : Door
See also : Golang : Check if a word is countable or not
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12k Elastic Search : Return all records (higher than default 10)
+9k Golang : How to protect your source code from client, hosting company or hacker?
+21.4k Golang : Join arrays or slices example
+15.5k Golang : How to reverse elements order in map ?
+5.3k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+7.3k Golang : Getting Echo framework StartAutoTLS to work
+11.8k Golang : Get month name from date example
+8.6k Golang : Go as a script or running go with shebang/hashbang style
+21.1k SSL : How to check if current certificate is sha1 or sha2
+28.6k Golang : Get first few and last few characters from string
+8.5k Golang : Handle sub domain with Gin
+9.7k CodeIgniter : Load different view for mobile devices