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

3

6

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

Bulger answered 11/6, 2014 at 21:37 Comment(0)
B
8

Now with a new function: ML.NGRAMS():

WITH data AS  (
  SELECT REGEXP_EXTRACT_ALL(LOWER(title), '[a-z]+') title_arr
  FROM `fh-bigquery.reddit_posts.2019_08`  
  WHERE title LIKE '% %'
  AND score>1
)


SELECT APPROX_TOP_COUNT(bigram, 10) top
FROM (
  SELECT ML.NGRAMS(title_arr, [2,2]) x
  FROM data
), UNNEST(x) bigram
WHERE LENGTH(bigram) > 10

enter image description here

Docs:

Bulger answered 9/1, 2020 at 7:55 Comment(1)
Thanks for sharing, @Felipe The statement that you provide is nested within a row. How would you "unnest" it further so that the only output columns are top.value and top.count and not the Row column?Indiscipline
B
11

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
Bulger answered 11/6, 2014 at 21:37 Comment(4)
Lovely!! where can some documentation can be found?Appenzell
Soon in developers.google.com/bigquery/docs/query-reference. I couldn't wait to share the news :)Bulger
@FelipeHoffa This is using LegacySQL, any chance you'd be up for updating this using StandardSQL?Comminute
check out the new ML.NGRAMS() function in my new answer belowBulger
B
8

Now with a new function: ML.NGRAMS():

WITH data AS  (
  SELECT REGEXP_EXTRACT_ALL(LOWER(title), '[a-z]+') title_arr
  FROM `fh-bigquery.reddit_posts.2019_08`  
  WHERE title LIKE '% %'
  AND score>1
)


SELECT APPROX_TOP_COUNT(bigram, 10) top
FROM (
  SELECT ML.NGRAMS(title_arr, [2,2]) x
  FROM data
), UNNEST(x) bigram
WHERE LENGTH(bigram) > 10

enter image description here

Docs:

Bulger answered 9/1, 2020 at 7:55 Comment(1)
Thanks for sharing, @Felipe The statement that you provide is nested within a row. How would you "unnest" it further so that the only output columns are top.value and top.count and not the Row column?Indiscipline
E
1

The standard SQL version:

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 ( 
    SELECT created_utc, title, SPLIT(title, ' ') word FROM `bigquery-samples.reddit.full`), UNNEST(word) as word WITH OFFSET pos))
WHERE nextword IS NOT null
GROUP BY 1, 2
ORDER BY c DESC
LIMIT 100

When unnesting an ARRAY you can retrieve the position of that element using the following syntax:

UNNEST(word) as word WITH OFFSET pos
Estival answered 22/11, 2019 at 11:21 Comment(1)
check out the new ML.NGRAMS() function in my new answer belowBulger

© 2022 - 2024 — McMap. All rights reserved.