Review keywords
EasyOnline Assessment
Find the words customers mention most often in a batch of product reviews.
Each review is turned into keywords like this, in order:
- Lowercase it and cut it into words: maximal runs of the letters
a-z. Everything else (spaces, digits, punctuation, apostrophes, hyphens) only separates words, so"don't"givesdonandt. - Drop every word shorter than 3 letters.
- Drop every word that is in
stop_words(a list of lowercase words). This check uses the word as written, before step 4. - Reduce what is left to its stem using the first rule that applies, and no other:
- it ends in
iesand has at least 5 letters: replaceieswithy(criesbecomescry); - it ends in
ingand has at least 6 letters: removeing(chargingbecomescharg); - it ends in
s, does not end inss, and has at least 4 letters: remove thes(lastsbecomeslast,glassandbusstay).
A stem counts once per review, however many words in that review reduce to it. A stem's count is the number of reviews it appears in.
Input: reviews, a list of strings; stop_words, a list of lowercase words; and k, an integer.
Output: a list of at most k strings "stem count", sorted by count descending and, for equal counts, by stem in ascending alphabetical order. If there are fewer than k distinct stems, return all of them; if there are none, return [].
Example 1
- Input:
- [["The battery lasts long and charging is fast","Fast charging, long battery. Battery was great!","Batteries die fast: not great for traveling"],["the","and","was","this","for","not","very","too"],3]
- Output:
- ["battery 3","fast 3","charg 2"]
- Explanation
- "Batteries" becomes "battery" (ies rule), "charging" becomes "charg", "traveling" becomes "travel" and "lasts" becomes "last". "battery" appears in all three reviews (twice in the second, which still counts once) and "fast" in all three. Then "charg", "great" and "long" each appear in two reviews; the tie is broken alphabetically, so "charg" takes the last place.
Example 2
- Input:
- [["Cries for glass bottles","bottle, Bottles; GLASS glass","Bus rides, ties and dies"],["the","and","was","this","for","not","very","too"],5]
- Output:
- ["bottle 2","glass 2","bus 1","cry 1","die 1"]
- Explanation
- "Cries" is cry, "ties" has only 4 letters so it falls to the s rule and is "tie", "dies" is "die", "rides" is "ride". "glass" ends in ss and "bus" is too short to lose its s, so both stay. "bottle" and "bottles" are one stem, counted once in the second review, so "bottle" is 2, and "glass" is 2 for the same reason. The count-1 stems follow alphabetically.
Example 3
- Input:
- [["Don't buy it. It's too cheap & not-great!","ab ab cd ef ghij"],["the","and","was","this","for","not","very","too"],10]
- Output:
- ["buy 1","cheap 1","don 1","ghij 1","great 1"]
- Explanation
- Apostrophes and hyphens split words: "Don't" gives "don" and "t" (dropped, too short) and "It's" gives "it" and "s" (both dropped). "too" and "not" are stop words. "great" and "cheap" survive. In the second review "ab", "cd" and "ef" are too short, leaving "ghij". With k = 10 all five stems are returned, each once.
Constraints
0 <= reviews.length <= 500- Each review has at most 300 characters; characters are printable ASCII
0 <= stop_words.length <= 500 <= k <= 100