All problems
Topic Coherence (Lite)
Problem
Compute a lite topic coherence. For each topic, average the PMI log((c12 * N) / (c1 * c2)) over all word pairs that have positive unigram and pair counts, treating pair counts as symmetric.
Return the average of the per-topic means, or 0.0 when no pair qualifies.
Hint
Look up the pair count in both orders before computing the pointwise mutual information.
Test cases
case 1
in: [[["cat","dog"]],{"cat":2,"dog":2},{"cat dog":1},4]
expected: 0
case 2
in: [[["cat","dog"]],{"cat":2,"dog":2},{"cat dog":2},4]
expected: 0.6931471805599453
case 3
in: [[["cat","dog"],["sun","moon"]],{"cat":2,"dog":2,"sun":1,"moon":1},{"cat dog":2,"sun moon":2},4]
expected: 1.3862943611198906
case 4
in: [[["cat","dog"]],{"cat":0,"dog":2},{"cat dog":2},4]
expected: 0
case 5
in: [[],{},{},4]
expected: 0
Starter codePython
import math
def topic_coherence_lite(topics, word_counts, pair_counts, n_docs):
# Your code here
pass5 test cases · run your code against them in the editor