Skip to content

Commit f154d66

Browse files
authored
Merge pull request #214 from rosette-api/RLPNC-7350-return-multi-translations
RLPNC-7350: Rosapi returns a list of RNT results
2 parents 6574f22 + 42f7f31 commit f154d66

File tree

9 files changed

+208
-2
lines changed

9 files changed

+208
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,34 @@ void testTranslateName(String testFilename, String responseStr, int statusCode)
258258
}
259259
}
260260

261+
private static Stream<Arguments> testMultiTranslateNameParameters() throws IOException {
262+
return getTestFiles("-multi-name-translation.json");
263+
}
264+
265+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
266+
@MethodSource("testMultiTranslateNameParameters")
267+
void testMultiTranslateName(String testFilename, String responseStr, int statusCode) throws IOException {
268+
setStatusCodeResponse(responseStr, statusCode);
269+
NameTranslationRequest request = readValueNameTranslation(testFilename);
270+
try {
271+
NameTranslationResponse response = api.perform(AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH, request,
272+
NameTranslationResponse.class);
273+
verifyMultiNameTranslations(response, responseStr);
274+
} catch (HttpRosetteAPIException e) {
275+
verifyException(e, responseStr);
276+
}
277+
}
278+
261279
private void verifyNameTranslation(NameTranslationResponse response, String responseStr) throws IOException {
262280
NameTranslationResponse goldResponse = mapper.readValue(responseStr, NameTranslationResponse.class);
263281
assertEquals(goldResponse.getTranslation(), response.getTranslation());
264282
}
265283

