'Reducing number of levels of indentation for plural stemming code in python
My code works fine and it exactly does what it is suppose to do. However, it has too many levels of indentation. I wonder if anyone here has anyway to reduce number of levels of indentation.
Below is my code. The section I am mostly concerned about is the plural section.
#Returns stemmed sentence
def pos(sentence):
if sentence == " ":
return " "
final_sentence=''
word_list=sentence.split(" ")
vowels=['a','e','i','o','u','y']
vowelFound=False
final_word=''
for word in word_list:
#Remove ownerships
if '\'s' in word:
final_word=word.replace('\'s',"")
elif 's\'' in word:
final_word=word.replace('s\'',"")
else:
final_word=word
#Remove Plurals
if final_word.endswith('s'):
if word[-2] in vowels:
for c in word:
if c in vowels:
vowelFound=True
print(vowelFound)
break
if vowelFound:
if not word.endswith('us') or word.endswith('ss'):
if word.endswith('sses'):
final_word=word.replace("sses","ss")
elif word.endswith('ies'):
final_word=word.replace('ies','i')
if len(final_word)<=2:
final_word=word
elif word[-2] not in vowels:
final_word=word.replace('s','')
else:
final_word=word
else:
final_word=word
#Remove past tense
elif word.endswith('ied'):
final_word=word.replace("ied","i")
if len(final_word)<=2:
final_word=word.replace("d","")
elif word.endswith('ed'):
final_word=word.replace("ed","")
#Remove adverbs
if word.endswith('ing'):
final_word=word.replace("ing","")
if len(final_word)<3:
final_word=word
if final_word.endswith('ly'):
final_word=final_word.replace("ly","")
#Remove adjectives
if final_word.endswith('er'):
final_word=final_word.replace("er","")
final_sentence+=final_word+" "
return final_sentence
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
