To learn vectors for phrases such as "new york times", Mikolov et al. first join word pairs that occur together often, and rarely apart, into single tokens. A bigram is scored by where is a discount that stops pairs of very rare words from becoming phrases, and bigrams scoring above a threshold become phrases. Running several passes with a decreasing threshold lets longer phrases form. Implement the score, one pass, and the repeated passes.
Tokens are non-empty strings without spaces. Counts are taken from the token list the function is given.
phrase_scores(tokens, delta) -> dict
count(w) is the number of positions holding w; count(w_i w_j) is the number of adjacent positions holding w_i then w_j.(w_i, w_j) that occurs in tokens to its score as a float. An empty or one-token list gives {}.merge_pass(tokens, delta, threshold) -> list
tokens once, then scan left to right: if the current token and the next one form a bigram whose score is strictly greater than threshold, emit one token left + "_" + right and move past both; otherwise emit the current token and move one step.build_phrases(tokens, delta, thresholds) -> list
merge_pass once per threshold, in the order given, each pass on the output of the previous one (so counts are recomputed every pass). Return the final token list.