Skip to content

Commit 7603aea

Browse files
added custom model support for language translation
1 parent 5b61fb1 commit 7603aea

File tree

7 files changed

+172
-74
lines changed

7 files changed

+172
-74
lines changed

src/main/java/com/ibm/watson/developer_cloud/language_translation/v2/LanguageTranslation.java

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ibm.watson.developer_cloud.language_translation.v2;
1717

18+
import java.io.File;
1819
import java.io.IOException;
1920
import java.lang.reflect.Type;
2021
import java.util.HashMap;
@@ -50,12 +51,16 @@
5051
*/
5152
public class LanguageTranslation extends WatsonService {
5253

54+
public static final String BASE_MODEL_ID = "base_model_id";
55+
public static final String FORCED_GLOSSARY = "forced_glossary";
5356
public static final String DEFAULT = "default";
54-
private static final String LANGUAGES = "languages";
5557
public static final String MODEL_ID = "model_id";
5658
public static final String SOURCE = "source";
5759
public static final String TARGET = "target";
5860
public static final String TEXT = "text";
61+
public static final String NAME = "name";
62+
63+
private static final String LANGUAGES = "languages";
5964

6065
/** The url. */
6166
private static final String URL = "https://gateway.watsonplatform.net/language-translation/api";
@@ -79,11 +84,11 @@ public LanguageTranslation() {
7984
setEndPoint(URL);
8085
}
8186

82-
87+
8388

8489
/**
8590
* Retrieves the list of identifiable languages.
86-
*
91+
*
8792
* @return the identifiable languages
8893
* @see TranslationModel
8994
*/
@@ -101,20 +106,92 @@ public List<IdentifiableLanguage> getIdentifiableLanguages() {
101106
throw new RuntimeException(e);
102107
}
103108
}
104-
109+
105110
/**
106-
* Retrieves the list of models.
107-
*
111+
* Retrieves the list of translation models.
112+
*
108113
* @return the translation models
109114
* @see TranslationModel
110115
*/
111116
public List<TranslationModel> getModels() {
112117
return getModels(null, null, null);
113118
}
114119

