Word frequency ignoring stop words

Python data engineering interview problem. Difficulty: intermediate. Pattern: Dictionaries. About 15 minutes. Part of the Pro drill bank.

A knowledge-base search wants word counts from an article but should ignore stop words like 'the' and 'and' so the histogram is actually useful.

A word cloud should not be full of words like the and and. You get a list of stop words to ignore. Count how many times each word appears in the text, ignoring case and punctuation, and skip any word in the stop word list. Return a dictionary of word to count. Write word_freq_filtered(text, stop). For 'The the cat sat' with stop words ['the'], the result is {'cat': 1, 'sat': 1}. An empty text gives {}. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Lowercase the text and take letters and digits as words.
  • Skip words that are in the stop list (compare in lowercase).
  • Count the other words.
  • Return a dictionary.

Constraints

  • text is a string and stop is a list of words.
  • Stop words match whatever their case in the list is.

Examples

Input: word_freq_filtered('The the cat sat', ['the']) Output: {'cat': 1, 'sat': 1} This input follows the stated rules and produces this output.

Topics: lakebench, python, frequency-counting.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Word frequency ignoring stop words

Interview-style drill: Count word frequency in a document, excluding a given set of stop words.

Count word frequency in text after removing a provided stop-word list.