forked from kyma-incubator/kyma-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-entity.yaml
129 lines (124 loc) · 4.16 KB
/
text-entity.yaml
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
apiVersion: serverless.kyma-project.io/v1alpha1
kind: Function
metadata:
name: text-entity
spec:
deps: |-
{
"name": "text-entity",
"version": "1.0.0",
"dependencies": {
"node-fetch": "2.6.1",
"@google-cloud/language":"4.3.0",
"nanoid": "3.1.25"
}
}
env:
- name: GCP_API_KEY
valueFrom:
secretKeyRef:
key: GCP_API_KEY
name: kyma-showcase-secret
- name: GCP_EMAIL
valueFrom:
secretKeyRef:
key: GCP_EMAIL
name: kyma-showcase-secret
- name: API_CONFIG
valueFrom:
configMapKeyRef:
key: config.json
name: showcase-functions-config
runtime: nodejs14
source: |-
module.exports = {
main: async function (event, context) {
async function getText(id) {
console.log("Getting text from DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL);
const data = await response.json();
const content = data.gcp.map(JSON.parse);
const font =
content?.find((obj) => Object.keys(obj).includes('font'))?.font || [];
console.log("Text loaded");
return font;
}
function calculateSentiment(score, magnitude, text) {
score = parseFloat(score);
magnitude = parseFloat(magnitude);
scoreThreshold = 0.2;
magnitudeThreshold = 0.5;
if (score > scoreThreshold) return 'positive';
else if (score < -scoreThreshold) return 'negative';
else if (-scoreThreshold < score && score < scoreThreshold) {
if (magnitude / (text.split(/\S+/).length - 1) < magnitudeThreshold)
return 'neutral';
else return 'mixed';
}
}
async function getTextEntities(text) {
console.log("Getting text entities");
const language = require('@google-cloud/language');
const options = {
credentials: {
client_email: process.env.GCP_EMAIL,
private_key: process.env.GCP_API_KEY.replace(/\\n/gm, '\n'),
},
};
const client = new language.LanguageServiceClient(options);
const document = {
content: text,
type: 'PLAIN_TEXT',
};
const [result] = await client.analyzeEntitySentiment({ document });
const entities = result.entities;
const entityDetails = entities.map((entity) => ({
name: entity.name,
type: entity.type,
score: entity.sentiment.score,
magnitude: entity.sentiment.magnitude,
sentiment: calculateSentiment(
entity.sentiment.score,
entity.sentiment.magnitude,
entity.name
),
}));
if (entityDetails.length === 0) return null;
const entityJSON = JSON.stringify({ entityDetails });
return entityJSON;
}
async function putDetailsToDB(id, data) {
console.log("Putting entity details to DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL, {
method: 'PUT',
body: data,
headers: {
'Content-type': 'application/json',
},
});
await console.log(
"Entity details put to DB with status: " + response.status
);
return response.json();
}
try {
// Needed to remove unwanted apostrophes from our ID
const imgID = event.data.slice(3, -3);
console.log("Processing image: " + imgID);
const text = await getText(imgID);
const textEntities = await getTextEntities(text);
if (textEntities) {
await putDetailsToDB(imgID, textEntities);
} else {
console.log("Entities array empty");
}
} catch (err) {
console.error(err);
return null;
}
},
};