Skip to content Skip to sidebar Skip to footer

Porter Stemmer Algorithm Not Returning The Expected Output? When Modified Into Def

I'm using the PorterStemmer Python Port The Porter stemming algorithm (or ‘Porter stemmer’) is a process for removing the commoner morphological and inflexional endings from

Solution 1:

So it looks like the culprit is that it doesn't currently write the final part of the input to output (try "Michael is Singing stuff", for example - it should write everything correctly and omit 'stuff'). There is likely a more elegant way to handle this, but one thing you could try is adding an else clause to the for loop. Since the issue is that the final word is not being included in output, we can use else to make sure that the final word gets added upon the completion of the for loop:

def algorithm(input):
    print input
    p = PorterStemmer()
    while 1:
        output = ''
        word = ''
        if input == '':
            break
        for c in input:
            if c.isalpha():
                word += c.lower()
            elif word:
                output += p.stem(word, 0,len(word)-1)
                word = ''
                output += c.lower()
        else:
            output += p.stem(word, 0, len(word)-1)  
        print output
        return output

This has been extensively tested with two test cases, so clearly it is bulletproof :) There are probably some edge cases crawling around there, but hopefully it will get you started.


Post a Comment for "Porter Stemmer Algorithm Not Returning The Expected Output? When Modified Into Def"