简体   繁体   English

您如何使用客户端 JS 基于现有文本从 Google 的 Cloud Natural Language API 获取 JSON?

[英]How do you fetch JSON from Google's Cloud Natural Language API based on existing text using client-side JS?

Does anyone know how to retrieve data from Google's Cloud Natural Language (NL) API using client-side javascript?有谁知道如何使用客户端 javascript 从 Google 的 Cloud Natural Language (NL) API 检索数据? What is incorrect with the code block below - likely line 29?下面的代码块有什么不正确 - 可能是第 29 行?

I've been looking at a lot of documentation today to figure out how to perform sentiment analysis using Google's Cloud NL API.我今天一直在查看大量文档,以了解如何使用 Google 的 Cloud NL API 执行情绪分析。 Many of them have not been very clear.他们中的许多人都不是很清楚。

This tutorial helped me understand how Google's Cloud NL API works to a large degree.教程帮助我在很大程度上了解了 Google 的 Cloud NL API 是如何工作的。

I still have a few gaps.我还有一些差距。

This is the block of code I derived after looking at a few docs.这是我在查看一些文档后得出的代码块。

One area I think that may be a potential issue, line 29. I've never fetched using two arguments.我认为这可能是一个潜在问题,第 29 行。我从未使用两个参数进行提取。

1  sentiApiKey = ***API KEY***
2  sentiEndPoint = ***Google's Cloud Natural Language endpoint***
3  //dictData is existing JSON data

5  function searchSenti(dictData) {
6     const sentiParams = {
7         key: sentiApiKey,
8     }

10    const sentiApiKeyString = formatQueryParams(sentiParams)
11    const sentiUrl = sentiAnEndPoint + "?" + sentiApiKeyString;

13    const def = dictData[0].shortdef[0]
14    const dict = {
15        language: 'en-us',
16        type: 'PLAIN_TEXT',
17        content: def
18    };

20    const nlApiData = {
21        document: dict,
22        encodingType: 'UTF8'
23    };

25    const nlCallOptions = {
26        method: 'post',
27        contentType: 'application/json',
28        payload: JSON.stringify(nlApiData)
29    }

31    fetch(sentiUrl, nlCallOptions)
32    .then(response => {
33        if (response.ok) {
34            return response.json();
35        }
36        throw new Error(response.statusText);
37    })
38    .then(sentiData => parseSenti(sentiData))
39    .catch(err => {
40        $("#error-message").removeClass("hidden");
41        $("#js-error-message").text(`Something went wrong with the 
          Sentiment API: ${err.message}`);
43    });
44 }

46 function parseSenti(sentiData) {
47    const data = JSON.parse(sentiData);
48    const sentiment = 0.0;

50    if (data && data.documentSentiment && data.documentSentiment.score){
51       sentiment = data.documentSentiment.score;
52    }

54    console.log(sentiment);
55 }

A few days ago, I learned that there is no way to use Google's Cloud Natural Language API using client-side JS.前几天,我了解到使用客户端JS无法使用Google的Cloud Natural Language API。

Here's some documentation that will help you use client-side JS for sentiment analysis: http://www.gtlambert.com/blog/sentiment-analysis-sentimoodjs https://github.com/thinkroth/Sentimental这里有一些文档可以帮助您使用客户端 JS 进行情​​绪分析: http : //www.gtlambert.com/blog/sentiment-analysis-sentimoodjs https://github.com/thinkroth/Sentimental

Cheers.干杯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将客户端 firebase 云消息传递令牌获取到谷歌云 function? - How do you get client-side firebase cloud messaging token into google cloud function? 使用现有访问令牌通过Google的gapi库进行的授权客户端JS API调用? - Authorized client-side JS API calls with Google's gapi library with an existing access token? 如何使用服务器上的 Node 从客户端 JavaScript 上传到 Google Cloud Storage? - How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server? 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)? 客户端js获取node.js API调用 - Client-side js to fetch node.js API calls 如何仅使用客户端语言从本地文件加载字符串? - How do you load a string from local file only using client-side languages? 无法通过 fetch 从客户端调用 Firebase Cloud Function - Unable to call Firebase Cloud Function from client-side with fetch 您是否需要为客户端应用程序使用Google Maps API的API密钥? - Do you need to use API key for google maps API for client-side applications? 仅使用客户端javascript与Google自然语言API进行简单身份验证 - Simple authentication with Google Natural Language API using only client javascript 您如何在客户端使用Node.js变量? - How do you use a Node.js variable on the client-side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM