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
+15.4k Golang : Check if a string contains multiple sub-strings in []string?
+32.9k Golang : Call a function after some delay(time.Sleep and Tick)
+4.8k Unix/Linux/MacOSx : Get local IP address
+5k Javascript : How to replace HTML inside <div>?
+6.2k Golang : Null and nil value
+11.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+17.9k Golang : Delete duplicate items from a slice/array
+28.9k Golang : How to get HTTP request header information?
+6.2k Golang : Get environment variable
+49.5k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+9.1k Golang : Test a slice of integers for odd and even numbers
+8.7k Golang : Copy map(hash table) example