-
Notifications
You must be signed in to change notification settings - Fork 0
/
languageProcessing.js
58 lines (56 loc) · 1.64 KB
/
languageProcessing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function analyzeText(text) {
return new Promise((resolve, reject) => {
const requestUrl = [
"https://language.googleapis.com/v1/documents:analyzeEntities?key=",
"AIzaSyADeFluX6JmUaFcjWewHHd80Tyl-3xAJw8"
].join("");
const data = {
document: {
type: "PLAIN_TEXT",
language: "EN",
content: text
}
};
fetch(requestUrl, {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}).then(fetched => {
fetched.json().then(res => {
console.log(res);
// const printableResult = JSON.stringify(res);
let org;
let item;
let i = 0;
while (
i < res["entities"].length &&
(typeof org === "undefined" || typeof item === "undefined")
) {
if (
typeof org === "undefined" &&
res["entities"][i].type === "ORGANIZATION"
) {
org = res["entities"][i].name;
}
if (
typeof item === "undefined" &&
(res["entities"][i].type === "CONSUMER_GOOD" ||
res["entities"][i].type === "LOCATION" ||
res["entities"][i].type === "WORK_OF_ART") &&
!res["entities"][i].name.toLowerCase().includes("product") &&
(res["entities"][i].mentions.length > 1 ||
res["entities"][i].salience > 0.1)
) {
item = res["entities"][i].name;
}
i++;
}
// Only using the item right now, could use org too
resolve(item);
});
});
});
}