Skip to content

Commit

Permalink
Splitting language translation & translator into seperate services, a…
Browse files Browse the repository at this point in the history
…dding a warning when using translator without a url because the default is incorrect and will change soon
  • Loading branch information
nfriedly committed Oct 12, 2016
1 parent edc6b0b commit ba6ee74
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 45 deletions.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,32 @@ document_conversion.convert({
See the [Document Conversion integration example][document_conversion_integration_example] about how to integrate the Document Conversion service
with the Retrieve and Rank service.


### Language Translation

The IBM Watson™ Language Translation service has been rebranded as the Language Translator service.
The Language Translator service provides the same capabilities as the Language Translation service, but with simpler pricing.
For information about migrating existing applications from the Language Translation service to the Language Translator service, see the
[Migration documentation](http://www.ibm.com/watson/developercloud/doc/language-translator/migrating.shtml)

```javascript
var LanguageTranslationV2 = require('watson-developer-cloud/language-translation/v2');

var language_translation = new LanguageTranslationV2({
username: '<username>',
password: '<password>'
});
```

### Language Translator

Translate text from one language to another or idenfity a language using the [Language Translator][language_translator] service.

**Note:** There is a deprecated Language *Translation* service and a newer Language *Translator* service. The only difference is the pricing structure and the service endpoint. The SDK currently defaults to the older endpoint, but that will change in the near future. **To guarentee compatibility, include the `url` when creating a LanguageTranslatorV2 instance.** See http://www.ibm.com/watson/developercloud/doc/language-translator/migrating.shtml for more details.
**Note:** There is a deprecated Language *Translation* service and a newer Language *Translator* service.
The only difference is the pricing structure and the service endpoint.
The SDK currently defaults to the older endpoint for both `LanguageTranslationV2` and `LanguageTranslatorV2`, but `LanguageTranslatorV2`'s default endpoint will change in the next major release (3.0.0).
**To guarantee compatibility, include the `url` when creating a `LanguageTranslatorV2` instance.**
See http://www.ibm.com/watson/developercloud/doc/language-translator/migrating.shtml for more details.

```javascript
var LanguageTranslatorV2 = require('watson-developer-cloud/language-translator/v2');
Expand Down Expand Up @@ -326,21 +347,6 @@ language_translator.identify({
});
```

To use with the deprecated Language Translation service, pass in the older endpoint:

```javascript
var LanguageTranslatorV2 = require('watson-developer-cloud/language-translator/v2');

var language_translation = new LanguageTranslatorV2({
username: '<username>',
password: '<password>',
url: 'https://gateway.watsonplatform.net/language-translation/api'
});
```




### Natural Language Classifier

Use [Natural Language Classifier](http://www.ibm.com/watson/developercloud/doc/nl-classifier/) service to create a classifier instance by providing a set of representative strings and a set of one or more correct classes for each as training. Then use the trained classifier to classify your new question for best matching answers or to retrieve next actions for your application.
Expand Down
14 changes: 1 addition & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports.DialogV1 = require('./dialog/v1');

exports.DocumentConversionV1 = require('./document-conversion/v1');

exports.LanguageTranslationV2 = require('./language-translation/v2');
exports.LanguageTranslatorV2 = require('./language-translator/v2');

exports.NaturalLanguageClassifierV1 = require('./natural-language-classifier/v1');
Expand Down Expand Up @@ -103,16 +104,3 @@ Object.keys(servicesByVersion).forEach(function(serviceName) {
}
});
});

Object.defineProperty(exports, 'language_translation', {
enumerable: false,
configurable: true,
writable: true,
value: function(options) {
if (!options.silent) {
//eslint-disable-next-line no-console
console.warn(new Error("Watson language_translation is now language_translator. Set {silent: true} to disable this message.").stack)
}
return exports.language_translator(options);
}
});
222 changes: 222 additions & 0 deletions language-translation/v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

'use strict';

var requestFactory = require('../lib/requestwrapper');
var extend = require('extend');
var pick = require('object.pick');
var isStream = require('isstream');
var helper = require('../lib/helper');
var util = require('util');
var BaseService = require('../lib/base_service');

/**
*
* @param {string} [params.url=https://gateway.watsonplatform.net/language-translation/api] The service URL.
* @param {string} params.username Username
* @param {string} params.password Password
* @constructor
*/
function LanguageTranslationV2(options) {
BaseService.call(this, options);
}
util.inherits(LanguageTranslationV2, BaseService);
LanguageTranslationV2.prototype.name = 'language_translation';
LanguageTranslationV2.prototype.version = 'v2';
LanguageTranslationV2.URL = 'https://gateway.watsonplatform.net/language-translation/api';

/**
* Return the translation models
*/

/**
* Return the translation models
* @param {string} params.default Query filters to check if the model is
* the default one used when only source
* and target languages are specified.
* @param {string} params.source Filter by source language
* @param {string} params.target Filter by target language
*/
LanguageTranslationV2.prototype.getModels = function(params, callback) {
params = params || {};

var parameters = {
options: {
method: 'GET',
url: '/v2/models',
qs: pick(params,['default','source','target']),
json: true
},
defaultOptions: this._options
};
return requestFactory(parameters, callback);
};

/**
* Return the translation model
* @param {string} params.model_id The model identifier
*/
LanguageTranslationV2.prototype.getModel = function(params, callback) {
params = params || {};

var parameters = {
options: {
method: 'GET',
url: '/v2/models/{model_id}',
path: pick(params,['model_id']),
json: true
},
requiredParams: ['model_id'],
defaultOptions: this._options
};
return requestFactory(parameters, callback);
};

/**
* Creates a translation model
* @param {string} params.base_model_id The base model identifier
* @param {string} params.name The model name
* @param {stream} params.forced_glossary A UTF-8 encoded TMX file that contains pairs of matching terms in the source and target language that are seen as absolute by the system. This file completely overwrites the original domain data.
* @param {stream} params.parallel_corpus A UTF-8 encoded TMX file that contains matching phrases in the source and target language that serve as examples for Watson. Parallel corpora differ from glossaries because they do not overwrite the original domain data.
* @param {stream} params.monolingual_corpus A UTF-8 encoded plain text file that contains a body of text in the target language that is related to what you are translating. A monolingual corpus helps improve literal translations to be more fluent and human.
*/

LanguageTranslationV2.prototype.createModel = function(params, callback) {
params = params || {};

var missingParams = helper.getMissingParams(params, ['base_model_id']);
if (missingParams) {
callback(new Error('Missing required parameters: ' + missingParams.join(', ')));
return;
}

var inputTypes = ['forced_glossary', 'parallel_corpus', 'monolingual_corpus'];

for (var type in inputTypes) {
if (inputTypes.hasOwnProperty(type)) {
if (params[inputTypes[type]] && !isStream(params[inputTypes[type]])) {
callback(new Error(inputTypes[type] + ' is not a standard Node.js Stream'));
return;
}
}
}

var parameters = {
options: {
method: 'POST',
url: '/v2/models',
qs: pick(params,['name', 'base_model_id']),
formData: pick(params, inputTypes),
json: true
},
defaultOptions: this._options
};
return requestFactory(parameters, callback);
};

/**
* Deletes a model
* @param {string} params.model_id The model identifier
*/
LanguageTranslationV2.prototype.deleteModel = function(params, callback) {
params = params || {};

var parameters = {
options: {
method: 'DELETE',
url: '/v2/models/{model_id}',
path: pick(params,['model_id']),
json: true
},
requiredParams: ['model_id'],
defaultOptions: this._options
};
return requestFactory(parameters, callback);
};

/**
* Translate pharagraphs from one language into another
* @param {string} params.source Source language
* @param {string} params.target Target language
*/
LanguageTranslationV2.prototype.translate = function(params, callback) {
params = params || {};

if (!(params.model_id || (params.source && params.target))){
callback(new Error('Missing required parameters: model_id or source and target'));
return;
}
var parameters = {
options: {
url: '/v2/translate',
method: 'POST',
json: true,
body: pick(params, ['source','target','text','model_id'])
},
requiredParams: ['text'],
defaultOptions: this._options
};

return requestFactory(parameters, callback);
};

/**
* Returns the identifiable languages
*/
LanguageTranslationV2.prototype.getIdentifiableLanguages = function(params, callback) {
params = params || {};

var parameters = {
options: {
url: '/v2/identifiable_languages',
method: 'GET'
},
defaultOptions: this._options
};

return requestFactory(parameters, callback);
};


/**
* Identify the text based on the identifiable languages
* @param {string} params.text text to identify
*/
LanguageTranslationV2.prototype.identify = function(params, callback) {
if (!params || !params.text){
callback(new Error('Missing required parameters: text'));
return;
}

var parameters = {
options: {
url: '/v2/identify',
method: 'POST',
body: params.text
},
defaultOptions: extend(true, {}, this._options, {
headers: {
'accept':'application/json',
'content-type': 'plain/text'
}
})
};

return requestFactory(parameters, callback);
};

module.exports = LanguageTranslationV2;
28 changes: 17 additions & 11 deletions language-translator/v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,28 @@ var BaseService = require('../lib/base_service');
* @constructor
*/
function LanguageTranslatorV2(options) {
// Welp, this is awkward. Originally the rename was *just* a rename, but then (after the SDK was updated,
// but before the backend was updated), it was decided that the billing should be simplified at the same time.
// That's a solid improvement, but it means that the SDK now needs to support both services independently,
// and correcting the default URL here will break older code, so it must be reserved for a major release.
// todo: consider checking for options.url === LanguageTranslationV2.URL and also throw this warning then.
// (This probably does't matter since the api didn't change)
if (!options.url) {
var err = new Error("LanguageTranslatorV2 currently defaults to the url for LanguageTranslationV2, " +
"but this will change in the next major release of the watson-developer-cloud Node.js SDK." +
"Please either specify the url https://gateway.watsonplatform.net/language-translator/api/v2/ or else use " +
"LanguageTranslationV2. " +
"See http://www.ibm.com/watson/developercloud/doc/language-translator/migrating.shtml for more details.");
// eslint-disable-next-line no-console
console.warn(err);
}

BaseService.call(this, options);
}
util.inherits(LanguageTranslatorV2, BaseService);
LanguageTranslatorV2.prototype.name = 'language_translator';
LanguageTranslatorV2.prototype.version = 'v2';
LanguageTranslatorV2.URL = 'https://gateway.watsonplatform.net/language-translation/api';

LanguageTranslatorV2.prototype.getCredentialsFromBluemix = function(name) {
return extend(
{},
// the Language Translator service was formerly named Language Translation,
// and there are still old instances floating around with credentials that specify the old name
BaseService.prototype.getCredentialsFromBluemix.call(this, 'language_translation'),
BaseService.prototype.getCredentialsFromBluemix.call(this, name)
);
};
LanguageTranslatorV2.URL = 'https://gateway.watsonplatform.net/language-translation/api'; // This is incorrect and will change in v 3.0.0

/**
* Return the translation models
Expand Down
Loading

0 comments on commit ba6ee74

Please sign in to comment.