120+
/**
121+
* Retrieves a translation models.
122+
*
123+
* @param modelId the model identifier
124+
* @return the translation models
125+
* @see TranslationModel
126+
*/
127+
public TranslationModel getModel(String modelId) {
128+
if (modelId == null || modelId.isEmpty())
129+
throw new IllegalArgumentException("model_id can not be null or empty");
130+
131+
132+
HttpRequestBase request = Request.Get("/v2/models/" + modelId).build();
133+
try {
134+
HttpResponse response = execute(request);
135+
String modelAsString = ResponseUtil.getString(response);
136+
TranslationModel model = getGson().fromJson(modelAsString,TranslationModel.class);
137+
return model;
138+
} catch (IOException e) {
139+
throw new RuntimeException(e);
140+
}
141+
}
142+
143+
/**
144+
* Deletes a translation models.
145+
*
146+
* @param modelId the model identifier
147+
*/
148+
public void deleteModel(String modelId) {
149+
if (modelId == null || modelId.isEmpty())
150+
throw new IllegalArgumentException("model_id can not be null or empty");
151+
152+
HttpRequestBase request = Request.Delete("/v2/models/" + modelId).build();
153+
execute(request);
154+
}
155+
156+
/**
157+
* Creates a translation models.
158+
*
159+
* @param params String name the model name,
160+
* String base_model_id the model id to use as base model,
161+
* File forced_glossary the tmx file use in the model
162+
*/
163+
public TranslationModel createModel(Map<String,Object> params) {
164+
// forced_glossary
165+
File forcedGlossary = (File) params.get(FORCED_GLOSSARY);
166+
if (forcedGlossary == null || !forcedGlossary.exists() || !forcedGlossary.isFile())
167+
throw new IllegalArgumentException("forced_glossary is not a valid file");
168+
169+
// base_model_id
170+
String baseModelId = (String) params.get(BASE_MODEL_ID);
171+
if (baseModelId == null || baseModelId.isEmpty())
172+
throw new IllegalArgumentException("base_model_id can not be null or empty");
173+
174+
Request request = Request.Post("/v2/models");
175+
176+
request.withForm(FORCED_GLOSSARY, forcedGlossary);
177+
request.withForm(BASE_MODEL_ID, baseModelId);
178+
179+
if (params.containsKey(NAME))
180+
request.withForm(NAME, (String)params.get(NAME));
181+
182+
HttpRequestBase requestBase = request.build();
183+
try {
184+
HttpResponse response = execute(requestBase);
185+
String modelAsString = ResponseUtil.getString(response);
186+
TranslationModel model = getGson().fromJson(modelAsString, TranslationModel.class);
187+
return model;
188+
} catch (IOException e) {
189+
throw new RuntimeException(e);
190+
} }
191+
115192
/**
116193
* Retrieves the list of models.
117-
*
194+
*
118195
* @param showDefault
119196
* show default models
120197
* @param source
@@ -151,7 +228,7 @@ public List<TranslationModel> getModels(final Boolean showDefault,
151228

152229
/**
153230
* Identify language in which text is written.
154-
*
231+
*
155232
* @param text
156233
* the text to identify
157234
* @return the identified language
@@ -174,7 +251,7 @@ public List<IdentifiedLanguage> identify(final String text) {
174251

175252
/*
176253
* (non-Javadoc)
177-
*
254+
*
178255
* @see java.lang.Object#toString()
179256
*/
180257
@Override
@@ -195,10 +272,10 @@ public String toString() {
195272
* @param source the source language
196273
* @param target the target language
197274
* @param model_id the model id
198-
* @return The {@link TranslationResult}
275+
* @return The {@link TranslationResult}
199276
*/
200277
public TranslationResult translate(final Map<String, Object> params) {
201-
278+
202279
final String source = (String) params.get(SOURCE);
203280
final String target = (String) params.get(TARGET);
204281
final String modelId = (String) params.get(MODEL_ID);
@@ -211,7 +288,7 @@ public TranslationResult translate(final Map<String, Object> params) {
211288
} else {
212289
text = null;
213290
}
214-
291+
215292
if ((modelId == null || modelId.isEmpty())
216293
&& (source == null || source.isEmpty() || target == null || target
217294
.isEmpty()))
@@ -229,10 +306,10 @@ public TranslationResult translate(final Map<String, Object> params) {
229306
paragraphs.add(new JsonPrimitive(paragraph));
230307
}
231308
contentJson.add(TEXT, paragraphs);
232-
309+
233310
Request requestBuilder = Request.Post("/v2/translate")
234311
.withContent(contentJson);
235-
312+
236313
if (source != null && !source.isEmpty())
237314
requestBuilder.withQuery(SOURCE, source);
238315

@@ -262,22 +339,22 @@ public TranslationResult translate(final Map<String, Object> params) {
262339
*
263340
* @param text The submitted paragraphs to translate
264341
* @param model_id the model id
265-
* @return The {@link TranslationResult}
342+
* @return The {@link TranslationResult}
266343
*/
267344
public TranslationResult translate(final String text,final String modelId) {
268345
Map<String, Object> params = new HashMap<String,Object>();
269346
params.put(TEXT, text);
270347
params.put(MODEL_ID, modelId);
271348
return translate(params);
272349
}
273-
350+
274351
/**
275352
* Translate text using source and target languages
276353
*
277354
* @param text The submitted paragraphs to translate
278355
* @param source The source language
279356
* @param target The target language
280-
* @return The {@link TranslationResult}
357+
* @return The {@link TranslationResult}
281358
*/
282359
public TranslationResult translate(final String text,final String source,final String target) {
283360
Map<String, Object> params = new HashMap<String,Object>();
@@ -286,5 +363,5 @@ public TranslationResult translate(final String text,final String source,final S
286363
params.put(TARGET, target);
287364
return translate(params);
288365
}
289-
366+
290367
}

src/main/java/com/ibm/watson/developer_cloud/language_translation/v2/model/IdentifiableLanguage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* Identifiable language used by the {@link LanguageTranslation} service.
23-
*
23+
*
2424
* @author German Attanasio Ruiz ([email protected])
2525
*/
2626
public class IdentifiableLanguage {
@@ -33,7 +33,7 @@ public class IdentifiableLanguage {
3333

3434
/**
3535
* Gets the name.
36-
*
36+
*
3737
* @return the name
3838
*/
3939
public String getName() {
@@ -42,7 +42,7 @@ public String getName() {
4242

4343
/**
4444
* Instantiates a new identifiable language.
45-
*
45+
*
4646
* @param language
4747
* the language
4848
* @param name
@@ -56,7 +56,7 @@ public IdentifiableLanguage(final String language, final String name) {
5656

5757
/**
5858
* Instantiates a new language.
59-
*
59+
*
6060
* @param language
6161
* the language
6262
*/
@@ -67,7 +67,7 @@ public IdentifiableLanguage(final String language) {
6767

6868
/**
6969
* Gets the language.
70-
*
70+
*
7171
* @return the language
7272
*/
7373
public String getLanguage() {
@@ -76,7 +76,7 @@ public String getLanguage() {
7676

7777
/*
7878
* (non-Javadoc)
79-
*
79+
*
8080
* @see java.lang.Object#toString()
8181
*/
8282
@Override

src/main/java/com/ibm/watson/developer_cloud/language_translation/v2/model/IdentifiedLanguage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* Language detected by the {@link LanguageTranslation} service.
23-
*
23+
*
2424
* @author German Attanasio Ruiz ([email protected])
2525
*/
2626
public class IdentifiedLanguage extends IdentifiableLanguage {
@@ -30,7 +30,7 @@ public class IdentifiedLanguage extends IdentifiableLanguage {
3030

3131
/**
3232
* Instantiates a new language.
33-
*
33+
*
3434
* @param language
3535
* the language
3636
* @param confidence
@@ -43,7 +43,7 @@ public IdentifiedLanguage(final String language, final double confidence) {
4343

4444
/**
4545
* Gets the confidence.
46-
*
46+
*
4747
* @return the confidence
4848
*/
4949
public Double getConfidence() {
@@ -52,7 +52,7 @@ public Double getConfidence() {
5252

5353
/*
5454
* (non-Javadoc)
55-
*
55+
*
5656
* @see java.lang.Object#toString()
5757
*/
5858
@Override

src/main/java/com/ibm/watson/developer_cloud/language_translation/v2/model/Translation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Translation {
1717

1818
/**
1919
* Gets the translation.
20-
*
20+
*
2121
* @return The translation
2222
*/
2323
public String getTranslation() {
@@ -26,7 +26,7 @@ public String getTranslation() {
2626

2727
/**
2828
* Sets the translation.
29-
*
29+
*
3030
* @param translation
3131
* The translation
3232
*/
@@ -36,7 +36,7 @@ public void setTranslation(final String translation) {
3636

3737
/**
3838
* With translation.
39-
*
39+
*
4040
* @param translation
4141
* the translation
4242
* @return the translation
@@ -48,7 +48,7 @@ public Translation withTranslation(final String translation) {
4848

4949
/*
5050
* (non-Javadoc)
51-
*
51+
*
5252
* @see java.lang.Object#toString()
5353
*/
5454
@Override

0 commit comments

Comments
 (0)