簡體   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