Skip to content

Commit 0c5a5f6

Browse files
Merge pull request #94 from watson-developer-cloud/dev
support for document conversion and bug fixing
2 parents 4125959 + 5ee1cc1 commit 0c5a5f6

File tree

15 files changed

+776
-49
lines changed

15 files changed

+776
-49
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
node_modules/*
22
npm-debug.log
33
coverage
4-
.DS_Store
4+
.DS_Store
5+
test/resources/auth.js
6+
test/resources/mobydick.txt
7+
test/test.all-services.js
8+
test/resources/tts-output.ogg
9+
test/resources/face.jpg

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ APIs and SDKs that use cognitive computing to solve complex problems.
2525
* [Concept Expansion](#concept-expansion)
2626
* [Concept Insights](#concept-insights)
2727
* [Dialog](#dialog)
28+
* [Document Conversion](#document-conversion)
2829
* [Language Translation](#language-translation)
2930
* [Message Resonance](#message-resonance)
3031
* [Natural Language Classifier](#natural-language-classifier)
@@ -221,12 +222,37 @@ var dialog = watson.dialog({
221222
version: 'v1'
222223
});
223224

224-
225225
dialog.getDialogs({}, function (err, dialogs) {
226-
if (err)
227-
console.log('error:', err);
228-
else
229-
console.log(JSON.stringify(dialogs, null, 2));
226+
if (err)
227+
console.log('error:', err);
228+
else
229+
console.log(JSON.stringify(dialogs, null, 2));
230+
});
231+
```
232+
233+
### Document Conversion
234+
235+
```javascript
236+
var watson = require('watson-developer-cloud');
237+
var fs = require('fs');
238+
239+
var document_conversion = watson.document_conversion({
240+
username: '<username>',
241+
password: '<password>',
242+
version: 'v1-experimental'
243+
});
244+
245+
// convert a single document
246+
document_conversion.convert({
247+
// (JSON) ANSWER_UNITS, NORMALIZED_HTML, or NORMALIZED_TEXT
248+
file: fs.createReadStream('sample-docx.docx'),
249+
conversion_target: document_conversion.conversion_target.ANSWER_UNITS
250+
}, function (err, response) {
251+
if (err) {
252+
console.error(err);
253+
} else {
254+
console.log(JSON.stringify(response, null, 2));
255+
}
230256
});
231257
```
232258

RELEASE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Release process
2+
3+
Standard practice with node.js modules is to commit all of the changes for a release except updating the package.json version, and then run the following:
4+
5+
```
6+
npm version major|minor|patch
7+
git push --tags
8+
git push origin master
9+
npm publish
10+
```
11+
12+
`npm version *` will update the package.json version field appropriately and create a git commit and tag for the version.
13+
`git push --tags` will publish the tag to github., and then immediately.
14+
`git push origin master` will publish the changes to package.json.
15+
`npm publish` will publish the npm package.
16+
17+
The reason for this is that it allows someone to easily view the source code (and readme) for whatever version they happen to have downloaded from npm. This is particularly helpful when github is ahead of npm.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var watson = require('watson-developer-cloud');
4+
var fs = require('fs');
5+
6+
var document_conversion = watson.document_conversion({
7+
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
8+
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
9+
version: 'v1-experimental'
10+
});
11+
12+
// convert a single document
13+
document_conversion.convert({
14+
// (JSON) ANSWER_UNITS, NORMALIZED_HTML, or NORMALIZED_TEXT
15+
file: fs.createReadStream(__dirname + '/resources/document_conversion/sample-docx.docx'),
16+
conversion_target: document_conversion.conversion_target.ANSWER_UNITS
17+
}, function (err, response) {
18+
if (err) {
19+
console.error(err);
20+
} else {
21+
console.log(JSON.stringify(response, null, 2));
22+
}
23+
});
17.3 KB
Binary file not shown.

lib/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var extend = require('extend');
2222
var bluemix = require('./bluemix');
2323
var helper = require('./helper');
2424
var omit = require('object.omit');
25+
var request = require('request');
2526

2627

2728
function createServiceAPI(serviceName) {
@@ -36,6 +37,8 @@ function createServiceAPI(serviceName) {
3637
options.alchemy = true;
3738
options.version = 'v1';
3839
options.api_key = options.apikey || options.api_key;
40+
} else {
41+
options.jar = request.jar();
3942
}
4043

4144
// Check if 'version' was provided
@@ -111,15 +114,18 @@ var watson = {};
111114
'tone_analyzer',
112115
'dialog',
113116
'retrieve_and_rank',
117+
'document_conversion',
118+
114119
// deprecated
115120
'search',
116121
'language_identification',
117122
'machine_translation',
118123
'user_modeling',
119124

120125
// alchemy
121-
'alchemy_language'
122-
//'alchemy_vision'
126+
'alchemy_language',
127+
'alchemy_vision',
128+
'alchemy_data_news'
123129

124130
].forEach(function(api) {
125131
watson[api] = createServiceAPI(api);

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
"concept insights",
2929
"tradeoff analytics",
3030
"tone analyzer",
31+
"retrieve and rank",
3132
"natural language classifier",
3233
"dialog",
3334
"tone_analyzer",
34-
"alchemy api",
35+
"alchemy",
36+
"alchemyapi",
3537
"alchemy vision",
3638
"alchemy language",
37-
"alchemy data"
39+
"alchemy datanews "
3840
],
3941
"author": "IBM Corp.",
4042
"contributors": [
@@ -60,6 +62,7 @@
6062
"coveralls": "~2.11.2",
6163
"istanbul": "~0.3.15",
6264
"nock": "~2.10.0",
65+
"jshint": "~2.8.0",
6366
"qs": "~4.0.0"
6467
},
6568
"dependencies": {
@@ -68,7 +71,6 @@
6871
"csv-stringify": "~0.0.8",
6972
"extend": "~3.0.0",
7073
"isstream": "~0.1.2",
71-
"jshint": "^2.8.0",
7274
"object.omit": "~2.0.0",
7375
"object.pick": "~1.1.1",
7476
"request": "~2.61.0",

services/alchemy_data_news/v1.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var extend = require('extend');
20+
var requestFactory = require('../../lib/requestwrapper');
21+
var endpoints = require('../../lib/alchemy_endpoints.json');
22+
var helper = require('../../lib/helper');
23+
24+
function errorFormatter(cb) {
25+
return function(err, result, response) {
26+
if (err) {
27+
cb(err, result);
28+
}
29+
else {
30+
if (result.status === 'OK')
31+
cb(err,result);
32+
else
33+
cb({
34+
error: result.statusInfo || response['headers']['x-alchemyapi-error-msg'],
35+
code: 400
36+
}, null);
37+
}
38+
};
39+
}
40+
41+
function createRequest(method) {
42+
return function(_params, callback ) {
43+
var params = _params || {};
44+
var accepted_formats = Object.keys(endpoints[method]);
45+
var format = helper.getFormat(params, accepted_formats);
46+
47+
if (format === null) {
48+
callback(new Error('Missing required parameters: ' +
49+
accepted_formats.join(', ') +
50+
' needs to be specified'));
51+
return;
52+
}
53+
54+
var parameters = {
55+
options: {
56+
url: endpoints[method][format],
57+
method: 'POST',
58+
json: true,
59+
form: extend({outputMode: 'json'}, params) // change default output to json
60+
},
61+
defaultOptions: this._options
62+
};
63+
return requestFactory(parameters, errorFormatter(callback));
64+
};
65+
}
66+
67+
function AlchemyDataNews(options) {
68+
// Default URL
69+
var serviceDefaults = {
70+
url: 'https://access.alchemyapi.com/calls'
71+
};
72+
// Replace default options with user provided
73+
this._options = extend(serviceDefaults, options);
74+
}
75+
76+
/**
77+
* Extracts a grouped, ranked list of named entities (people, companies,
78+
* organizations, etc.) from text, a URL or HTML.
79+
*/
80+
AlchemyDataNews.prototype.getNews = function(params, callback ) {
81+
params = params || {};
82+
83+
var parameters = {
84+
options: {
85+
url: '/data/GetNews',
86+
method: 'GET',
87+
json: true,
88+
qs: extend({}, params, {outputMode: 'json'}) // change default output to json
89+
},
90+
requiredParams: ['end','start'],
91+
defaultOptions: this._options
92+
};
93+
return requestFactory(parameters, errorFormatter(callback));
94+
};
95+
96+
module.exports = AlchemyDataNews;

services/alchemy_language/v1.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function errorFormatter(cb) {
3636
}, null);
3737
}
3838
};
39-
};
39+
}
4040

4141
function createRequest(method) {
4242
return function(_params, callback ) {
@@ -56,8 +56,7 @@ function createRequest(method) {
5656
url: endpoints[method][format],
5757
method: 'POST',
5858
json: true,
59-
path: params,
60-
form: extend({outputMode: 'json'}, params) // change default output to json
59+
form: extend({}, params, {outputMode: 'json'}) // change default output to json
6160
},
6261
defaultOptions: this._options
6362
};

0 commit comments

Comments
 (0)