The issues with your functions are the followig:
- Dictionary key-value are swapped.
Workaround: Try https://stackoverflow.com/a/1031878/4954434
- The
append() function by default will append to end of the list.
Thus position is not taken care properly.
temp does not have all the words.
The following function should work.
def stemmingWords(sentence, dictionary):
dictionary = dict((v,k) for k,v in dictionary.iteritems())
splitted = sentence.split()
for i in range(len(splitted)):
if splitted[i] in dictionary:
print splitted[i]
splitted[i] = dictionary[splitted[i]]
sentence = ' '.join(splitted)
return(sentence)
While I hope this answer will help newbies, Jean-François Fabre's answer is far better.