Golang : Takes a plural word and makes it singular



Tags : golang inflector countable plural singular item-in-slice

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



Tags : golang inflector countable plural singular item-in-slice

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