You need to split the text into words for the stemmer to work. Currently, the variable text
contains the whole file as one big string. The loop for plural in text:
assigns each character in text
to plural
.
Try for plural in text.split():
instead.
[EDIT] To get the output in the format you want, you need to read the file line by line instead of reading it all at once:
def stemming_text_1():
with open('test.txt', 'r') as f:
for line in f:
print line
singles = []
stemmer = PorterStemmer() #problem from HERE
for plural in line.split():
singles.append(stemmer.stem(plural))
print ' '.join(singles)