Skip to content

Instantly share code, notes, and snippets.

@sararob
Last active November 20, 2016 21:55
Show Gist options
  • Save sararob/c1701d78523d4efe741203c08cc6a4bf to your computer and use it in GitHub Desktop.
Save sararob/c1701d78523d4efe741203c08cc6a4bf to your computer and use it in GitHub Desktop.
Count all adjectives in tweets about Hillary and Trump in BigQuery
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
@indrabayu
Copy link

Hi Sarah! Do you have more codes like this SQL/JavaScript that utilizes the NL API?
I like this example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment