1- /**
2- * Copyright 2017 IBM Corp. All Rights Reserved.
1+ /*
2+ * Copyright 2018 IBM Corp. All Rights Reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55 * the License. You may obtain a copy of the License at
1212 */
1313package com .ibm .watson .developer_cloud .natural_language_classifier .v1 ;
1414
15- import java .io .File ;
16-
1715import com .google .gson .JsonObject ;
18- import com .ibm .watson .developer_cloud .http .HttpHeaders ;
19- import com .ibm .watson .developer_cloud .http .HttpMediaType ;
2016import com .ibm .watson .developer_cloud .http .RequestBuilder ;
2117import com .ibm .watson .developer_cloud .http .ServiceCall ;
2218import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .Classification ;
2319import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .Classifier ;
24- import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .Classifiers ;
20+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .ClassifierList ;
21+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .ClassifyOptions ;
22+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .CreateClassifierOptions ;
23+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .DeleteClassifierOptions ;
24+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .GetClassifierOptions ;
25+ import com .ibm .watson .developer_cloud .natural_language_classifier .v1 .model .ListClassifiersOptions ;
2526import com .ibm .watson .developer_cloud .service .WatsonService ;
27+ import com .ibm .watson .developer_cloud .util .RequestUtils ;
2628import com .ibm .watson .developer_cloud .util .ResponseConverterUtils ;
2729import com .ibm .watson .developer_cloud .util .Validator ;
28-
29- import okhttp3 .Headers ;
3030import okhttp3 .MultipartBody ;
31- import okhttp3 .Request ;
3231import okhttp3 .RequestBody ;
3332
3433/**
35- * The IBM Watson Natural Language Classifier service applies deep learning techniques to make predictions about the
36- * best predefined classes for short sentences or phrases. The classes can trigger a corresponding action in an
37- * application, such as directing a request to a location or person, or answering a question. After training, the
38- * service returns information for texts that it hasn't seen before. The response includes the name of the top classes
39- * and confidence values.
34+ * IBM Watson Natural Language Classifier uses machine learning algorithms to return the top matching predefined classes
35+ * for short text input. You create and train a classifier to connect predefined classes to example texts so that the
36+ * service can apply those classes to new inputs.
4037 *
4138 * @version v1
42- * @see <a href= "http://www.ibm.com/watson/developercloud/nl-classifier.html"> Natural Language Classifier</a>
39+ * @see <a href="http://www.ibm.com/watson/developercloud/natural-language-classifier.html">Natural Language
40+ * Classifier</a>
4341 */
4442public class NaturalLanguageClassifier extends WatsonService {
4543
4644 private static final String SERVICE_NAME = "natural_language_classifier" ;
47- private static final String FORM_DATA_TRAINING_DATA = "form-data; name=\" training_data\" " ;
48- private static final String PATH_CLASSIFIERS = "/v1/classifiers" ;
49- private static final String TEXT = "text" ;
50- private static final String PATH_CLASSIFY = "/v1/classifiers/%s/classify" ;
51- private static final String LANGUAGE = "language" ;
52- private static final String NAME = "name" ;
53- private static final String TRAINING_METADATA = "training_metadata" ;
54- private static final String PATH_CLASSIFIER = "/v1/classifiers/%s" ;
5545 private static final String URL = "https://gateway.watsonplatform.net/natural-language-classifier/api" ;
5646
5747 /**
58- * Instantiates a new Natural Language Classifier service.
48+ * Instantiates a new `NaturalLanguageClassifier`.
49+ *
5950 */
6051 public NaturalLanguageClassifier () {
6152 super (SERVICE_NAME );
@@ -65,7 +56,7 @@ public NaturalLanguageClassifier() {
6556 }
6657
6758 /**
68- * Instantiates a new natural language service by username and password.
59+ * Instantiates a new `NaturalLanguageClassifier` with username and password.
6960 *
7061 * @param username the username
7162 * @param password the password
@@ -76,92 +67,99 @@ public NaturalLanguageClassifier(String username, String password) {
7667 }
7768
7869 /**
79- * Returns classification information for a classifier on a phrase.
70+ * Classify.
71+ *
72+ * Returns label information for the input. The status must be `Available` before you can use the classifier to
73+ * classify text. Use `Get information about a classifier` to retrieve the status.
8074 *
81- * @param classifierId The classifier id
82- * @param text The submitted phrase to classify
83- * @return the classification of a phrase with a given classifier
75+ * @param classifyOptions the {@link ClassifyOptions} containing the options for the call
76+ * @return a {@link ServiceCall} with a response type of {@link Classification}
8477 */
85- public ServiceCall <Classification > classify (final String classifierId , final String text ) {
86- Validator .isTrue (( classifierId != null ) && ! classifierId . isEmpty () , "classifierId cannot be null or empty " );
87- Validator . isTrue (( text != null ) && ! text . isEmpty (), "text cannot be null or empty" );
88-
78+ public ServiceCall <Classification > classify (ClassifyOptions classifyOptions ) {
79+ Validator .notNull ( classifyOptions , "classifyOptions cannot be null" );
80+ RequestBuilder builder = RequestBuilder . post ( String . format ( "/v1/classifiers/%s/classify" , classifyOptions
81+ . classifierId ()));
8982 final JsonObject contentJson = new JsonObject ();
90- contentJson .addProperty (TEXT , text );
91- final String path = String .format (PATH_CLASSIFY , classifierId );
92- final Request request = RequestBuilder .post (path ).bodyJson (contentJson ).build ();
93- return createServiceCall (request , ResponseConverterUtils .getObject (Classification .class ));
83+ contentJson .addProperty ("text" , classifyOptions .text ());
84+ builder .bodyJson (contentJson );
85+ return createServiceCall (builder .build (), ResponseConverterUtils .getObject (Classification .class ));
9486 }
9587
9688 /**
97- * Sends data to create and train a classifier, and returns information about the new classifier. The status has the
98- * value of `Training` when the operation is successful, and might remain at this status for a while.
89+ * Create classifier.
9990 *
100- * @param name the classifier name
101- * @param language IETF primary language for the classifier. for example: 'en'
102- * @param trainingData The set of questions and their "keys" used to adapt a system to a domain (the ground truth)
103- * @return the classifier
104- * @see Classifier
91+ * Sends data to create and train a classifier and returns information about the new classifier.
92+ *
93+ * @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for the call
94+ * @return a {@link ServiceCall} with a response type of {@link Classifier}
10595 */
106- public ServiceCall <Classifier > createClassifier (final String name , final String language , final File trainingData ) {
107- Validator .isTrue ((trainingData != null ) && trainingData .exists (), "trainingData cannot be null or not be found" );
108- Validator .isTrue ((language != null ) && !language .isEmpty (), "language cannot be null or empty" );
109-
110- final JsonObject contentJson = new JsonObject ();
111-
112- contentJson .addProperty (LANGUAGE , language );
113-
114- if ((name != null ) && !name .isEmpty ()) {
115- contentJson .addProperty (NAME , name );
116- }
117-
118- final RequestBody body = new MultipartBody .Builder ().setType (MultipartBody .FORM )
119- .addPart (Headers .of (HttpHeaders .CONTENT_DISPOSITION , FORM_DATA_TRAINING_DATA ),
120- RequestBody .create (HttpMediaType .BINARY_FILE , trainingData ))
121- .addFormDataPart (TRAINING_METADATA , contentJson .toString ()).build ();
122-
123- final Request request = RequestBuilder .post (PATH_CLASSIFIERS ).body (body ).build ();
124- return createServiceCall (request , ResponseConverterUtils .getObject (Classifier .class ));
96+ public ServiceCall <Classifier > createClassifier (CreateClassifierOptions createClassifierOptions ) {
97+ Validator .notNull (createClassifierOptions , "createClassifierOptions cannot be null" );
98+ RequestBuilder builder = RequestBuilder .post ("/v1/classifiers" );
99+ MultipartBody .Builder multipartBuilder = new MultipartBody .Builder ();
100+ multipartBuilder .setType (MultipartBody .FORM );
101+ RequestBody trainingMetadataBody = RequestUtils .inputStreamBody (createClassifierOptions .metadata (),
102+ "application/json" );
103+ multipartBuilder .addFormDataPart ("training_metadata" , createClassifierOptions .metadataFilename (),
104+ trainingMetadataBody );
105+ RequestBody trainingDataBody = RequestUtils .inputStreamBody (createClassifierOptions .trainingData (), "text/csv" );
106+ multipartBuilder .addFormDataPart ("training_data" , createClassifierOptions .trainingDataFilename (), trainingDataBody );
107+ builder .body (multipartBuilder .build ());
108+ return createServiceCall (builder .build (), ResponseConverterUtils .getObject (Classifier .class ));
125109 }
126110
127111 /**
128- * Deletes a classifier.
112+ * Delete classifier.
129113 *
130- * @param classifierId the classifier ID
131- * @return the service call
132- * @see Classifier
114+ * @param deleteClassifierOptions the {@link DeleteClassifierOptions} containing the options for the call
115+ * @return a {@link ServiceCall} with a response type of Void
133116 */
134- public ServiceCall <Void > deleteClassifier (String classifierId ) {
135- Validator .isTrue (( classifierId != null ) && ! classifierId . isEmpty () , "classifierId cannot be null or empty " );
136-
137- final Request request = RequestBuilder . delete ( String . format ( PATH_CLASSIFIER , classifierId )). build ( );
138- return createServiceCall (request , ResponseConverterUtils .getVoid ());
117+ public ServiceCall <Void > deleteClassifier (DeleteClassifierOptions deleteClassifierOptions ) {
118+ Validator .notNull ( deleteClassifierOptions , "deleteClassifierOptions cannot be null" );
119+ RequestBuilder builder = RequestBuilder . delete ( String . format ( "/v1/classifiers/%s" , deleteClassifierOptions
120+ . classifierId ()) );
121+ return createServiceCall (builder . build () , ResponseConverterUtils .getVoid ());
139122 }
140123
141124 /**
142- * Retrieves a classifier.
125+ * Get information about a classifier.
143126 *
144- * @param classifierId the classifier ID
145- * @return the classifier list
146- * @see Classifier
127+ * Returns status and other information about a classifier.
128+ *
129+ * @param getClassifierOptions the {@link GetClassifierOptions} containing the options for the call
130+ * @return a {@link ServiceCall} with a response type of {@link Classifier}
147131 */
148- public ServiceCall <Classifier > getClassifier (String classifierId ) {
149- Validator .isTrue (( classifierId != null ) && ! classifierId . isEmpty () , "classifierId cannot be null or empty " );
150-
151- final Request request = RequestBuilder . get ( String . format ( PATH_CLASSIFIER , classifierId )). build ( );
152- return createServiceCall (request , ResponseConverterUtils .getObject (Classifier .class ));
132+ public ServiceCall <Classifier > getClassifier (GetClassifierOptions getClassifierOptions ) {
133+ Validator .notNull ( getClassifierOptions , "getClassifierOptions cannot be null" );
134+ RequestBuilder builder = RequestBuilder . get ( String . format ( "/v1/classifiers/%s" , getClassifierOptions
135+ . classifierId ()) );
136+ return createServiceCall (builder . build () , ResponseConverterUtils .getObject (Classifier .class ));
153137 }
154138
155139 /**
156- * Retrieves the list of classifiers for the user.
140+ * List classifiers.
141+ *
142+ * Returns an empty array if no classifiers are available.
157143 *
158- * @return the classifier list
159- * @see Classifier
144+ * @param listClassifiersOptions the {@link ListClassifiersOptions} containing the options for the call
145+ * @return a {@link ServiceCall} with a response type of {@link ClassifierList}
160146 */
161- public ServiceCall <Classifiers > getClassifiers () {
162- final Request request = RequestBuilder .get (PATH_CLASSIFIERS ).build ();
163- return createServiceCall (request , ResponseConverterUtils .getObject (Classifiers .class ));
147+ public ServiceCall <ClassifierList > listClassifiers (ListClassifiersOptions listClassifiersOptions ) {
148+ RequestBuilder builder = RequestBuilder .get ("/v1/classifiers" );
149+ if (listClassifiersOptions != null ) {
150+ }
151+ return createServiceCall (builder .build (), ResponseConverterUtils .getObject (ClassifierList .class ));
152+ }
164153
154+ /**
155+ * List classifiers.
156+ *
157+ * Returns an empty array if no classifiers are available.
158+ *
159+ * @return a {@link ServiceCall} with a response type of {@link ClassifierList}
160+ */
161+ public ServiceCall <ClassifierList > listClassifiers () {
162+ return listClassifiers (null );
165163 }
166164
167165}
0 commit comments