繁体   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?

有谁知道如何使用客户端 javascript 从 Google 的 Cloud Natural Language (NL) API 检索数据? 下面的代码块有什么不正确 - 可能是第 29 行?

我今天一直在查看大量文档,以了解如何使用 Google 的 Cloud NL API 执行情绪分析。 他们中的许多人都不是很清楚。

教程帮助我在很大程度上了解了 Google 的 Cloud NL API 是如何工作的。

我还有一些差距。

这是我在查看一些文档后得出的代码块。

我认为这可能是一个潜在问题,第 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 }

前几天,我了解到使用客户端JS无法使用Google的Cloud Natural Language API。

这里有一些文档可以帮助您使用客户端 JS 进行情​​绪分析: http : //www.gtlambert.com/blog/sentiment-analysis-sentimoodjs https://github.com/thinkroth/Sentimental

干杯。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM