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
+11.4k Golang : How to determine a prime number?
+7.8k Android Studio : AlertDialog to get user attention example
+14.9k Golang : Send email with attachment(RFC2822) using Gmail API example
+18.6k Golang : Read binary file into memory
+7.3k Golang : Array mapping with Interface
+8.8k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+5.6k Golang : What is StructTag and how to get StructTag's value?
+7.9k Golang : How to execute code at certain day, hour and minute?
+18.8k Golang : Get download file size
+4.8k Chrome : How to block socketloop.com links in Google SERP?
+7k Mac/Linux/Windows : Get CPU information from command line
+14.2k Golang : convert rune to unicode hexadecimal value and back to rune character