Skip to content

Commit e7dc9a3

Browse files
fhasanajguruxu
authored andcommitted
RLPNC-4887: Made changes to support supported-languages for names (#137)
* RLPNC-4887: Made changes to support supported-languages for names * RLPNC-4887: Updated if condition to use CONSTANT.equals() to avoid NPEs * RLPNC-4887: Changed schemes to all uppercase.
1 parent 69d28eb commit e7dc9a3

File tree

8 files changed

+186
-2
lines changed

8 files changed

+186
-2
lines changed

api/src/main/java/com/basistech/rosette/api/HttpRosetteAPI.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.basistech.rosette.apimodel.PingResponse;
2626
import com.basistech.rosette.apimodel.Request;
2727
import com.basistech.rosette.apimodel.Response;
28+
import com.basistech.rosette.apimodel.SupportedLanguagePairsResponse;
2829
import com.basistech.rosette.apimodel.SupportedLanguagesResponse;
2930
import com.basistech.rosette.apimodel.jackson.ApiModelMixinModule;
3031
import com.basistech.rosette.apimodel.jackson.DocumentRequestMixin;
@@ -227,13 +228,30 @@ public PingResponse ping() throws IOException, HttpRosetteAPIException {
227228
*/
228229
@Override
229230
public SupportedLanguagesResponse getSupportedLanguages(String endpoint) throws HttpRosetteAPIException {
230-
if (DOC_ENDPOINTS.contains(endpoint)) {
231+
if (DOC_ENDPOINTS.contains(endpoint) || NAME_DEDUPLICATION_SERVICE_PATH.equals(endpoint)) {
231232
return sendGetRequest(urlBase + endpoint + SUPPORTED_LANGUAGES_SUBPATH, SupportedLanguagesResponse.class);
232233
} else {
233234
return null;
234235
}
235236
}
236237

238+
/**
239+
* Gets the set of language, script codes and transliteration scheme pairs supported by the specified Rosette API
240+
* endpoint.
241+
*
242+
* @param endpoint Rosette API endpoint.
243+
* @return SupportedLanguagePairsResponse
244+
* @throws HttpRosetteAPIException for an error returned from the Rosette API.
245+
*/
246+
@Override
247+
public SupportedLanguagePairsResponse getSupportedLanguagePairs(String endpoint) throws HttpRosetteAPIException {
248+
if (NAMES_ENDPOINTS.contains(endpoint) && !NAME_DEDUPLICATION_SERVICE_PATH.equals(endpoint)) {
249+
return sendGetRequest(urlBase + endpoint + SUPPORTED_LANGUAGES_SUBPATH, SupportedLanguagePairsResponse.class);
250+
} else {
251+
return null;
252+
}
253+
}
254+
237255
/**
238256
*
239257
* @param endpoint which endpoint.

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
import com.basistech.rosette.apimodel.AdmRequest;
2020
import com.basistech.rosette.apimodel.Response;
2121
import com.basistech.rosette.apimodel.SupportedLanguage;
22+
import com.basistech.rosette.apimodel.SupportedLanguagePair;
23+
import com.basistech.rosette.apimodel.SupportedLanguagePairsResponse;
2224
import com.basistech.rosette.apimodel.SupportedLanguagesResponse;
2325
import com.basistech.rosette.apimodel.jackson.ApiModelMixinModule;
2426
import com.basistech.rosette.dm.AnnotatedText;
2527

2628
import com.basistech.util.ISO15924;
2729
import com.basistech.util.LanguageCode;
30+
import com.basistech.util.TextDomain;
31+
import com.basistech.util.TransliterationScheme;
32+
2833
import org.apache.commons.io.IOUtils;
2934
import org.apache.http.HttpHeaders;
3035
import org.junit.Before;
@@ -48,6 +53,8 @@
4853
import com.fasterxml.jackson.databind.ObjectMapper;
4954

5055
import static com.basistech.rosette.api.common.AbstractRosetteAPI.ENTITIES_SERVICE_PATH;
56+
import static com.basistech.rosette.api.common.AbstractRosetteAPI.NAME_SIMILARITY_SERVICE_PATH;
57+
import static com.basistech.rosette.api.common.AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH;
5158
import static java.util.concurrent.TimeUnit.SECONDS;
5259

5360
public class BasicTest extends AbstractTest {
@@ -244,4 +251,60 @@ public void testLanguageSupport() throws Exception {
244251
.build()));
245252
}
246253
}
254+
255+
@Test
256+
public void testNameSimilarityLanguageSupport() throws Exception {
257+
try (InputStream respIns = getClass().getResourceAsStream("/name-similarity-supported-languages.json")) {
258+
mockServer.when(HttpRequest.request()
259+
.withMethod("GET")
260+
.withPath("/rest/v1/name-similarity/supported-languages"))
261+
.respond(HttpResponse.response()
262+
.withStatusCode(200)
263+
.withHeader("Content-Type", "application/json")
264+
.withBody(IOUtils.toString(respIns, "UTF-8")));
265+
api = new HttpRosetteAPI.Builder()
266+
.key("foo-key")
267+
.url(String.format("http://localhost:%d/rest/v1", serverPort))
268+
.build();
269+
270+
SupportedLanguagePairsResponse resp = api.getSupportedLanguagePairs(NAME_SIMILARITY_SERVICE_PATH);
271+
assertEquals(2, resp.getSupportedLanguagePairs().size());
272+
assertTrue(resp.getSupportedLanguagePairs().contains(SupportedLanguagePair.builder()
273+
.source(new TextDomain(ISO15924.Latn, LanguageCode.ENGLISH, null))
274+
.target(new TextDomain(ISO15924.Latn, LanguageCode.ENGLISH, null))
275+
.build()));
276+
assertTrue(resp.getSupportedLanguagePairs().contains(SupportedLanguagePair.builder()
277+
.source(new TextDomain(ISO15924.Arab, LanguageCode.ARABIC, null))
278+
.target(new TextDomain(ISO15924.Arab, LanguageCode.ARABIC, null))
279+
.build()));
280+
}
281+
}
282+
283+
@Test
284+
public void testNameTranslationLanguageSupport() throws Exception {
285+
try (InputStream respIns = getClass().getResourceAsStream("/name-translation-supported-languages.json")) {
286+
mockServer.when(HttpRequest.request()
287+
.withMethod("GET")
288+
.withPath("/rest/v1/name-translation/supported-languages"))
289+
.respond(HttpResponse.response()
290+
.withStatusCode(200)
291+
.withHeader("Content-Type", "application/json")
292+
.withBody(IOUtils.toString(respIns, "UTF-8")));
293+
api = new HttpRosetteAPI.Builder()
294+
.key("foo-key")
295+
.url(String.format("http://localhost:%d/rest/v1", serverPort))
296+
.build();
297+
298+
SupportedLanguagePairsResponse resp = api.getSupportedLanguagePairs(NAME_TRANSLATION_SERVICE_PATH);
299+
assertEquals(2, resp.getSupportedLanguagePairs().size());
300+
assertTrue(resp.getSupportedLanguagePairs().contains(SupportedLanguagePair.builder()
301+
.source(new TextDomain(ISO15924.Latn, LanguageCode.ENGLISH, TransliterationScheme.NATIVE))
302+
.target(new TextDomain(ISO15924.Latn, LanguageCode.ENGLISH, TransliterationScheme.IC))
303+
.build()));
304+
assertTrue(resp.getSupportedLanguagePairs().contains(SupportedLanguagePair.builder()
305+
.source(new TextDomain(ISO15924.Arab, LanguageCode.ARABIC, TransliterationScheme.NATIVE))
306+
.target(new TextDomain(ISO15924.Arab, LanguageCode.ARABIC, TransliterationScheme.NATIVE))
307+
.build()));
308+
}
309+
}
247310
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"supportedLanguagePairs": [
3+
{"source": {"language": "eng", "script": "Latn"}, "target": {"language": "eng", "script": "Latn"}},
4+
{"source": {"language": "ara", "script": "Arab"}, "target": {"language": "ara", "script": "Arab"}}
5+
]
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"supportedLanguagePairs": [
3+
{"source": {"language": "eng", "script": "Latn", "transliterationScheme": "NATIVE"}, "target": {"language": "eng", "script": "Latn", "transliterationScheme": "IC"}},
4+
{"source": {"language": "ara", "script": "Arab", "transliterationScheme": "NATIVE"}, "target": {"language": "ara", "script": "Arab", "transliterationScheme": "NATIVE"}}
5+
]
6+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.basistech.rosette.apimodel.CommonRosetteAPIException;
2525
import com.basistech.rosette.apimodel.Request;
2626
import com.basistech.rosette.apimodel.Response;
27+
import com.basistech.rosette.apimodel.SupportedLanguagePairsResponse;
2728
import com.basistech.rosette.apimodel.SupportedLanguagesResponse;
2829
import com.basistech.rosette.dm.AnnotatedText;
2930

@@ -85,6 +86,16 @@ public abstract class AbstractRosetteAPI implements AutoCloseable {
8586
*/
8687
public abstract SupportedLanguagesResponse getSupportedLanguages(String endpoint) throws CommonRosetteAPIException;
8788

89+
/**
90+
* Gets the set of language, script codes and transliteration scheme pairs supported by the specified Rosette API
91+
* endpoint.
92+
*
93+
* @param endpoint Rosette API endpoint.
94+
* @return SupportedLanguagePairsResponse
95+
* @throws CommonRosetteAPIException for an error returned from the Rosette API.
96+
*/
97+
public abstract SupportedLanguagePairsResponse getSupportedLanguagePairs(String endpoint) throws CommonRosetteAPIException;
98+
8899
/**
89100
* Perform a request to an endpoint of the Rosette API.
90101
* @param endpoint which endpoint.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018 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;
18+
19+
import com.basistech.rosette.annotations.JacksonMixin;
20+
import com.basistech.util.TextDomain;
21+
22+
import lombok.Builder;
23+
import lombok.Value;
24+
25+
/**
26+
* Supported language/script/scheme pairs
27+
*/
28+
@Value
29+
@Builder
30+
@JacksonMixin
31+
public class SupportedLanguagePair {
32+
/**
33+
* @return the source TextDomain
34+
*/
35+
private final TextDomain source;
36+
37+
/**
38+
* @return the target TextDomain
39+
*/
40+
private final TextDomain target;
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2018 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;
18+
19+
import com.basistech.rosette.annotations.JacksonMixin;
20+
21+
import lombok.Builder;
22+
import lombok.EqualsAndHashCode;
23+
import lombok.Getter;
24+
25+
import java.util.Set;
26+
27+
/**
28+
* Supported language/script/scheme pairs for name-translation and name-similarity endpoints
29+
*/
30+
@Getter
31+
@EqualsAndHashCode
32+
@Builder
33+
@JacksonMixin
34+
public class SupportedLanguagePairsResponse extends Response {
35+
/**
36+
* @return the set of supported language pairs
37+
*/
38+
private final Set<SupportedLanguagePair> supportedLanguagePairs;
39+
}

model/src/main/java/com/basistech/rosette/apimodel/SupportedLanguagesResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.Set;
2525

2626
/**
27-
* Supported langauges/scripts for a given endpoint
27+
* Supported languages/scripts for a given endpoint
2828
*/
2929
@Getter
3030
@EqualsAndHashCode

0 commit comments

Comments
 (0)