Skip to content

Commit

Permalink
change emotions prediction api, change ui
Browse files Browse the repository at this point in the history
  • Loading branch information
fihis committed Jun 15, 2020
1 parent 776852c commit f993775
Show file tree
Hide file tree
Showing 24 changed files with 736 additions and 135 deletions.
27 changes: 22 additions & 5 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@paralect/email-service": "0.2.0",
"@paralect/node-mongo": "2.0.0-beta.1",
"app-module-path": "2.2.0",
"axios": "0.19.2",
"bcrypt": "4.0.1",
"google-auth-library": "6.0.0",
"ibm-watson": "5.5.0",
Expand Down
3 changes: 3 additions & 0 deletions api/src/resources/processing/analyze/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validate = require('middlewares/validate');

const NLPService = require('services/nlp.service');

const validator = require('./validator');
Expand All @@ -9,9 +10,11 @@ const handler = async (ctx) => {

const result = await NLPService.analyzeText(text);
const vehicleColors = await NLPService.recognizeVehicleColor(image);
const emotions = await NLPService.analyzeEmotions(text);

ctx.body = {
...result,
emotions,
entities: [
...result.entities,
...vehicleColors,
Expand Down
22 changes: 21 additions & 1 deletion api/src/services/nlp.service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable class-methods-use-this */
const fs = require('fs');
const sharp = require('sharp');
const axios = require('axios');
const vision = require('@google-cloud/vision');
const { NaturalLanguageUnderstandingV1 } = require('ibm-watson/sdk');
const { IamAuthenticator } = require('ibm-watson/auth');

const { rgbToHex } = require('helpers/base.helper');

class NaturalLanguageProcessingService {
Expand Down Expand Up @@ -34,14 +37,31 @@ class NaturalLanguageProcessingService {
concepts: {},
entities: {},
keywords: {},
emotion: {},
semantic_roles: {},
},
});

return response.result;
}

async analyzeEmotions(text) {
const sentencesArray = text.replace(/([.?!])\s*(?=[A-Z])/g, '$1|').split('|');

const predictionsArray = (await Promise.all(sentencesArray.map((sentence) => (
axios.post('http://demo-nlp.paralect.net/predict', { text: sentence })
)))).map((element) => (element.data.predictions.flat(1)));

const averageEmotions = predictionsArray[0].map((_col, i) => predictionsArray.map(
(row) => row[i],
).reduce((acc, c) => acc + c, 0) / predictionsArray.length);

if (!averageEmotions) {
throw new Error('Not recognized');
}

return averageEmotions;
}

async localizeVehicle(image) {
const imageBuffer = fs.readFileSync(image.path);
const sharpImage = sharp(imageBuffer);
Expand Down
Loading

0 comments on commit f993775

Please sign in to comment.