Skip to content

Commit a105236

Browse files
RLPNC-5039 add support for address similarity
1 parent 3f4aff1 commit a105236

File tree

12 files changed

+495
-2
lines changed

12 files changed

+495
-2
lines changed

api/src/test/java/com/basistech/rosette/api/RosetteAPITest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.basistech.rosette.RosetteRuntimeException;
2020
import com.basistech.rosette.api.common.AbstractRosetteAPI;
21+
import com.basistech.rosette.apimodel.AddressSimilarityRequest;
22+
import com.basistech.rosette.apimodel.AddressSimilarityResponse;
2123
import com.basistech.rosette.apimodel.CategoriesResponse;
2224
import com.basistech.rosette.apimodel.DocumentRequest;
2325
import com.basistech.rosette.apimodel.EntitiesOptions;
@@ -141,7 +143,7 @@ public void setUp() throws Exception {
141143

142144
@Test
143145
public void testMatchName() throws IOException {
144-
if (!(testFilename.endsWith("-matched-name.json"))) {
146+
if (!(testFilename.endsWith("-name-similarity.json"))) {
145147
return;
146148
}
147149
NameSimilarityRequest request = readValueNameMatcher();
@@ -163,6 +165,30 @@ private NameSimilarityRequest readValueNameMatcher() throws IOException {
163165
return mapper.readValue(input, NameSimilarityRequest.class);
164166
}
165167

168+
@Test
169+
public void testMatchAddress() throws IOException {
170+
if (!(testFilename.endsWith("-address-similarity.json"))) {
171+
return;
172+
}
173+
AddressSimilarityRequest request = readValueAddressMatcher();
174+
try {
175+
AddressSimilarityResponse response = api.perform(AbstractRosetteAPI.ADDRESS_SIMILARITY_SERVICE_PATH, request, AddressSimilarityResponse.class);
176+
verifyAddressMatcher(response);
177+
} catch (HttpRosetteAPIException e) {
178+
verifyException(e);
179+
}
180+
}
181+
182+
private void verifyAddressMatcher(AddressSimilarityResponse response) throws IOException {
183+
AddressSimilarityResponse goldResponse = mapper.readValue(responseStr, AddressSimilarityResponse.class);
184+
assertEquals(goldResponse.getScore(), response.getScore(), 0.0);
185+
}
186+
187+
private AddressSimilarityRequest readValueAddressMatcher() throws IOException {
188+
File input = new File("src/test/mock-data/request", testFilename);
189+
return mapper.readValue(input, AddressSimilarityRequest.class);
190+
}
191+
166192
@Test
167193
public void testTranslateName() throws IOException {
168194
if (!(testFilename.endsWith("-translated-name.json"))) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"address1": {
3+
"houseNumber" : "1600",
4+
"road" : "Pennsylvania Ave NW",
5+
"city" : "Washington",
6+
"state" : "DC",
7+
"postCode" : "20500"
8+
},
9+
"address2": {
10+
"houseNumber" : "160",
11+
"road" : "Pennsilvana Ave",
12+
"city" : "Washington",
13+
"state" : "D.C.",
14+
"postCode" : "20500"
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"score": 0.77
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
200

common/src/main/java/com/basistech/rosette/api/common/AbstractRosetteAPI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public abstract class AbstractRosetteAPI implements AutoCloseable {
4242
public static final String NAME_DEDUPLICATION_SERVICE_PATH = "/name-deduplication";
4343
public static final String NAME_TRANSLATION_SERVICE_PATH = "/name-translation";
4444
public static final String NAME_SIMILARITY_SERVICE_PATH = "/name-similarity";
45+
public static final String ADDRESS_SIMILARITY_SERVICE_PATH = "/address-similarity";
4546
public static final String TOKENS_SERVICE_PATH = "/tokens";
4647
public static final String SENTENCES_SERVICE_PATH = "/sentences";
4748
public static final String TEXT_EMBEDDING_SERVICE_PATH = "/text-embedding";
@@ -79,7 +80,8 @@ public abstract class AbstractRosetteAPI implements AutoCloseable {
7980
public static final Set<String> NAMES_ENDPOINTS = new HashSet<>(Arrays.asList(
8081
NAME_SIMILARITY_SERVICE_PATH,
8182
NAME_TRANSLATION_SERVICE_PATH,
82-
NAME_DEDUPLICATION_SERVICE_PATH
83+
NAME_DEDUPLICATION_SERVICE_PATH,
84+
ADDRESS_SIMILARITY_SERVICE_PATH
8385
));
8486

8587
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2014 Basis Technology Corp.
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+
package com.basistech.rosette.examples;
18+
19+
import java.io.IOException;
20+
21+
import com.basistech.rosette.api.HttpRosetteAPI;
22+
import com.basistech.rosette.apimodel.Address;
23+
import com.basistech.rosette.apimodel.AddressSimilarityRequest;
24+
import com.basistech.rosette.apimodel.AddressSimilarityResponse;
25+
26+
/**
27+
* Example which demonstrates address similarity
28+
*/
29+
public final class AddressSimilarityExample extends ExampleBase {
30+
public static void main(String[] args) {
31+
try {
32+
new AddressSimilarityExample().run();
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
System.exit(1);
36+
}
37+
}
38+
39+
private void run() throws IOException {
40+
Address address1 = Address.builder()
41+
.houseNumber("1600")
42+
.road("Pennsylvania Ave NW")
43+
.city("Washington")
44+
.state("DC")
45+
.postCode("20500")
46+
.build();
47+
Address address2 = Address.builder()
48+
.houseNumber("160")
49+
.road("Pennsilvana Ave")
50+
.city("Washington")
51+
.state("DC")
52+
.postCode("20500")
53+
.build();
54+
HttpRosetteAPI rosetteApi = new HttpRosetteAPI.Builder()
55+
.key(getApiKeyFromSystemProperty())
56+
.url(getAltUrlFromSystemProperty())
57+
.build();
58+
//The api object creates an http client, but to provide your own:
59+
//api.httpClient(CloseableHttpClient)
60+
AddressSimilarityRequest request = AddressSimilarityRequest.builder().address1(address1).address2(address2).build();
61+
AddressSimilarityResponse response = rosetteApi.perform(HttpRosetteAPI.ADDRESS_SIMILARITY_SERVICE_PATH, request, AddressSimilarityResponse.class);
62+
System.out.println(responseToJson(response));
63+
}
64+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2014 Basis Technology Corp.
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+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonInclude;
23+
import com.fasterxml.jackson.annotation.JsonProperty;
24+
import com.fasterxml.jackson.annotation.JsonTypeName;
25+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
26+
27+
@JsonTypeName("Address")
28+
@JsonInclude(JsonInclude.Include.NON_NULL)
29+
public abstract class AddressMixin {
30+
31+
@JsonCreator
32+
protected AddressMixin(@JsonProperty("house") String house,
33+
@JsonProperty("houseNumber") String houseNumber,
34+
@JsonProperty("road") String road,
35+
@JsonProperty("unit") String unit,
36+
@JsonProperty("level") String level,
37+
@JsonProperty("staircase") String staircase,
38+
@JsonProperty("entrance") String entrance,
39+
@JsonProperty("suburb") String suburb,
40+
@JsonProperty("cityDistrict") String cityDistrict,
41+
@JsonProperty("city") String city,
42+
@JsonProperty("island") String island,
43+
@JsonProperty("stateDistrict") String stateDistrict,
44+
@JsonProperty("state") String state,
45+
@JsonProperty("countryRegion") String countryRegion,
46+
@JsonProperty("country") String country,
47+
@JsonProperty("worldRegion") String worldRegion,
48+
@JsonProperty("postCode") String postCode,
49+
@JsonProperty("poBox") String poBox,
50+
@JsonProperty("extra") Map<String, String> extra,
51+
@JsonProperty("uid") String uid) {
52+
//
53+
}
54+
55+
@JsonCreator
56+
protected AddressMixin() {
57+
//
58+
}
59+
60+
@JsonPOJOBuilder(withPrefix = "")
61+
abstract class AddressBuilderMixin {
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014 Basis Technology Corp.
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+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonInclude;
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import com.fasterxml.jackson.annotation.JsonTypeName;
23+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
24+
25+
import com.basistech.rosette.apimodel.Address;
26+
27+
@JsonTypeName("AddressSimilarityRequest")
28+
@JsonInclude(JsonInclude.Include.NON_NULL)
29+
public class AddressSimilarityRequestMixin {
30+
31+
@JsonCreator
32+
protected AddressSimilarityRequestMixin(
33+
@JsonProperty("profileId") String profileId,
34+
@JsonProperty("address1") Address address1,
35+
@JsonProperty("address2") Address address2
36+
) {
37+
//
38+
}
39+
40+
@JsonPOJOBuilder(withPrefix = "")
41+
abstract class AddressSimilarityRequestBuilderMixin {
42+
}
43+
}

json/src/main/java/com/basistech/rosette/apimodel/jackson/ApiModelMixinModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.basistech.rosette.apimodel.jackson;
1818

19+
import com.basistech.rosette.apimodel.Address;
20+
import com.basistech.rosette.apimodel.AddressSimilarityRequest;
1921
import com.basistech.rosette.apimodel.AdmRequest;
2022
import com.basistech.rosette.apimodel.DocumentRequest;
2123
import com.basistech.rosette.apimodel.Name;
@@ -57,6 +59,10 @@ public void setupModule(Module.SetupContext context) {
5759
context.setMixInAnnotations(NameTranslationRequest.NameTranslationRequestBuilder.class, NameTranslationRequestMixin.NameTranslationRequestBuilderMixin.class);
5860
context.setMixInAnnotations(NameDeduplicationRequest.class, NameDeduplicationRequestMixin.class);
5961
context.setMixInAnnotations(NameDeduplicationRequest.NameDeduplicationRequestBuilder.class, NameDeduplicationRequestMixin.NameDeduplicationRequestBuilderMixin.class);
62+
context.setMixInAnnotations(Address.class, AddressMixin.class);
63+
context.setMixInAnnotations(Address.AddressBuilder.class, AddressMixin.AddressBuilderMixin.class);
64+
context.setMixInAnnotations(AddressSimilarityRequest.class, AddressSimilarityRequestMixin.class);
65+
context.setMixInAnnotations(AddressSimilarityRequest.AddressSimilarityRequestBuilder.class, AddressSimilarityRequestMixin.AddressSimilarityRequestBuilderMixin.class);
6066
}
6167

6268
/**

0 commit comments

Comments
 (0)