-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
53 lines (42 loc) · 1.67 KB
/
index.coffee
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
request = require 'request'
querystring = require 'querystring'
entities = ['people_eng', 'places_eng', 'companies_eng', 'organizations',
'languages', 'drugs_eng', 'professions', 'universities',
'films', 'internet', 'teams', ]
makeUrl = (paramaters, apiKey) ->
host = "https://api.havenondemand.com/1/api/sync/extractentities/v2"
if typeof paramaters is 'string'
urlParams = 'text='+paramaters
for entity in entities then urlParams += '&entity_type='+entity
urlParams += '&show_alternatives=false'
else if typeof paramaters is 'object'
entities = paramaters.entity_type
urlParams = querystring.stringify(paramaters)
url = host + '?' + urlParams + '&apikey=' + apiKey
formatResults = (body) ->
results = {}
for b in body.entities
if !results[b.type] then results[b.type] = []
matches = []
for m in b.matches then matches.push m.original_text
additionalInformation = {}
additionalInformation.wiki =
if b.additional_information.hasOwnProperty 'wikipedia_eng'
b.additional_information.wikipedia_eng
else ''
additionalInformation.image =
if b.additional_information.hasOwnProperty 'image'
b.additional_information.image
else ''
results[b.type].push({
normalized_text: b.normalized_text
matches: matches
additional_information: additionalInformation
})
results
module.exports = (paramaters, apiKey, callback)->
url = makeUrl paramaters, apiKey # Make the URL
# Make the actual request, and call the callback
request {url: url, json: true}, (error, response, body) ->
if !error and response.statusCode == 200 then callback formatResults body
else callback(error)