Javascript 您如何从Google';s基于现有文本的云自然语言API,使用客户端JS?

Javascript 您如何从Google';s基于现有文本的云自然语言API,使用客户端JS?,javascript,api,google-cloud-platform,nlp,Javascript,Api,Google Cloud Platform,Nlp,有人知道如何使用客户端javascript从谷歌的云自然语言(NL)API检索数据吗?下面的代码块有什么不正确-可能是第29行 今天我看了很多文档,想知道如何使用Google的Cloud NL API进行情绪分析。其中许多问题还不是很清楚 这在很大程度上帮助我理解了谷歌的云NL API是如何工作的 我还有一些差距 这是我在看了几个文档后得到的代码块 我认为这可能是一个潜在的问题,第29行。 我从来没有使用两个参数来获取数据 1 sentiApiKey = ***API KEY*** 2 se

有人知道如何使用客户端javascript从谷歌的云自然语言(NL)API检索数据吗?下面的代码块有什么不正确-可能是第29行

今天我看了很多文档,想知道如何使用Google的Cloud NL API进行情绪分析。其中许多问题还不是很清楚

这在很大程度上帮助我理解了谷歌的云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的云自然语言API

下面的一些文档将帮助您使用客户端JS进行情绪分析:

干杯