Last active
November 20, 2016 21:55
-
-
Save sararob/c1701d78523d4efe741203c08cc6a4bf to your computer and use it in GitHub Desktop.
Count all adjectives in tweets about Hillary and Trump in BigQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
COUNT(*) as c, adjective, subject | |
FROM | |
JS( | |
(SELECT tokens FROM [sara-bigquery:syntax.election_tweets]), | |
tokens, | |
"[{ name: 'adjective', type: 'string'}, | |
{name: 'subject', type: 'string'} | |
]", | |
"function(row, emit) { | |
try { | |
x = JSON.parse(row.tokens); | |
x.forEach(function(token) { | |
if (token.dependencyEdge.label === 'NSUBJ') { | |
var content = token.text.content.toLowerCase(); | |
var subj; | |
if ((content === 'realdonaldtrump') || (content === 'trump')) { | |
subj = 'trump'; | |
} else if ((content === 'hillary') || (content === 'hillaryclinton') || (content === 'clinton')) { | |
subj = 'hillary'; | |
} | |
if (subj) { | |
findAdjectives(subj); | |
} | |
} | |
}); | |
function findAdjectives(subj) { | |
x.forEach(function(token) { | |
if (token.partOfSpeech.tag === 'ADJ') { | |
emit({ adjective: token.text.content.toLowerCase(), subject: subj }); | |
} | |
}); | |
} | |
} catch (e) {} | |
}" | |
) | |
GROUP BY adjective, subject | |
ORDER BY c DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Sarah! Do you have more codes like this SQL/JavaScript that utilizes the NL API?
I like this example!