-
Notifications
You must be signed in to change notification settings - Fork 0
/
nluService.js
74 lines (65 loc) · 2.1 KB
/
nluService.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const NLU = require('watson-developer-cloud/natural-language-understanding/v1.js');
const api = require('./config.json');
const BASE_URL = api.url;
const API_KEY = api.key;
const nlu = new NLU({
version: '2018-11-16',
iam_apikey: API_KEY,
url: BASE_URL
});
exports.get = (str, features, callback) => {
let parameters = {
'features': {
// 'sentiment': {},
// 'categories': {},
// 'concepts': {},
// 'keywords': {},
'entities': {},
},
};
if(validURL(str)){
parameters.url = str;
}
else{
parameters.text = str;
}
nlu.analyze(parameters, (err, res) => {
if (err) {
console.error(err);
callback([],[], err);
return;
}
let persons = res.entities.filter(ele => ele.type=="Person");
persons.sort((a, b) => {return b.relevance - a.relevance})
persons.forEach( (p)=> {
p.relevance = Math.round(p.relevance*1000)/10;
console.log(p.relevance);
})
let emails = res.entities.filter(ele => ele.type=="EmailAddress" )
emails.forEach( (p)=> {
p.relevance = Math.round(p.relevance*1000)/10;
})
emails.sort((a, b) => {return b.relevance - a.relevance})
let locations = res.entities.filter(ele => ele.type=="Location" )
locations.sort((a, b) => {return b.relevance - a.relevance})
locations.forEach( (p)=> {
p.relevance = Math.round(p.relevance*1000)/10;
})
// console.log({persons});
// console.log({emails});
// console.log('entites',res.entities);
// console.log('keyword',res.keywords);
if (callback) {
callback(persons, emails, locations, err);
}
});
}
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+
'((\\d{1,3}\\.){3}\\d{1,3}))'+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+
'(\\?[;&a-z\\d%_.~+=-]*)?'+
'(\\#[-a-z\\d_]*)?$','i');
return !!pattern.test(str);
}