Skip to content
DeepForgeby svx
All problems
HardNLPnlp-137

Keyword Extraction by TF-IDF

Problem

Rank the top-k keywords across a document collection by summed TF-IDF. For each term, add count-in-document / document length times log(N / df) over every document containing it, where N is the number of documents and df its document frequency.

Return the top-k term strings ranked by descending score with alphabetical tie-breaking, or an empty list when k is not positive or docs is empty.

Hint

A term appearing in every document has idf log(1) = 0, so it scores nothing.

Test cases

case 1
in: [[["a","b","a"],["b","c"],["c","a"]],2]
expected: ["a","c"]
case 2
in: [[["x"]],1]
expected: ["x"]
case 3
in: [[["a","b"]],0]
expected: []
case 4
in: [[],2]
expected: []
case 5
in: [[["a"],["a"]],2]
expected: ["a"]
Starter codePython
import math
def keyword_extraction_tfidf(docs, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor