0

Declaration: Ive searched high and low on SOF for an answer but I doesnt click with my problem.

attempting to create a condition that if the last two elements of an input argument = 'sh' or 'ch', append 'es' to the end.

elif word [-2:-1].lower() == 's' + 'h' or (word [-2:-1].lower() == 'c' + 'h'):      
     word = word + 'es'
     dictionary ['plural'] = word; dictionary ['status'] = 'success'  #dictionary format

the output im getting at the moment :

pouch --> {'plural': 'pouchs', 'status': 'success'}

This is taking place (i think) since I have a prior condition that any input argument that does not come from my file (containing a list of strings) should be pluralized with the letter 's'. This would be my last condition in my program:

elif word not in proper_nouns:                             
     word = word + 's'
     dictionary ['plural'] = word; dictionary ['status'] = 'success'

2 Answers2

1

You could try this code, which takes the last two characters of your string and compares them to 'ch' and 'sh'.

elif word [-2:].lower() == 'sh' or (word [-2:].lower() == 'ch'):      
     word = word + 'es'
Mitchell Olislagers
  • 1,758
  • 1
  • 4
  • 10
0

attempting to create a condition that if the last two elements of an input argument = 'sh' or 'ch', append 'es' to the end.

If you really need to implement this on your own, an elegant way IMHO would be:

import re
regex = re.compile(r"(ch|sh)$", re.IGNORECASE)

def add_es(singular):
    return re.sub(regex, r'\1es', singular)

# >>> add_es('church')
# 'churches'

If you are using it in a serious project, please consider nltk.

Hope it helps and feel free to ask any questions.

Daniel Deng
  • 272
  • 1
  • 7