Skip to content

Instantly share code, notes, and snippets.

@estelsmith
Created February 16, 2023 21:00
Show Gist options
  • Save estelsmith/cc6b0bb3dc787d42913a32be87eb6efc to your computer and use it in GitHub Desktop.
Save estelsmith/cc6b0bb3dc787d42913a32be87eb6efc to your computer and use it in GitHub Desktop.
Airtable Scripting - Google Natural Language Classification Test
let config = input.config({
title: 'Natural Language Classification',
description: 'Example of using Google Natural Language REST API to classify text.',
items: []
});
let apiKey = 'REDACTED'; // Provide your own API key here.
let endpoints = {
classifyText: 'https://language.googleapis.com/v1/documents:classifyText'
}
let requestBody = {
document: {
type: 'PLAIN_TEXT',
language: 'en',
content: 'This is a document all about making RESTful API calls!'
},
classificationModelOptions: {
v2Model: {
contentCategoriesVersion: 'V2'
}
}
};
let queryString = new URLSearchParams({
key: apiKey
});
let endpoint = endpoints.classifyText + '?' + queryString.toString();
let response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(requestBody)
});
output.text(endpoint);
output.text(JSON.stringify(requestBody));
if (!response.ok) {
output.text('Request failed! ' + response.statusText);
} else {
let responseBody = await response.json();
output.text(JSON.stringify(responseBody));
let categories = responseBody.categories;
output.text('Found the following categories:');
for (let category of categories) {
output.text(`${category.name}; confidence ${category.confidence}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment