All problems
Collocation Count
Problem
Return how many times w1 is immediately followed by w2 in tokens.
Only adjacent pairs count, and a token list shorter than two elements yields 0.
Hint
Overlapping bigrams both count, so a a a contains two (a, a) pairs.
Test cases
case 1
in: [["new","york","new","york","city"],"new","york"]
expected: 2
case 2
in: [["a","a","a"],"a","a"]
expected: 2
case 3
in: [["a","b","c"],"b","a"]
expected: 0
case 4
in: [["a"],"a","a"]
expected: 0
Starter codePython
def collocation_count(tokens, w1, w2):
# Your code here
pass4 test cases · run your code against them in the editor