Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the most common bi-grams with BigQuery?

I want to find the most common bi-grams (pair of words) in my table. How can I do this with BigQuery?

like image 532
Felipe Hoffa Avatar asked Dec 02 '22 19:12

Felipe Hoffa


1 Answers

BigQuery now supports SPLIT():

SELECT word, nextword, COUNT(*) c 
FROM (
SELECT pos, title, word, LEAD(word) OVER(PARTITION BY created_utc,title ORDER BY pos) nextword FROM (
SELECT created_utc, title, word, pos FROM FLATTEN(
  (SELECT created_utc, title, word, POSITION(word) pos FROM
   (SELECT created_utc, title, SPLIT(title, ' ') word FROM [bigquery-samples:reddit.full])
  ), word)
))
WHERE nextword IS NOT null
GROUP EACH BY 1, 2
ORDER BY c DESC
LIMIT 100
like image 133
Felipe Hoffa Avatar answered Dec 29 '22 18:12

Felipe Hoffa