1515 */
1616package com .ibm .watson .developer_cloud .language_translation .v2 ;
1717
18+ import java .io .File ;
1819import java .io .IOException ;
1920import java .lang .reflect .Type ;
2021import java .util .HashMap ;
5051 */
5152public 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}
0 commit comments