284+
private void verifyMultiNameTranslations(NameTranslationResponse response, String responseStr) throws IOException {
285+
NameTranslationResponse goldResponse = mapper.readValue(responseStr, NameTranslationResponse.class);
286+
assertEquals(goldResponse.getTranslations(), response.getTranslations());
287+
}
288+
266289
private NameTranslationRequest readValueNameTranslation(String testFilename) throws IOException {
267290
File input = new File("src/test/mock-data/request", testFilename);
268291
return mapper.readValue(input, NameTranslationRequest.class);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"entityType": "PERSON",
3+
"name": "معمر محمد أبو منيار القذاف",
4+
"sourceLanguageOfOrigin": "ara",
5+
"sourceLanguageOfUse": "ara",
6+
"sourceScript": "Arab",
7+
"targetLanguage": "eng",
8+
"targetScheme": "IC",
9+
"targetScript": "Latn",
10+
"maximumResults": 10
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"sourceScript": "Arab",
3+
"sourceLanguageOfUse": "ara",
4+
"targetLanguage": "eng",
5+
"targetScript": "Latn",
6+
"targetScheme": "IC",
7+
"translations": [
8+
{
9+
"translatio": "Mu'ammar Muhammad Abu-Minyar al-Qadhaf",
10+
"confidence": 0.10655738
11+
},
12+
{
13+
"translation": "Mu'ammar Muhammad Abu-Minyar al-Qadhaf",
14+
"confidence": 0.10382514
15+
},
16+
{
17+
"translation": "Mu'ammar Muhammad Abaw Minyar al-Qadhaf",
18+
"confidence": 0.1010929
19+
},
20+
{
21+
"translation": "Mu'ammar Muhammad Abu-Minyar al-Qadhaf",
22+
"confidence": 0.10094909
23+
},
24+
{
25+
"translation": "Mu'ammar Mhammad Abu-Minyar al-Qadhaf",
26+
"confidence": 0.10094909
27+
},
28+
{
29+
"translation": "Mu'ammar Muhammad Abu-Minyar al-Qadhaf",
30+
"confidence": 0.09836066
31+
},
32+
{
33+
"translation": "Mu'ammar Muhammad Abw Minyar al-Qadhaf",
34+
"confidence": 0.09836066
35+
},
36+
{
37+
"translation": "Mu'ammar Mhammad Abu-Minyar al-Qadhaf",
38+
"confidence": 0.09836066
39+
},
40+
{
41+
"translation": "Mu'ammar Muhammad Abaw Minyar al-Qadhaf",
42+
"confidence": 0.09577222
43+
},
44+
{
45+
"translation": "Mu'ammar Mhammad Abaw Minyar al-Qadhaf",
46+
"confidence": 0.09577222
47+
}
48+
]
49+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
200
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 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+
package com.basistech.rosette.examples;
17+
18+
import static com.basistech.rosette.api.common.AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH;
19+
20+
import java.io.IOException;
21+
22+
import com.basistech.rosette.api.HttpRosetteAPI;
23+
import com.basistech.rosette.apimodel.NameTranslationRequest;
24+
import com.basistech.rosette.apimodel.NameTranslationResponse;
25+
import com.basistech.util.LanguageCode;
26+
27+
/**
28+
* Example which demonstrates the name translation api with maximumResults option set.
29+
*/
30+
@SuppressWarnings({"java:S1166", "java:S2221", "java:S106"})
31+
public final class NameMultipleTranslationsExample extends ExampleBase {
32+
public static void main(String[] args) {
33+
try {
34+
new NameMultipleTranslationsExample().run();
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
System.exit(1);
38+
}
39+
}
40+
41+
private void run() throws IOException {
42+
String translatedNameData = "معمر محمد أبو منيار القذاف";
43+
NameTranslationRequest request = NameTranslationRequest.builder()
44+
.name(translatedNameData)
45+
.targetLanguage(LanguageCode.ENGLISH)
46+
.maximumResults(10)
47+
.build();
48+
49+
HttpRosetteAPI rosetteApi = new HttpRosetteAPI.Builder()
50+
.key(getApiKeyFromSystemProperty())
51+
.url(getAltUrlFromSystemProperty())
52+
.build();
53+
//The api object creates an http client, but to provide your own:
54+
//api.httpClient(CloseableHttpClient)
55+
NameTranslationResponse response = rosetteApi.perform(NAME_TRANSLATION_SERVICE_PATH, request, NameTranslationResponse.class);
56+
System.out.println(responseToJson(response));
57+
}
58+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ protected NameTranslationRequestMixin(
4141
@JsonProperty("sourceLanguageOfUse") LanguageCode sourceLanguageOfUse,
4242
@JsonProperty("targetLanguage") LanguageCode targetLanguage,
4343
@JsonProperty("targetScript") ISO15924 targetScript,
44-
@JsonProperty("targetScheme") TransliterationScheme targetScheme
44+
@JsonProperty("targetScheme") TransliterationScheme targetScheme,
45+
@JsonProperty("maximumResults") Integer maximumResults
4546
) {
4647
//
4748
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 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 lombok.Builder;
20+
import lombok.EqualsAndHashCode;
21+
import lombok.Getter;
22+
23+
import com.basistech.rosette.annotations.JacksonMixin;
24+
25+
@Getter
26+
@EqualsAndHashCode
27+
@Builder
28+
@JacksonMixin
29+
public class NameTranslation {
30+
String translation;
31+
Double confidence;
32+
33+
public NameTranslation(String translation, Double confidence) {
34+
this.translation = translation;
35+
this.confidence = confidence;
36+
}
37+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public final class NameTranslationRequest extends Request {
7272
*/
7373
private final TransliterationScheme targetScheme;
7474

75-
@Builder // workaround for inheritance https://github.com/rzwitserloot/lombok/issues/853
75+
/**
76+
* @return the maximum number of translation results to return
77+
*/
78+
private final Integer maximumResults;
79+
7680
public NameTranslationRequest(String profileId,
7781
String name,
7882
String entityType,
@@ -82,6 +86,20 @@ public NameTranslationRequest(String profileId,
8286
LanguageCode targetLanguage,
8387
ISO15924 targetScript,
8488
TransliterationScheme targetScheme) {
89+
this(profileId, name, entityType, sourceScript, sourceLanguageOfOrigin, sourceLanguageOfUse, targetLanguage, targetScript, targetScheme, null);
90+
}
91+
92+
@Builder // workaround for inheritance https://github.com/rzwitserloot/lombok/issues/853
93+
public NameTranslationRequest(String profileId,
94+
String name,
95+
String entityType,
96+
ISO15924 sourceScript,
97+
LanguageCode sourceLanguageOfOrigin,
98+
LanguageCode sourceLanguageOfUse,
99+
LanguageCode targetLanguage,
100+
ISO15924 targetScript,
101+
TransliterationScheme targetScheme,
102+
Integer maximumResults) {
85103
super(profileId);
86104
this.name = name;
87105
this.entityType = entityType;
@@ -91,5 +109,6 @@ public NameTranslationRequest(String profileId,
91109
this.targetLanguage = targetLanguage;
92110
this.targetScript = targetScript;
93111
this.targetScheme = targetScheme;
112+
this.maximumResults = maximumResults;
94113
}
95114
}

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

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

1717
package com.basistech.rosette.apimodel;
1818

19+
import java.util.List;
20+
1921
import com.basistech.rosette.annotations.JacksonMixin;
2022
import com.basistech.util.ISO15924;
2123
import com.basistech.util.LanguageCode;
@@ -72,4 +74,9 @@ public class NameTranslationResponse extends Response {
7274
* @return the translation confidence (0.0-1.0)
7375
*/
7476
private final Double confidence;
77+
78+
/**
79+
* @return the translation
80+
*/
81+
private final List<NameTranslation> translations;
7582
}

0 commit comments

Comments
 (0)