gpt4 book ai didi

javascript - 如何使用客户端 JS 根据现有文本从 Google 的 Cloud Natural Language API 获取 JSON?

转载 作者:行者123 更新时间:2023-12-02 22:13:02 26 4
gpt4 key购买 nike

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

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

这个tutorial帮助我在很大程度上了解了 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

干杯。

关于javascript - 如何使用客户端 JS 根据现有文本从 Google 的 Cloud Natural Language API 获取 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com