All problems
Bigram Counts
Problem
Count every adjacent token pair in the token list.
Return a dictionary whose keys are the two tokens joined by a single space and whose values are the counts. A list shorter than two tokens yields an empty dictionary.
Hint
Slide a window of size 2 and join each pair with a space.
Test cases
case 1
in: [["a","b","a","b"]]
expected: {"a b":2,"b a":1}
case 2
in: [["the","cat","sat","on","the","mat"]]
expected: {"the cat":1,"cat sat":1,"sat on":1,"on the":1,"the mat":1}
case 3
in: [["x"]]
expected: {}
case 4
in: [[]]
expected: {}
Starter codePython
def bigram_counts(tokens):
# Your code here
pass4 test cases · run your code against them in the editor