Skip to content

Commit cdff079

Browse files
iredpathguruxu
authored andcommitted
RD-2427 related terms endpoint (#71)
* RD-2427 related terms endpoint * RD-2427 relatedTerms -> similarTerms, support both textEmbeddings and semanticVectors
1 parent 083fa05 commit cdff079

File tree

7 files changed

+210
-6
lines changed

7 files changed

+210
-6
lines changed
File renamed without changes.

examples/similar_terms.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
var Api = require("../lib/Api");
4+
var ArgumentParser = require("argparse").ArgumentParser;
5+
6+
var parser = new ArgumentParser({
7+
addHelp: true,
8+
description: "Get the terms similar to an input in other languages"
9+
});
10+
parser.addArgument(["--key"], {help: "Rosette API key", required: true});
11+
parser.addArgument(["--url"], {help: "Rosette API alt-url", required: false});
12+
var args = parser.parseArgs();
13+
14+
var api = new Api(args.key, args.url);
15+
var endpoint = "similarTerms";
16+
var data = "spy"
17+
var options = {"resultLanguages": ["spa", "deu", "jpn"]}
18+
19+
api.parameters.content = data;
20+
api.parameters.options = options;
21+
api.rosette(endpoint, function(err, res){
22+
if(err){
23+
console.log(err);
24+
} else {
25+
console.log(JSON.stringify(res, null, 2));
26+
}
27+
});

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ module.exports.tokens = require("./lib/tokens");
2929
module.exports.translatedName = require("./lib/nameTranslation");
3030

3131
module.exports.syntax_dependencies = require("./lib/syntax_dependencies");
32+
33+
module.exports.similarTerms = require("./lib/similarTerms");
34+
35+
module.exports.semanticVectors = require("./lib/semanticVectors");

lib/Api.js

+2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ var morphology = require("./morphology");
3030
var nameDeduplication = require("./nameDeduplication");
3131
var ping = require("./ping");
3232
var relationships = require("./relationships");
33+
var semanticVectors = require("./semanticVectors");
3334
var sentences = require("./sentences");
3435
var sentiment = require("./sentiment");
36+
var similarTerms = require("./similarTerms");
3537
var syntax_dependencies = require("./syntax_dependencies");
3638
var textEmbedding = require("./textEmbedding");
3739
var tokens = require("./tokens");

lib/semanticVectors.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Rosette API.
3+
*
4+
* @copyright 2016-2018 Basis Technology Corporation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
7+
* with the License. You may obtain a copy of the License at
8+
* @license http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
11+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and limitations under the License.
13+
**/
14+
"use strict";
15+
16+
var URL = require("url");
17+
18+
var rosetteConstants = require("./rosetteConstants");
19+
var RosetteException = require("./rosetteExceptions");
20+
var rosetteRequest = require("./rosetteRequest");
21+
22+
/**
23+
* @class
24+
*
25+
* @copyright 2016-2018 Basis Technology Corporation.
26+
* @license http://www.apache.org/licenses/LICENSE-2.0
27+
*/
28+
function semanticVectors() {
29+
30+
};
31+
32+
/**
33+
* Makes an HTTP request to the specified Rosette API endpoint and returns the result
34+
* @param {string} parameters - The Rosette API endpoint parameters
35+
* @param {string} userKey - The Rosette API user access key
36+
* @param {string} serviceURL - The base service URL to be used to access the Rosette API
37+
* @param {function} callback - Callback function to be exectuted after the function to which it is passed is complete
38+
*/
39+
semanticVectors.prototype.getResults = function(parameters, userKey, protocol, serviceURL, callback) {
40+
41+
if (parameters.documentFile != null) {
42+
parameters.loadFile(parameters.loadParams().documentFile, parameters, userKey, protocol, serviceURL, "semantics/vector", callback);
43+
} else {
44+
// validate parameters
45+
if (!parameters.loadParams().content && !parameters.loadParams().contentUri) {
46+
return callback(new RosetteException("badArgument", "Must supply one of Content or ContentUri", "bad arguments"));
47+
} else if (parameters.loadParams().content && parameters.loadParams().contentUri) {
48+
return callback(new RosetteException("badArgument", "Cannot supply both Content and ContentUri", "bad arguments"));
49+
} else {
50+
// configure URL
51+
var urlParts = URL.parse(serviceURL + "semantics/vector");
52+
var req = new rosetteRequest();
53+
req.makeRequest('POST', userKey, protocol, urlParts, parameters, callback);
54+
}
55+
}
56+
};
57+
58+
module.exports = semanticVectors;

lib/similarTerms.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Rosette API.
3+
*
4+
* @copyright 2018 Basis Technology Corporation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
7+
* with the License. You may obtain a copy of the License at
8+
* @license http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
11+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and limitations under the License.
13+
**/
14+
"use strict";
15+
16+
var URL = require("url");
17+
18+
var RosetteConstants = require("./rosetteConstants");
19+
var RosetteException = require("./rosetteExceptions");
20+
var rosetteRequest = require("./rosetteRequest");
21+
22+
/**
23+
* @class
24+
*
25+
* @copyright 2018 Basis Technology Corporation.
26+
* @license http://www.apache.org/licenses/LICENSE-2.0
27+
*/
28+
function similarTerms() {
29+
30+
};
31+
32+
/**
33+
* Makes an HTTP request to the specified Rosette API endpoint and returns the result
34+
* @param {string} parameters - The Rosette API endpoint parameters
35+
* @param {string} userKey - The Rosette API user access key
36+
* @param {string} serviceURL - The base service URL to be used to access the Rosette API
37+
* @param {function} callback - Callback function to be exectuted after the function to which it is passed is complete
38+
*/
39+
similarTerms.prototype.getResults = function(parameters, userKey, protocol, serviceURL, callback) {
40+
41+
if (parameters.documentFile != null) {
42+
parameters.loadFile(parameters.documentFile, parameters, userKey, protocol, serviceURL, "semantics/similar", callback);
43+
} else {
44+
// validate parameters
45+
if (!parameters.loadParams().content && !parameters.loadParams().contentUri) {
46+
return callback(new RosetteException("badArgument", "Must supply one of content or contentUri", "bad arguments"));
47+
} else if (parameters.loadParams().content != null && parameters.loadParams().contentUri != null) {
48+
return callback(new RosetteException("badArgument", "Cannot supply content and contentUri", "bad arguments"));
49+
} else {
50+
// configure URL
51+
var urlParts = URL.parse(serviceURL + "semantics/similar");
52+
var req = new rosetteRequest();
53+
req.makeRequest('POST', userKey, protocol, urlParts, parameters, callback);
54+
}
55+
}
56+
};
57+
58+
module.exports = similarTerms;

tests/unittests.js

+61-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var morphology = require("../lib/morphology");
1818
var tokens = require("../lib/tokens");
1919
var topics = require("../lib/topics");
2020
var sentences = require("../lib/sentences");
21+
var similarTerms = require("../lib/similarTerms");
22+
var semanticVectors = require("../lib/semanticVectors");
2123
var info = require("../lib/info");
2224
var ping = require("../lib/ping");
2325
var syntax_dependencies = require("../lib/syntax_dependencies");
@@ -686,12 +688,65 @@ describe("Sentences Endpoint", function() {
686688
});
687689
});
688690

