ID:195103
 
//Title: Pluralisation
//Credit to: Jtgibson
//Contributed by: Jtgibson


/*
Finds the appropriate plural for the given word.
If no match is found in its dictionary, it makes an
attempt to create one based on its own knowledge of
how plurals work.

If this is incorrect, it is up to you to add the word
and its proper plural to the plural dictionary.

(Note that plural() can also be used to conjugate
verbs in the present tense -- get or gets, eat or
eats, etc. Some words are exceptions; as with
actual exemptive plurals, you need to add them to
the dictionary manually.)
*/




var/list/plural_dictionary
var/list/deplural_dictionary

world/New()
var/savefile/F = new ("plural_dictionary.sav")
F[""] >> plural_dictionary
if(!plural_dictionary) plural_dictionary = list()
F["deplural"] >> deplural_dictionary
if(!deplural_dictionary) deplural_dictionary = list()
. = ..()

world/Del()
var/savefile/F = new ("plural_dictionary.sav")
F[""] << plural_dictionary
if(!plural_dictionary) plural_dictionary = list()
..()


proc/plural(word)
if(plural_dictionary.Find(word))
return plural_dictionary[word]

//If there is no dictionary entry for this plural string,
// fake it.

var/new_word = ""

var/string_len = lentext(word)
if(!string_len) return null

var/last_three_chars = ""
if(string_len >= 3) last_three_chars = copytext(word, string_len-2)
var/last_two_chars = ""
if(string_len >= 2) last_two_chars = copytext(word, string_len-1)
var/last_char
if(string_len) last_char = copytext(word, string_len)

if(string_len >= 3)
var/string_to_three = copytext(word, 1, string_len-2)
switch(last_three_chars)
if("gus") new_word = word
if("man") new_word = string_to_three + "men"
if("eaf") new_word = copytext(word, 1, string_len) + "ves"

if(!new_word && string_len >= 2)
var/string_to_two = copytext(word, 1, string_len-1)
switch(last_two_chars)
if("sh","ch") new_word = word + "es"
if("ey","ay","oy","uy") new_word = word + "s"
if("fe") new_word = string_to_two + "ves"

if(!new_word)
var/string_to_last = copytext(word, 1, string_len)
switch(last_char)
if("x","s") new_word = word + "es"
if("y") new_word = string_to_last + "ies"
else new_word = word + "s"

AddPlural(word, new_word)
return new_word


proc/deplural(plural)
if(deplural_dictionary.Find(plural))
//world << "DBG: Matching plural"
return deplural_dictionary[plural]

var/new_word = ""

var/string_len = lentext(plural)
if(!string_len)
//world << "DBG: String has no length"
return null

var/last_four_chars = ""
if(string_len >= 4) last_four_chars = copytext(plural, string_len-3)

var/last_three_chars = ""
if(string_len >= 3) last_three_chars = copytext(plural, string_len-2)

if(string_len >= 4)
var/string_to_four = copytext(plural, 1, string_len-3)
switch(last_four_chars)
if("shes") new_word = string_to_four + "sh"
if("ches") new_word = string_to_four + "ch"
if("aves") new_word = string_to_four + "af"

if(!new_word && string_len >= 3)
var/string_to_three = copytext(plural, 1, string_len-2)
switch(last_three_chars)
if("gus") new_word = plural
if("men") new_word = string_to_three + "man"
if("xes") new_word = string_to_three + "x"
if("ses") new_word = string_to_three + "s"
if("eys") new_word = string_to_three + "ey"
if("ays") new_word = string_to_three + "ay"
if("oys") new_word = string_to_three + "oy"
if("uys") new_word = string_to_three + "uy"
if("ies") new_word = string_to_three + "y"
//world << "DBG: [new_word]"

if(!new_word)
new_word = copytext(plural, 1, string_len)
//world << "DBG: [new_word]"

AddPlural(new_word, plural)
return new_word


proc/AddPlural(string, plural)
plural_dictionary[string] = plural
deplural_dictionary[plural] = string

proc/RemovePlural(string)
plural_dictionary -= string
deplural_dictionary -= string



///*
//Testing code/sample implementation:

mob/verb/testplural(word as text)
set hidden = 1
usr << plural(word)

mob/verb/addplural(word as text, association as text)
set hidden = 1
set desc = "(word, plural)"
usr << "Added [word] -> [association] to plurals."
AddPlural(word, association)

mob/verb/viewplurals()
set hidden = 1
usr << "Listing plurals:"
for(var/V in plural_dictionary)
usr << "\t[V] -> [plural_dictionary[V]]"
usr << "Total Plurals: [plural_dictionary.len]"

mob/verb/removeplural(word in plural_dictionary)
set hidden = 1
RemovePlural(word)

mob/verb/testdeplural(word as text)
set hidden = 1
usr << deplural(word)

//*/