Home > Software design >  Pluralize word in python
Pluralize word in python

Time:11-27

I'm writing a function that must pluralize the word. For word starting with capital Z -> Zulma my output should be

Zulma --> {'plural': 'Zulma', 'status': 'proper_noun'}

whereas mine is:

Zulma --> {'plural': 'Zulmas', 'status': 'success'}

There is a mistake in the code in last elif or is last else

In two attachments I enclose .txt file and all question.

question file

def pluralize(word):
  wordsStorage = []
  x = ''
  word_in_plural = ''

  with open("proper_nouns.txt", "r") as file:
    wordsStorage = file.readlines()
  file.close() 

  for i in range(len(wordsStorage)):
    wordsStorage[i] = wordsStorage[i][:-1] 

  if word == '':  
    x = 'empty_string'
    word_in_plural = 'plural'
    return  {word_in_plural: '', "status": x}
  elif word[-1] == 's':
    word_in_plural = word
    x = 'already_in_plural'
    return {word_in_plural: word, "status": x}
  elif word.lower() in wordsStorage:
    word_in_plural = word
    x =  'proper_noun' 
    return {word_in_plural: word, "status": x}    

  else:
    if word[-1] == 'a' or word[-1] == 'e' or word[-1] == 'i' or word[-1] == 'o' or word[-1] == 'u':
      word_in_plural = word   's'
    elif  word[-1] == 'y' and word[-2] != 'a' and word[-2] != 'o' and word[-2] != 'i' and word[-2] != 'e' and word[-2] != 'u':
      word_in_plural = word[:-1]   'ies'
    elif word[-1] == 'f':
      word_in_plural = word[:-1]   'ves'
    elif word[-2:] == 'sh' or word[-2:] == 'ch'  or word[-2:] == 'z':
      word_in_plural = word   'es'
    else:
      word_in_plural  = word   's' 
    x = "success"
  return {"plural": word_in_plural, "status": x}  

        

    #pass
TEST_CASES = """
Zulma""".split('\n')

if __name__ == '__main__':

  for test_noun in TEST_CASES:

    print(test_noun,'-->',pluralize(test_noun))
    print('----')

CodePudding user response:

I think it's enough to delete:

for i in range(len(wordsStorage)):
    wordsStorage[i] = wordsStorage[i][:-1] 

This loop overwrites every word within the wordsStorage with the word without its last letter. For example, zulma is converted to zulm.

Therefore word.lower() in wordsStorage is false, in case word contains "zulma", because "zulm" is in the list.

CodePudding user response:

https://drive.google.com/file/d/1E1iXVja-aUqmUhAFVOeTT5GjrbdM_bO4/view?usp=sharing

That's link for proper_nouns.txt file

  • Related