15
15
*/
16
16
package com .ibm .watson .developer_cloud .language_translation .v2 ;
17
17
18
+ import java .io .File ;
18
19
import java .io .IOException ;
19
20
import java .lang .reflect .Type ;
20
21
import java .util .HashMap ;
50
51
*/
51
52
public class LanguageTranslation extends WatsonService {
52
53
54
+ public static final String BASE_MODEL_ID = "base_model_id" ;
55
+ public static final String FORCED_GLOSSARY = "forced_glossary" ;
53
56
public static final String DEFAULT = "default" ;
54
- private static final String LANGUAGES = "languages" ;
55
57
public static final String MODEL_ID = "model_id" ;
56
58
public static final String SOURCE = "source" ;
57
59
public static final String TARGET = "target" ;
58
60
public static final String TEXT = "text" ;
61
+ public static final String NAME = "name" ;
62
+
63
+ private static final String LANGUAGES = "languages" ;
59
64
60
65
/** The url. */
61
66
private static final String URL = "https://gateway.watsonplatform.net/language-translation/api" ;
@@ -79,11 +84,11 @@ public LanguageTranslation() {
79
84
setEndPoint (URL );
80
85
}
81
86
82
-
87
+
83
88
84
89
/**
85
90
* Retrieves the list of identifiable languages.
86
- *
91
+ *
87
92
* @return the identifiable languages
88
93
* @see TranslationModel
89
94
*/
@@ -101,20 +106,92 @@ public List<IdentifiableLanguage> getIdentifiableLanguages() {
101
106
throw new RuntimeException (e );
102
107
}
103
108
}
104
-
109
+
105
110
/**
106
- * Retrieves the list of models.
107
- *
111
+ * Retrieves the list of translation models.
112
+ *
108
113
* @return the translation models
109
114
* @see TranslationModel
110
115
*/
111
116
public List <TranslationModel > getModels () {
112
117
return getModels (null , null , null );
113
118
}
114
119
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
+
115
192
/**
116
193
* Retrieves the list of models.
117
- *
194
+ *
118
195
* @param showDefault
119
196
* show default models
120
197
* @param source
@@ -151,7 +228,7 @@ public List<TranslationModel> getModels(final Boolean showDefault,
151
228
152
229
/**
153
230
* Identify language in which text is written.
154
- *
231
+ *
155
232
* @param text
156
233
* the text to identify
157
234
* @return the identified language
@@ -174,7 +251,7 @@ public List<IdentifiedLanguage> identify(final String text) {
174
251
175
252
/*
176
253
* (non-Javadoc)
177
- *
254
+ *
178
255
* @see java.lang.Object#toString()
179
256
*/
180
257
@ Override
@@ -195,10 +272,10 @@ public String toString() {
195
272
* @param source the source language
196
273
* @param target the target language
197
274
* @param model_id the model id
198
- * @return The {@link TranslationResult}
275
+ * @return The {@link TranslationResult}
199
276
*/
200
277
public TranslationResult translate (final Map <String , Object > params ) {
201
-
278
+
202
279
final String source = (String ) params .get (SOURCE );
203
280
final String target = (String ) params .get (TARGET );
204
281
final String modelId = (String ) params .get (MODEL_ID );
@@ -211,7 +288,7 @@ public TranslationResult translate(final Map<String, Object> params) {
211
288
} else {
212
289
text = null ;
213
290
}
214
-
291
+
215
292
if ((modelId == null || modelId .isEmpty ())
216
293
&& (source == null || source .isEmpty () || target == null || target
217
294
.isEmpty ()))
@@ -229,10 +306,10 @@ public TranslationResult translate(final Map<String, Object> params) {
229
306
paragraphs .add (new JsonPrimitive (paragraph ));
230
307
}
231
308
contentJson .add (TEXT , paragraphs );
232
-
309
+
233
310
Request requestBuilder = Request .Post ("/v2/translate" )
234
311
.withContent (contentJson );
235
-
312
+
236
313
if (source != null && !source .isEmpty ())
237
314
requestBuilder .withQuery (SOURCE , source );
238
315
@@ -262,22 +339,22 @@ public TranslationResult translate(final Map<String, Object> params) {
262
339
*
263
340
* @param text The submitted paragraphs to translate
264
341
* @param model_id the model id
265
- * @return The {@link TranslationResult}
342
+ * @return The {@link TranslationResult}
266
343
*/
267
344
public TranslationResult translate (final String text ,final String modelId ) {
268
345
Map <String , Object > params = new HashMap <String ,Object >();
269
346
params .put (TEXT , text );
270
347
params .put (MODEL_ID , modelId );
271
348
return translate (params );
272
349
}
273
-
350
+
274
351
/**
275
352
* Translate text using source and target languages
276
353
*
277
354
* @param text The submitted paragraphs to translate
278
355
* @param source The source language
279
356
* @param target The target language
280
- * @return The {@link TranslationResult}
357
+ * @return The {@link TranslationResult}
281
358
*/
282
359
public TranslationResult translate (final String text ,final String source ,final String target ) {
283
360
Map <String , Object > params = new HashMap <String ,Object >();
@@ -286,5 +363,5 @@ public TranslationResult translate(final String text,final String source,final S
286
363
params .put (TARGET , target );
287
364
return translate (params );
288
365
}
289
-
366
+
290
367
}
0 commit comments