forked from mcantelon/node-calais
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalais.js
148 lines (120 loc) · 4.11 KB
/
calais.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
* calais
* Copyright(c) 2011 Mike Cantelon, 2012 Daniel Pfile
* MIT Licensed
*/
var request = require('request');
var Calais = function(api_key, options) {
this.initialize(api_key, options)
}
Calais.prototype = {
initialize:function(api_key, options) {
this.api_key = api_key
this.defaults = {
'apiHost': 'api.opencalais.com',
'apiPath': '/tag/rs/enrich',
'contentType': 'text/raw',
'outputFormat': 'object',
'reltagBaseURL': '',
'calculateRelevanceScore': true,
'enableMetadataType': 'GenericRelations,SocialTags',
'docRDFaccessible': false,
'allowDistribution': false,
'allowSearch': false,
'cleanResult': true
}
this._set_default_options(options)
},
_set_default_options:function(options) {
options = options || {}
this.options = {}
for (var index in this.defaults) {
this.options[index] = (this._undefined_or_null(options[index]))
? this.defaults[index]
: options[index]
}
},
_undefined_or_null:function(value) {
return value == undefined || value == null
},
set:function(key, value) {
this.options[key] = value
},
validate_options:function() {
return true
},
clean_result:function(result) {
var clean_result = []
for(var i in result) {
if (i != 'doc') {
clean_result.push(result[i])
}
}
return clean_result
},
fetch:function(callback) {
var calais = this
if (this.validate_options()) {
var outputFormat = (calais.options.outputFormat == 'object')
? 'application/json'
: this.options.outputFormat
var params = {
'Host': this.options.apiHost,
'x-calais-licenseID': this.api_key,
'Content-Type': this.options.contentType,
'Accept': outputFormat,
'calculateRelevanceScore': this.options.calculateRelevanceScore,
'enableMetadataType': this.options.enableMetadataType,
'docRDFaccessible': this.options.docRDFaccessible,
'allowDistribution': this.options.allowDistribution,
'allowSearch': this.options.allowSearch
}
if (!this._undefined_or_null(this.options['externalID'])) {
params.externalID = this.options['externalID']
}
if (!this._undefined_or_null(this.options['submitter'])) {
params.submitter = his.options['submitter']
}
var reqopts = {
uri: 'http://' + this.options.apiHost + this.options.apiPath,
headers: params,
method: 'POST',
body: this.options.content
};
request(reqopts, function (error, response, body) {
if (error) {
return callback(error, null);
}
if (response.statusCode != 200) {
if (body.indexOf('does not yet support') != -1) {
return callback(null, {'error': {'message': 'Unsupported language'}});
}
if (body.indexOf('length has exceeded') != -1) {
return callback(null, {'error': {'message': 'The text is too large'}});
}
if (body.indexOf('content is invalid') != -1) {
return callback(null, {'error': {'message': 'The content is invalid'}});
}
return callback(body, null);
}
var calaisData = body;
// take note of whether Javascript object output was requested
var jsOutput = (calais.options.outputFormat == 'object');
// parse to a Javascript object if requested
try {
var result = (jsOutput)
? JSON.parse(calaisData)
: calaisData;
} catch (error) {
return callback(error, null);
}
// ignore cleanResult preference if not requesting an object
result = (jsOutput && calais.options.cleanResult)
? calais.clean_result(result)
: result;
return callback(null, result);
});
}
}
}
exports.Calais = Calais