2020import com .ibm .cloud .sdk .core .util .GsonSingleton ;
2121import com .ibm .cloud .sdk .core .util .RequestUtils ;
2222import com .ibm .cloud .sdk .core .util .ResponseConverterUtils ;
23+ import com .ibm .cloud .sdk .core .util .Validator ;
24+ import com .ibm .watson .common .SdkCommon ;
2325import com .ibm .watson .natural_language_classifier .v1 .model .Classification ;
2426import com .ibm .watson .natural_language_classifier .v1 .model .ClassificationCollection ;
2527import com .ibm .watson .natural_language_classifier .v1 .model .Classifier ;
3032import com .ibm .watson .natural_language_classifier .v1 .model .DeleteClassifierOptions ;
3133import com .ibm .watson .natural_language_classifier .v1 .model .GetClassifierOptions ;
3234import com .ibm .watson .natural_language_classifier .v1 .model .ListClassifiersOptions ;
33- import com .ibm .cloud .sdk .core .util .Validator ;
35+ import java .util .Map ;
36+ import java .util .Map .Entry ;
3437import okhttp3 .MultipartBody ;
3538import okhttp3 .RequestBody ;
3639
37- import java .io .ByteArrayInputStream ;
38- import java .io .File ;
39- import java .io .FileNotFoundException ;
40- import java .util .HashMap ;
41- import java .util .Map ;
42-
4340/**
4441 * IBM Watson™ Natural Language Classifier uses machine learning algorithms to return the top matching predefined
4542 * classes for short text input. You create and train a classifier to connect predefined classes to example texts so
@@ -105,8 +102,11 @@ public ServiceCall<Classification> classify(ClassifyOptions classifyOptions) {
105102 String [] pathParameters = { classifyOptions .classifierId () };
106103 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
107104 pathParameters ));
108- builder .header ("X-IBMCloud-SDK-Analytics" ,
109- "service_name=natural_language_classifier;service_version=v1;operation_id=classify" );
105+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "classify" );
106+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
107+ builder .header (header .getKey (), header .getValue ());
108+ }
109+ builder .header ("Accept" , "application/json" );
110110 final JsonObject contentJson = new JsonObject ();
111111 contentJson .addProperty ("text" , classifyOptions .text ());
112112 builder .bodyJson (contentJson );
@@ -130,8 +130,11 @@ public ServiceCall<ClassificationCollection> classifyCollection(ClassifyCollecti
130130 String [] pathParameters = { classifyCollectionOptions .classifierId () };
131131 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
132132 pathParameters ));
133- builder .header ("X-IBMCloud-SDK-Analytics" ,
134- "service_name=natural_language_classifier;service_version=v1;operation_id=classifyCollection" );
133+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "classifyCollection" );
134+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
135+ builder .header (header .getKey (), header .getValue ());
136+ }
137+ builder .header ("Accept" , "application/json" );
135138 final JsonObject contentJson = new JsonObject ();
136139 contentJson .add ("collection" , GsonSingleton .getGson ().toJsonTree (classifyCollectionOptions .collection ()));
137140 builder .bodyJson (contentJson );
@@ -150,16 +153,18 @@ public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createCl
150153 Validator .notNull (createClassifierOptions , "createClassifierOptions cannot be null" );
151154 String [] pathSegments = { "v1/classifiers" };
152155 RequestBuilder builder = RequestBuilder .post (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
153- builder .header ("X-IBMCloud-SDK-Analytics" ,
154- "service_name=natural_language_classifier;service_version=v1;operation_id=createClassifier" );
156+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "createClassifier" );
157+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
158+ builder .header (header .getKey (), header .getValue ());
159+ }
160+ builder .header ("Accept" , "application/json" );
155161 MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
156162 multipartBuilder .setType (MultipartBody .FORM );
157163 RequestBody trainingMetadataBody = RequestUtils .inputStreamBody (createClassifierOptions .metadata (),
158164 "application/json" );
159- multipartBuilder .addFormDataPart ("training_metadata" , createClassifierOptions .metadataFilename (),
160- trainingMetadataBody );
165+ multipartBuilder .addFormDataPart ("training_metadata" , "filename" , trainingMetadataBody );
161166 RequestBody trainingDataBody = RequestUtils .inputStreamBody (createClassifierOptions .trainingData (), "text/csv" );
162- multipartBuilder .addFormDataPart ("training_data" , createClassifierOptions . trainingDataFilename () , trainingDataBody );
167+ multipartBuilder .addFormDataPart ("training_data" , "filename" , trainingDataBody );
163168 builder .body (multipartBuilder .build ());
164169 return createServiceCall (builder .build (), ResponseConverterUtils .getObject (Classifier .class ));
165170 }
@@ -176,8 +181,11 @@ public ServiceCall<Void> deleteClassifier(DeleteClassifierOptions deleteClassifi
176181 String [] pathParameters = { deleteClassifierOptions .classifierId () };
177182 RequestBuilder builder = RequestBuilder .delete (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
178183 pathParameters ));
179- builder .header ("X-IBMCloud-SDK-Analytics" ,
180- "service_name=natural_language_classifier;service_version=v1;operation_id=deleteClassifier" );
184+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "deleteClassifier" );
185+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
186+ builder .header (header .getKey (), header .getValue ());
187+ }
188+ builder .header ("Accept" , "application/json" );
181189 return createServiceCall (builder .build (), ResponseConverterUtils .getVoid ());
182190 }
183191
@@ -195,8 +203,11 @@ public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierO
195203 String [] pathParameters = { getClassifierOptions .classifierId () };
196204 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ,
197205 pathParameters ));
198- builder .header ("X-IBMCloud-SDK-Analytics" ,
199- "service_name=natural_language_classifier;service_version=v1;operation_id=getClassifier" );
206+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "getClassifier" );
207+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
208+ builder .header (header .getKey (), header .getValue ());
209+ }
210+ builder .header ("Accept" , "application/json" );
200211 return createServiceCall (builder .build (), ResponseConverterUtils .getObject (Classifier .class ));
201212 }
202213
@@ -211,8 +222,11 @@ public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierO
211222 public ServiceCall <ClassifierList > listClassifiers (ListClassifiersOptions listClassifiersOptions ) {
212223 String [] pathSegments = { "v1/classifiers" };
213224 RequestBuilder builder = RequestBuilder .get (RequestBuilder .constructHttpUrl (getEndPoint (), pathSegments ));
214- builder .header ("X-IBMCloud-SDK-Analytics" ,
215- "service_name=natural_language_classifier;service_version=v1;operation_id=listClassifiers" );
225+ Map <String , String > sdkHeaders = SdkCommon .getSdkHeaders ("natural_language_classifier" , "v1" , "listClassifiers" );
226+ for (Entry <String , String > header : sdkHeaders .entrySet ()) {
227+ builder .header (header .getKey (), header .getValue ());
228+ }
229+ builder .header ("Accept" , "application/json" );
216230 if (listClassifiersOptions != null ) {
217231 }
218232 return createServiceCall (builder .build (), ResponseConverterUtils .getObject (ClassifierList .class ));
@@ -229,90 +243,4 @@ public ServiceCall<ClassifierList> listClassifiers() {
229243 return listClassifiers (null );
230244 }
231245
232- /**
233- * Classify.
234- *
235- * This method is here for backwards-compatibility with the other version of classify.
236- *
237- * @param classifierId the classifier ID
238- * @param text the submitted phrase to classify
239- * @return the classification of a phrase with a given classifier
240- */
241- public ServiceCall <Classification > classify (String classifierId , String text ) {
242- ClassifyOptions classifyOptions = new ClassifyOptions .Builder ()
243- .classifierId (classifierId )
244- .text (text )
245- .build ();
246- return classify (classifyOptions );
247- }
248-
249- /**
250- * Create classifier.
251- *
252- * This method is here for backwards-compatibility with the old version of createClassifier.
253- *
254- * @param name the classifier name
255- * @param language IETF primary language for the classifier. for example: 'en'
256- * @param trainingData the set of questions and their "keys" used to adapt a system to a domain (the ground truth)
257- * @return the classifier
258- * @throws FileNotFoundException if the file could not be found
259- */
260- public ServiceCall <Classifier > createClassifier (String name , String language , File trainingData )
261- throws FileNotFoundException {
262- Map <String , String > metadataMap = new HashMap <>();
263- metadataMap .put ("name" , name );
264- metadataMap .put ("language" , language );
265- String metadataString = GsonSingleton .getGson ().toJson (metadataMap );
266-
267- CreateClassifierOptions createClassifierOptions = new CreateClassifierOptions .Builder ()
268- .metadata (new ByteArrayInputStream (metadataString .getBytes ()))
269- .trainingData (trainingData )
270- .build ();
271-
272- return createClassifier (createClassifierOptions );
273- }
274-
275- /**
276- * Delete classifier.
277- *
278- * This method is here for backwards-compatibility with the old version of deleteClassifier.
279- *
280- * @param classifierId the classifier ID
281- * @return the service call
282- */
283- public ServiceCall <Void > deleteClassifier (String classifierId ) {
284- DeleteClassifierOptions deleteClassifierOptions = new DeleteClassifierOptions .Builder ()
285- .classifierId (classifierId )
286- .build ();
287-
288- return deleteClassifier (deleteClassifierOptions );
289- }
290-
291- /**
292- * Get information about a classifier.
293- *
294- * This method is here for backwards-compatibility with the old version of getClassifier.
295- *
296- * @param classifierId the classifier ID
297- * @return the classifier
298- */
299- public ServiceCall <Classifier > getClassifier (String classifierId ) {
300- GetClassifierOptions getClassifierOptions = new GetClassifierOptions .Builder ()
301- .classifierId (classifierId )
302- .build ();
303-
304- return getClassifier (getClassifierOptions );
305- }
306-
307- /**
308- * List classifiers.
309- *
310- * This method is here for backwards-compatibility with the old version of getClassifiers, which has been renamed
311- * to listClassifiers.
312- *
313- * @return the classifier list
314- */
315- public ServiceCall <ClassifierList > getClassifiers () {
316- return listClassifiers ();
317- }
318246}
0 commit comments