Assets/Watson/Scripts/Services/NaturalLanguageClassifier/NaturalLanguageClassifier.cs
/// <summary>
/// Classifies the given text, invokes the callback with the results.
/// </summary>
/// <param name="classifierId">The ID of the classifier to use.</param>
/// <param name="text">The text to classify.</param>
/// <param name="callback">The callback to invoke with the results.</param>
/// <returns>Returns false if we failed to submit the request.</returns>
Classifies the given text, invokes the callback with the results.
Classifies the given text invokes the callback with the results .
public bool Classify(string classifierId, string text, OnClassify callback)
        {
            if (string.IsNullOrEmpty(classifierId))
                throw new ArgumentNullException("classifierId");
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException("text");
            if (callback == null)
                throw new ArgumentNullException("callback");
            string textId = Utility.GetMD5(text);
            if (!DisableCache)
            {
                DataCache cache = null;
                if (!m_ClassifyCache.TryGetValue(classifierId, out cache))
                {
                    cache = new DataCache("NaturalLanguageClassifier_" + classifierId);
                    m_ClassifyCache[classifierId] = cache;
                }
                byte[] cached = cache.Find(textId);
                if (cached != null)
                {
                    ClassifyResult res = ProcessClassifyResult(cached);
                    if (res != null)
                    {
                        callback(res);
                        return true;
                    }
                }
            }
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/classifiers");
            if (connector == null)
                return false;
            ClassifyReq req = new ClassifyReq();
            req.TextId = textId;
            req.ClassiferId = classifierId;
            req.Callback = callback;
            req.OnResponse = OnClassifyResp;
            req.Function = "/" + classifierId + "/classify";
            req.Headers["Content-Type"] = "application/json";
            Dictionary<string, object> body = new Dictionary<string, object>();
            body["text"] = text;
            req.Send = Encoding.UTF8.GetBytes(Json.Serialize(body));
            return connector.Send(req);
        }
public bool Classify ( string classifierId , string text , OnClassify callback ) { if ( string . IsNullOrEmpty ( classifierId ) ) throw new ArgumentNullException ( " " ) ; if ( string . IsNullOrEmpty ( text ) ) throw new ArgumentNullException ( " " ) ; if ( callback == null ) throw new ArgumentNullException ( " " ) ; string textId = Utility . GetMD5 ( text ) ; if ( ! DisableCache ) { DataCache cache = null ; if ( ! m_ClassifyCache . TryGetValue ( classifierId , out cache ) ) { cache = new DataCache ( " " + classifierId ) ; m_ClassifyCache [ classifierId ] = cache ; } byte [ ] cached = cache . Find ( textId ) ; if ( cached != null ) { ClassifyResult res = ProcessClassifyResult ( cached ) ; if ( res != null ) { callback ( res ) ; return true ; } } } RESTConnector connector = RESTConnector . GetConnector ( SERVICE_ID , " " ) ; if ( connector == null ) return false ; ClassifyReq req = new ClassifyReq ( ) ; req . TextId = textId ; req . ClassiferId = classifierId ; req . Callback = callback ; req . OnResponse = OnClassifyResp ; req . Function = " " + classifierId + " " ; req . Headers [ " " ] = " " ; Dictionary < string , object > body = new Dictionary < string , object > ( ) ; body [ " " ] = text ; req . Send = Encoding . UTF8 . GetBytes ( Json . Serialize ( body ) ) ; return connector . Send ( req ) ; }
Classifies the given text, invokes the callback with the results.
Classifies the given text invokes the callback with the results .
Returns false if we failed to submit the request.
Returns false if we failed to submit the request .
classifierId
string
The ID of the classifier to use.
The ID of the classifier to use .
text
string
The text to classify.
The text to classify .
callback
OnClassify
The callback to invoke with the results.
The callback to invoke with the results .