I have a series of conditions that pluralise words. Conditions for if they are found in the file, and conditions if they are not found in the file. I have (I think) indentified my problem but unsure how to fix it. I'm sure its related to how im slicing strings to match conditions.
I believe you can read all prior to else:
so I will jump ahead.
all following else
aims to in order of:
**if word not in plural, and not a proper noun, then: if word ends with vowel, add 's'. Otherwise;
-if ends with 'y' and is preceded by a consonant, erase the last letter and add 'ies'.
-if ends with 'f', erase the last letter and add 'ves'
-if ends with 'sh'/'ch'/'z', add 'es'
-if none of the above applies, just add 's'.**
logic must follow the order above and cannot deviate.
def pluralize(word):
proper_nouns= [line.strip() for line in open (filepath)] ### opens file when function is called, removes white spaces, and returns a list of the content within the file.
word_in_plural = '' ### placeholders for string values.
x = ''
vowels = ('aeiou') ### variables to apply conditional statements
consonants = ('bcdfghjklmnpqrstvwxyz')
dictionary = {'plural' : word_in_plural, 'status' : x} ### dictionary definition
if word == '':
dictionary ['plural'] = ''; dictionary ['status'] = 'empty_string'
elif word [-1] == 's':
dictionary ['plural'] = word; dictionary ['status'] = 'already_in_plural'
elif word.lower() in proper_nouns:
dictionary ['plural'] = word; dictionary ['status'] = 'proper_noun'
else:
if word [-1].lower() in vowels: #works
word = word + 's'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-2].lower() in consonants and word[-1] == 'y': #works
word = word [:-1] + 'ies'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-1].lower() == 'f': #works
word = word [:-1] + 'ves'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [-1:-2].lower() == 's' + 'h' or 'c' + 'h': #does not
word = word + 'es'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word [:-1].lower() == 'z': #works
word = word + 'es'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
elif word not in proper_nouns.lower(): #not sure
word = word + 's'
dictionary ['plural'] = word; dictionary ['status'] = 'success'
output:
failure --> {'plural': 'failures', 'status': 'success'}
----
food --> {'plural': 'foodes', 'status': 'success'}
----
Zulma --> {'plural': 'Zulma', 'status': 'proper_noun'}
----
injury --> {'plural': 'injuries', 'status': 'success'}
----
elf --> {'plural': 'elves', 'status': 'success'}
----
buzz --> {'plural': 'buzzes', 'status': 'success'}
----
computers --> {'plural': 'computers', 'status': 'already_in_plural'}
----
PCs --> {'plural': 'PCs', 'status': 'already_in_plural'}
----
--> {'plural': '', 'status': 'empty_string'}
----
highway --> {'plural': 'highwayes', 'status': 'success'}
----
presentation --> {'plural': 'presentationes', 'status': 'success'}
----
pouch --> {'plural': 'pouches', 'status': 'success'}
----
COVID-19 --> {'plural': 'COVID-19es', 'status': 'success'}
----
adam --> {'plural': 'adam', 'status': 'proper_noun'}
food , highways, presentations and covid-19 is wrong. They are not in the file so should end with 's'