Data Formatting
General Info
Useful Snippets

9.0.22 stem words in a string

Example:

def stem_words_in_text(text):
    "converts each word in the string to it's stem, or root word, and returns the stemmed string"
    token_list = word_tokenize(text)  # Tokenize the string by converting each word to an item in a list
    porter = PorterStemmer()          # Instantiate the PorterStemmer

    # Go through  each word in the token_list, stem it, and add it to a list of 'stemmed_tokens'
    stemmed_tokens = []            
    for t in token_list:
        stem_word = porter.stem( t)
        stemmed_tokens.append(stem_word)

    # convert the stemmed_tokens list back to a string
    stemmed_string = ' '.join(str(word) for word in stemmed_tokens)
    return(stemmed_string)