17. Keyword Highlights from Product Reviews
Above the reviews section, a product page shows chips like "battery (24)" and "screen (18)" so shoppers can jump to reviews about what matters to them.
Counting raw word frequency gave bad chips: "the" and "and" always won, and one angry reviewer who wrote "terrible" twelve times put "terrible" on top. The product team wants each word counted at most once per review, so the count means "how many reviews mention it".
Your task:
Write getReviewKeywords(reviews, topN) and return [{ word, count }].
Rules:
- Lowercase everything. Words are runs of the letters a to z; anything else (spaces, digits, punctuation, apostrophes) separates words.
- Ignore words shorter than 3 letters and these stop words: the, and, for, this, that, with, was, but, are, have, not, you, its, very, after.
- count is the number of reviews that contain the word.
- Sort by count (highest first), then by word A to Z. Return the first topN.
Examples
Example 1
- Input
reviews = [ "Battery life is great, great screen", "Screen cracked after a week", "Great battery!" ]
topN = 3
- Output
[ { word: "battery", count: 2 }, { word: "great", count: 2 }, { word: "screen", count: 2 } ]
- Explanation
- "great" appears twice in the first review but counts once for it. Three words are tied at 2, so they are sorted A to Z.
Solution
Write getReviewKeywords. Tab indents (Esc then Tab leaves the editor), Ctrl/⌘ + Enter runs. Each run has a 3-second limit. Your code is saved in this browser.
Test Results
Click "Run Code" to test your solution.