Skip to content
DeepForgeby svx
All problems
HardNLPnlp-333

Hapax Legomena Ratio

Problem

Hapax legomena are tokens appearing exactly once. The hapax ratio is the count of such token types divided by the number of types.

Given the token list, return the ratio, or 0.0 when empty.

Hint

Count token frequencies first, then compare the number of singletons to the vocabulary size.

Test cases

case 1
in: [["a","b","b","c"]]
expected: 0.6666666666666666
case 2
in: [["a","a","a"]]
expected: 0
case 3
in: [["x","y","x","y"]]
expected: 0
Starter codePython
def hapax_legomena_ratio(tokens):
    # Your code here
    pass

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