Skip to content

Commit b04a5eb

Browse files
authored
Merge pull request #76 from Katsuya-Tomioka/DEVOPS-247-address-similarity
DEVOPS-247: support address-similarity
2 parents 22f6232 + 86c4993 commit b04a5eb

File tree

6 files changed

+146
-8
lines changed

6 files changed

+146
-8
lines changed

examples/address_similarity.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 similarity score of two names"
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+
var api = new Api(args.key, args.url);
14+
var endpoint = "addressSimilarity";
15+
16+
api.parameters.address1 = {"city": "Cambridge"}
17+
api.parameters.address2 = {"city": "cambridge", "state": "ma"}
18+
19+
api.rosette(endpoint, function(err, res){
20+
if(err){
21+
console.log(err);
22+
} else {
23+
console.log(JSON.stringify(res, null, 2));
24+
}
25+
});

lib/Api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Rosette API.
33
*
4-
* @copyright 2016-2018 Basis Technology Corporation.
4+
* @copyright 2016-2019 Basis Technology Corporation.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
77
* with the License. You may obtain a copy of the License at
@@ -21,6 +21,7 @@ var rosetteConstants = require("./rosetteConstants");
2121
var RosetteException = require("./rosetteExceptions");
2222
var paramObj = require("./parameters");
2323

24+
var addressSimilarity = require("./addressSimilarity")
2425
var categories = require("./categories");
2526
var entities = require("./entities");
2627
var info = require("./info");

lib/addressSimilarity.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Rosette API.
3+
*
4+
* @copyright 2019 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 addressSimilarity() {
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+
addressSimilarity.prototype.getResults = function(parameters, userKey, protocol, serviceURL, callback) {
40+
41+
if (parameters.documentFile != null) {
42+
return callback(new RosetteException("badArgument", "Address similarity does not support documentFile", "bad arguments"));
43+
} else {
44+
// validate parameters
45+
if (!parameters.loadParams().address1 || !parameters.loadParams().address2) {
46+
return callback(new RosetteException("badArgument", "Must supply both address1 and address2 parameters to be matched."));
47+
} else {
48+
// configure URL
49+
var urlParts = URL.parse(serviceURL + "address-similarity");
50+
var req = new rosetteRequest();
51+
req.makeRequest('POST', userKey, protocol, urlParts, parameters, callback);
52+
}
53+
}
54+
};
55+
56+
module.exports = addressSimilarity;

lib/parameters.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Rosette API.
33
*
4-
* @copyright 2016-2018 Basis Technology Corporation.
4+
* @copyright 2016-2019 Basis Technology Corporation.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
77
* with the License. You may obtain a copy of the License at
@@ -48,6 +48,10 @@ function parameters() {
4848
this.genre = null;
4949
this.options = null;
5050

51+
// address similarity parameters
52+
this.address1 = null;
53+
this.address2 = null;
54+
5155
// name matching parameters
5256
this.name1 = null;
5357
this.name2 = null;
@@ -98,6 +102,8 @@ parameters.prototype.loadParams = function() {
98102
"contentUri": this.contentUri,
99103
"language": this.language,
100104
"genre": this.genre,
105+
"address1": this.address1,
106+
"address2": this.address2,
101107
"name1": this.name1,
102108
"name2": this.name2,
103109
"name": this.name,

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rosette-api",
3-
"version": "1.12.2",
3+
"version": "1.14.3",
44
"description": "Rosette API Node.js client SDK",
55
"main": "index",
66
"directories": {
@@ -12,6 +12,7 @@
1212
"url": "git://github.com/rosette-api/nodejs.git"
1313
},
1414
"keywords": [
15+
"address similarity",
1516
"categorization",
1617
"entity extraction",
1718
"language detection",
@@ -46,10 +47,10 @@
4647
"argparse": "^1.0.6",
4748
"chai": "^3.5.0",
4849
"eslint": "^6.0.1",
49-
"grunt": "^0.4.5",
50+
"grunt": "^1.0.4",
5051
"grunt-contrib-clean": "^1.0.0",
51-
"grunt-contrib-watch": "^0.6.1",
52-
"grunt-gh-pages": "^1.0.0",
52+
"grunt-contrib-watch": "^1.1.0",
53+
"grunt-gh-pages": "^3.1.0",
5354
"grunt-jsdoc": "^1.1.0",
5455
"grunt-jslint": "^1.1.14",
5556
"grunt-mocha-test": "^0.12.7",

tests/unittests.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
"use strict"
1+
"use strict";
22

33
var chai = require('chai');
44
var mocha = require('mocha');
55
var nock = require('nock');
66
var fs = require('fs');
77

88
var Api = require("../lib/Api");
9+
var addressSimilarity = require("../lib/addressSimilarity");
910
var language = require("../lib/language");
1011
var relationships = require("../lib/relationships");
1112
var nameDeduplication = require("../lib/nameDeduplication");
@@ -143,6 +144,55 @@ describe("Relationships Endpoint", function() {
143144
});
144145
});
145146

147+
describe("Address Similarity Endpoint", function() {
148+
beforeEach(function(done) {
149+
var mockResponse = JSON.stringify({'name': 'Rosette API', 'versionChecked': true});
150+
151+
nock('https://api.rosette.com', {"encodedQueryParams": true })
152+
.post('/rest/v1/info')
153+
.query({"clientVersion": "1.14.3"})
154+
.reply(200, JSON.parse(mockResponse));
155+
156+
nock('https://api.rosette.com', {"encodedQueryParams": true })
157+
.post('/rest/v1/address-similarity')
158+
.query({"clientVersion": "1.14.3"})
159+
.reply(200, JSON.parse(mockResponse));
160+
done();
161+
});
162+
163+
afterEach(function(done) {
164+
nock.cleanAll();
165+
done();
166+
});
167+
168+
it("successfully calls the address similarity endpoint", function(done) {
169+
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
170+
171+
api.parameters.address1 = {"city": "cambridge", "state": "ma"};
172+
api.parameters.address2 = {"city": "Cambridge", "road": "1 Kendall sq."};
173+
174+
api.rosette("addressSimilarity", function(err, res) {
175+
chai.expect(err).to.be.null;
176+
chai.expect(res.name).to.equal('Rosette API');
177+
done();
178+
});
179+
180+
});
181+
182+
it("detects missing name parameter", function(done) {
183+
var api = new Api('123456789', 'https://api.rosette.com/rest/v1');
184+
185+
api.parameters.address1 = {"city": "cambridge", "state": "ma"};
186+
187+
api.rosette("addressSimilarity", function(err, res) {
188+
chai.expect(err).to.not.be.null;
189+
chai.expect(err.name).to.equal('RosetteException');
190+
chai.expect(err.message).to.contain('badArgument');
191+
done();
192+
});
193+
});
194+
});
195+
146196
describe("Name Deduplication Endpoint", function() {
147197
beforeEach(function(done) {
148198
var mockResponse = JSON.stringify({'name': 'Rosette API', 'versionChecked': true});
@@ -1047,4 +1097,3 @@ describe("Topics Endpoint", function() {
10471097
});
10481098
});
10491099
});
1050-

0 commit comments

Comments
 (0)