689-
describe("Text Embedding Endpoint", function() {
691+
describe("Similar Terms Endpoint", function() {
690692
beforeEach(function(done) {
691693
var mockResponse = JSON.stringify({'name': 'Rosette API', 'versionChecked': true});
692694

693695
nock('https://api.rosette.com', {"encodedQueryParams": true })
694-
.post('/rest/v1/text-embedding')
696+
.post('/rest/v1/semantics/similar')
697+
.query({"clientVersion": "1.1"})
698+
.reply(200, JSON.parse(mockResponse));
699+
done()
700+
});
701+
702+
afterEach(function(done) {
703+
nock.cleanAll();
704+
done();
705+
});
706+
707+
it("successfully calls the similarTerms endpoint", function(done) {
708+
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
709+
api.parameters.content = "Some Content";
710+
711+
api.rosette("similarTerms", function(err, res) {
712+
chai.expect(err).to.be.null;
713+
chai.expect(res.name).to.equal('Rosette API');
714+
done();
715+
});
716+
717+
});
718+
719+
it("detects content and contentUri are defined", function(done) {
720+
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
721+
api.parameters.content = "Sample Content";
722+
api.parameters.contentUri = "http://some.url.com";
723+
724+
api.rosette("similarTerms", function(err, res) {
725+
chai.expect(err).to.not.be.null;
726+
chai.expect(err.name).to.equal('RosetteException');
727+
chai.expect(err.message).to.contain('badArgument');
728+
done();
729+
});
730+
});
731+
732+
it("detects neither content nor contentUri are defined", function(done) {
733+
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
734+
735+
api.rosette("similarTerms", function(err, res) {
736+
chai.expect(err).to.not.be.null;
737+
chai.expect(err.name).to.equal('RosetteException');
738+
chai.expect(err.message).to.contain('badArgument');
739+
done();
740+
});
741+
});
742+
});
743+
744+
describe("Semantic Vectors Endpoint", function() {
745+
beforeEach(function(done) {
746+
var mockResponse = JSON.stringify({'name': 'Rosette API', 'versionChecked': true});
747+
748+
nock('https://api.rosette.com', {"encodedQueryParams": true })
749+
.post('/rest/v1/semantics/vector')
695750
.query({"clientVersion": "1.1"})
696751
.reply(200, JSON.parse(mockResponse));
697752
done();
@@ -702,11 +757,11 @@ describe("Text Embedding Endpoint", function() {
702757
done();
703758
});
704759

705-
it("successfully calls the textEmbedding endpoint", function(done) {
760+
it("successfully calls the semanticVectors endpoint", function(done) {
706761
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
707762
api.parameters.content = "Some Content";
708763

709-
api.rosette("textEmbedding", function(err, res) {
764+
api.rosette("semanticVectors", function(err, res) {
710765
chai.expect(err).to.be.null;
711766
chai.expect(res.name).to.equal('Rosette API');
712767
done();
@@ -719,7 +774,7 @@ describe("Text Embedding Endpoint", function() {
719774
api.parameters.content = "Sample Content";
720775
api.parameters.contentUri = "http://some.url.com";
721776

722-
api.rosette("textEmbedding", function(err, res) {
777+
api.rosette("semanticVectors", function(err, res) {
723778
chai.expect(err).to.not.be.null;
724779
chai.expect(err.name).to.equal('RosetteException');
725780
chai.expect(err.message).to.contain('badArgument');
@@ -730,7 +785,7 @@ describe("Text Embedding Endpoint", function() {
730785
it("detects neither content nor contentUri are defined", function(done) {
731786
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
732787

733-
api.rosette("textEmbedding", function(err, res) {
788+
api.rosette("semanticVectors", function(err, res) {
734789
chai.expect(err).to.not.be.null;
735790
chai.expect(err.name).to.equal('RosetteException');
736791
chai.expect(err.message).to.contain('badArgument');

0 commit comments

Comments
 (0)