Skip to content

Commit a445c3e

Browse files
committed
Add /address-similarity
1 parent 50da083 commit a445c3e

File tree

10 files changed

+305
-26
lines changed

10 files changed

+305
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
/test/version_tmp/
1212
/tmp/
1313

14+
# Jetbrains
15+
**/.idea/*
16+
1417
## Specific to RubyMotion:
1518
.dat*
1619
.repl_history

.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Style/FrozenStringLiteralComment:
143143
# Configuration parameters: MinBodyLength.
144144
Style/GuardClause:
145145
Exclude:
146+
- 'lib/address_similarity_parameters.rb'
146147
- 'lib/name_similarity_parameters.rb'
147148
- 'lib/name_translation_parameters.rb'
148149
- 'lib/request_builder.rb'
@@ -230,6 +231,8 @@ Style/RaiseArgs:
230231
# Cop supports --auto-correct.
231232
Style/RedundantSelf:
232233
Exclude:
234+
- 'lib/address_parameter.rb'
235+
- 'lib/address_similarity_parameters.rb'
233236
- 'lib/document_parameters.rb'
234237
- 'lib/name_parameter.rb'
235238
- 'lib/name_similarity_parameters.rb'
@@ -239,6 +242,7 @@ Style/RedundantSelf:
239242
# Cop supports --auto-correct.
240243
Style/SpaceAfterColon:
241244
Exclude:
245+
- 'examples/address_similarity.rb'
242246
- 'examples/name_similarity.rb'
243247

244248
# Offense count: 2
@@ -265,6 +269,7 @@ Style/SpaceInsideHashLiteralBraces:
265269
Style/StringLiterals:
266270
Exclude:
267271
- 'Gemfile'
272+
- 'examples/address_similarity.rb'
268273
- 'examples/categories.rb'
269274
- 'examples/entities.rb'
270275
- 'examples/language.rb'
@@ -284,6 +289,7 @@ Style/StringLiterals:
284289
# SupportedStyles: final_newline, final_blank_line
285290
Style/TrailingBlankLines:
286291
Exclude:
292+
- 'examples/address_similarity.rb'
287293
- 'examples/info.rb'
288294
- 'examples/language.rb'
289295
- 'examples/morphology_compound-components.rb'

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Basis Technology Corp
22

3-
Copyright (C) 2015-2015 by Basis Technology Corp and the contributors
3+
Copyright (C) 2015-2019 by Basis Technology Corp and the contributors
44

55
Complete list of developers available at our web site:
66

examples/address_similarity.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'rosette_api'
2+
3+
api_key, url = ARGV
4+
5+
if !url
6+
rosette_api = RosetteAPI.new(api_key)
7+
else
8+
rosette_api = RosetteAPI.new(api_key, url)
9+
end
10+
11+
begin
12+
address1 = AddressParameter.new(
13+
'houseNumber': '1600',
14+
'road': 'Pennsylvania Ave NW',
15+
'city': 'Washington',
16+
'state': 'DC',
17+
'postCode': '20500'
18+
)
19+
address2 = AddressParameter.new(
20+
'houseNumber': '160',
21+
'road': 'Pennsilvana Avenue',
22+
'city': 'Washington',
23+
'state': 'D.C.',
24+
'postCode': '20500'
25+
)
26+
params = AddressSimilarityParameters.new(address1, address2)
27+
response = rosette_api.get_address_similarity(params)
28+
puts JSON.pretty_generate(response)
29+
rescue RosetteAPIError => rosette_api_error
30+
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
31+
end

examples/docker/run_ruby.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function checkAPI {
3030

3131
#Checks for valid url
3232
function validateURL() {
33-
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette API")
33+
match=$(curl "${ping_url}/ping" -H "X-RosetteAPI-Key: ${API_KEY}" | grep -o "Rosette at your service")
3434
if [ "${match}" = "" ]; then
3535
echo -e "\n${ping_url} server not responding\n"
3636
exit 1

lib/address_parameter.rb

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# This class represents an address in Rosette API.
2+
class AddressParameter
3+
# house (optional)
4+
attr_accessor :house
5+
# houseNumber (optional)
6+
attr_accessor :houseNumber
7+
# road (optional)
8+
attr_accessor :road
9+
# unit (optional)
10+
attr_accessor :unit
11+
# level (optional)
12+
attr_accessor :level
13+
# staircase (optional)
14+
attr_accessor :staircase
15+
# entrance (optional)
16+
attr_accessor :entrance
17+
# suburb (optional)
18+
attr_accessor :suburb
19+
# cityDistrict (optional)
20+
attr_accessor :cityDistrict
21+
# city (optional)
22+
attr_accessor :city
23+
# island (optional)
24+
attr_accessor :island
25+
# stateDistrict (optional)
26+
attr_accessor :stateDistrict
27+
# state (optional)
28+
attr_accessor :state
29+
# countryRegion (optional)
30+
attr_accessor :countryRegion
31+
# country (optional)
32+
attr_accessor :country
33+
# worldRegion (optional)
34+
attr_accessor :worldRegion
35+
# postCode (optional)
36+
attr_accessor :postCode
37+
# poBox (optional)
38+
attr_accessor :poBox
39+
40+
def initialize(options = {}) #:notnew:
41+
options = {
42+
house: nil,
43+
houseNumber: nil,
44+
road: nil,
45+
unit: nil,
46+
level: nil,
47+
staircase: nil,
48+
entrance: nil,
49+
suburb: nil,
50+
cityDistrict: nil,
51+
city: nil,
52+
island: nil,
53+
stateDistrict: nil,
54+
state: nil,
55+
countryRegion: nil,
56+
country: nil,
57+
worldRegion: nil,
58+
postCode: nil,
59+
poBox: nil
60+
}.update options
61+
@house = options[:house]
62+
@houseNumber = options[:houseNumber]
63+
@road = options[:road]
64+
@unit = options[:unit]
65+
@level = options[:level]
66+
@staircase = options[:staircase]
67+
@entrance = options[:entrance]
68+
@suburb = options[:suburb]
69+
@cityDistrict = options[:cityDistrict]
70+
@city = options[:city]
71+
@island = options[:island]
72+
@stateDistrict = options[:stateDistrict]
73+
@state = options[:state]
74+
@countryRegion = options[:countryRegion]
75+
@country = options[:country]
76+
@worldRegion = options[:worldRegion]
77+
@postCode = options[:postCode]
78+
@poBox = options[:poBox]
79+
end
80+
81+
# Converts this class to Hash with its keys in lower CamelCase.
82+
#
83+
# Returns the new Hash.
84+
def load_param
85+
to_hash.select { |_key, value| value }
86+
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
87+
.to_h
88+
end
89+
90+
# Converts this class to Hash.
91+
#
92+
# Returns the new Hash.
93+
def to_hash
94+
{
95+
house: @house,
96+
houseNumber: @houseNumber,
97+
road: @road,
98+
unit: @unit,
99+
level: @level,
100+
staircase: @staircase,
101+
entrance: @entrance,
102+
suburb: @suburb,
103+
cityDistrict: @cityDistrict,
104+
city: @city,
105+
island: @island,
106+
stateDistrict: @stateDistrict,
107+
state: @state,
108+
countryRegion: @countryRegion,
109+
country: @country,
110+
worldRegion: @worldRegion,
111+
postCode: @postCode,
112+
poBox: @poBox
113+
}
114+
end
115+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require_relative 'bad_request_error'
2+
require_relative 'address_parameter'
3+
4+
# This class encapsulates parameters that are needed for address-similarity in
5+
# Rosette API.
6+
class AddressSimilarityParameters
7+
# Address to be compared to address2
8+
attr_accessor :address1
9+
# Address to be compared to address1
10+
attr_accessor :address2
11+
12+
def initialize(address1, address2) #:notnew:
13+
@address1 = address1
14+
@address2 = address2
15+
end
16+
17+
# Validates the parameters by checking if address1 and address2 are instances of AddressParameter.
18+
def validate_params
19+
raise BadRequestError.new('address1 option can only be an instance of an AddressParameter') if [AddressParameter].none? { |clazz| @address1.is_a? clazz }
20+
raise BadRequestError.new('address2 option can only be an instance of an AddressParameter') if [AddressParameter].none? { |clazz| @address2.is_a? clazz }
21+
end
22+
23+
# Converts this class to Hash with its keys in lower CamelCase.
24+
#
25+
# Returns the new Hash.
26+
def load_params
27+
validate_params
28+
to_hash.reject { |_key, value| value.nil? }
29+
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
30+
.to_h
31+
end
32+
33+
# Converts this class to Hash.
34+
#
35+
# Returns the new Hash.
36+
def to_hash
37+
{
38+
address1: @address1.is_a?(AddressParameter) ? @address1.load_param : @address1,
39+
address2: @address2.is_a?(AddressParameter) ? @address2.load_param : @address2
40+
}
41+
end
42+
end

lib/rosette_api.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
require_relative 'name_deduplication_parameters'
44
require_relative 'name_translation_parameters'
55
require_relative 'name_similarity_parameters'
6+
require_relative 'address_similarity_parameters'
67
require_relative 'rosette_api_error'
78
require_relative 'bad_request_error'
89
require_relative 'bad_request_format_error'
910

1011
# This class allows you to access all Rosette API endpoints.
1112
class RosetteAPI
1213
# Version of Ruby binding
13-
BINDING_VERSION = '1.12.1'
14+
BINDING_VERSION = '1.14.3'
1415
# Rosette API language endpoint
1516
LANGUAGE_ENDPOINT = '/language'.freeze
1617
# Rosette API morphology endpoint
@@ -29,6 +30,8 @@ class RosetteAPI
2930
NAME_TRANSLATION_ENDPOINT = '/name-translation'.freeze
3031
# Rosette API name-similarity endpoint
3132
NAME_SIMILARITY_ENDPOINT = '/name-similarity'.freeze
33+
# Rosette API address-similarity endpoint
34+
ADDRESS_SIMILARITY_ENDPOINT = '/address-similarity'.freeze
3235
# Rosette API tokens endpoint
3336
TOKENS_ENDPOINT = '/tokens'.freeze
3437
# Rosette API sentences endpoint
@@ -289,6 +292,22 @@ def get_name_similarity(params)
289292
.send_post_request
290293
end
291294

295+
# Compares two addresses and returns a match score from 0 to 1.
296+
#
297+
# ==== Attributes
298+
#
299+
# * +params+ - AddressSimilarityParameters helps to build the request body in RequestBuilder.
300+
#
301+
# Returns the confidence score of matching 2 addresses.
302+
def get_address_similarity(params)
303+
check_params params, 'Expects a AddressSimilarityParameters type as an argument', AddressSimilarityParameters
304+
305+
params = params.load_params
306+
307+
RequestBuilder.new(@user_key, @alternate_url + ADDRESS_SIMILARITY_ENDPOINT, @http_client, params, @url_parameters, BINDING_VERSION)
308+
.send_post_request
309+
end
310+
292311
# Divides the input into tokens.
293312
#
294313
# ==== Attributes

rosette_api.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
88
s.required_ruby_version = '>= 2.0.0'
99

1010
s.name = 'rosette_api'
11-
s.version = '1.12.1'
11+
s.version = '1.14.3'
1212
s.license = 'MIT'
1313

1414
s.summary = 'Rosette API gem that supports multilingual text-analytics.'

0 commit comments

Comments
 (0)