minks
minks

Reputation: 3029

How do I call the function to remove the suffix?

I want to pass each word inside the results to the stemm method to remove suffixes. However, on printing my 'final' list, I observe that the method isn't working on the words. Am I calling the function wrongly?

             results=[]
             with open('/Users/mnk/Documents/Stemtry.txt') as filer:
                 for line in filer:
                    results.append(line.strip().split())
             result=[]
             final=[]

             def stemm(n):
                 for suffix in ['ing', 'ly', 'ed', 'ious', 'ies', 'ive', 'es', 's', 'ment']:
                    if n.endswith(suffix):
                        return n[:-len(suffix)]
                    return n

             for eachitem in results[:10]:
                 for n in eachitem:
                     r=stemm(n)
                     final.append(r)
             print(final)               
             result.append(final)

Upvotes: 0

Views: 699

Answers (1)

cwahls
cwahls

Reputation: 753

De-indent line 12 ("return n") once. It'll then wait until all of the suffixes have been checked before returning the result. You can also use an else clause.

def stemm(n):
    for suffix in ['ing', 'ly', 'ed', 'ious', 'ies', 'ive', 'es', 's', 'ment']:
        if n.endswith(suffix):
            return n[:-len(suffix)]
    else:
        return n

Upvotes: 2

Related Questions