Interview Query

Replace Words with Stems

8
Have you seen this question before?

In data science, there exists the concept of stemming, which is the heuristic of chopping off the end of a word to clean and bucket it into an easier feature set. 

Given a dictionary consisting of many roots and a sentence, write a function replace_words to stem all the words in the sentence with the root forming it. If a word has many roots that can form it, replace it with the root with the shortest length.

Example:

Input:

roots = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"

Output:

 "the cat was rat by the bat"
Next question: Find Bigrams
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code