Created
February 16, 2023 21:00
-
-
Save estelsmith/cc6b0bb3dc787d42913a32be87eb6efc to your computer and use it in GitHub Desktop.
Airtable Scripting - Google Natural Language Classification Test
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
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