Exemplo n.º 1
0
        private bool Classify(string imagePath, byte[] imageData, OnClassify callback, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en")
        {
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_CLASSIFY);

            if (connector == null)
            {
                return(false);
            }
            ClassifyReq req = new ClassifyReq();

            req.Callback                   = callback;
            req.Timeout                    = REQUEST_TIMEOUT;
            req.OnResponse                 = OnClassifyResp;
            req.AcceptLanguage             = acceptLanguage;
            req.Parameters["api_key"]      = mp_ApiKey;
            req.Parameters["version"]      = VisualRecognitionVersion.Version;
            req.Headers["Content-Type"]    = "application/x-www-form-urlencoded";
            req.Headers["Accept-Language"] = acceptLanguage;

            if (imageData != null)
            {
                req.Send = imageData;
            }

            return(connector.Send(req));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Classifies the given text, invokes the callback with the results.
        /// </summary>
        /// <param name="successCallback">The success callback.</param>
        /// <param name="failCallback">The fail callback.</param>
        /// <param name="classifierId">The ID of the classifier to use.</param>
        /// <param name="text">The text to classify.</param>
        /// <returns>Returns false if we failed to submit the request.</returns>
        public bool Classify(SuccessCallback <ClassifyResult> successCallback, FailCallback failCallback, string classifierId, string text, Dictionary <string, object> customData = null)
        {
            if (successCallback == null)
            {
                throw new ArgumentNullException("successCallback");
            }
            if (failCallback == null)
            {
                throw new ArgumentNullException("failCallback");
            }
            if (string.IsNullOrEmpty(classifierId))
            {
                throw new ArgumentNullException("classifierId");
            }
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v1/classifiers");

            if (connector == null)
            {
                return(false);
            }

            ClassifyReq req = new ClassifyReq();

            req.SuccessCallback        = successCallback;
            req.FailCallback           = failCallback;
            req.HttpMethod             = UnityWebRequest.kHttpVerbPOST;
            req.DisableSslVerification = DisableSslVerification;
            req.CustomData             = customData == null ? new Dictionary <string, object>() : customData;
            if (req.CustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
            {
                foreach (KeyValuePair <string, string> kvp in req.CustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary <string, string> )
                {
                    req.Headers.Add(kvp.Key, kvp.Value);
                }
            }
            req.OnResponse = OnClassifyResp;
            req.Headers["X-IBMCloud-SDK-Analytics"] = "service_name=natural_language_classifier;service_version=v1;operation_id=Classify";
            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));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Classifies image specified by URL.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="callback">Callback.</param>
        /// <param name="owners">Owners.</param>
        /// <param name="classifierIDs">Classifier IDs to be classified against.</param>
        /// <param name="threshold">Threshold.</param>
        /// <param name="acceptLanguage">Accept language.</param>
        public bool Classify(string url, OnClassify callback, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en")
        {
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
            }
            if (string.IsNullOrEmpty(mp_ApiKey))
            {
                throw new WatsonException("FindClassifier - VISUAL_RECOGNITION_API_KEY needs to be defined in config.json");
            }
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_CLASSIFY);

            if (connector == null)
            {
                return(false);
            }

            ClassifyReq req = new ClassifyReq();

            req.Callback       = callback;
            req.OnResponse     = OnClassifyResp;
            req.AcceptLanguage = acceptLanguage;
            req.Headers["Accepted-Language"] = acceptLanguage;
            req.Parameters["api_key"]        = mp_ApiKey;
            req.Parameters["url"]            = url;
            req.Parameters["version"]        = VisualRecognitionVersion.Version;
            if (owners != default(string[]))
            {
                req.Parameters["owners"] = string.Join(",", owners);
            }
            if (classifierIDs != default(string[]))
            {
                req.Parameters["classifier_ids"] = string.Join(",", classifierIDs);
            }
            if (threshold != default(float))
            {
                req.Parameters["threshold"] = threshold;
            }

            return(connector.Send(req));
        }
        /// <summary>
        /// Classifies the given text, invokes the callback with the results.
        /// </summary>
        /// <param name="successCallback">The success callback.</param>
        /// <param name="failCallback">The fail callback.</param>
        /// <param name="classifierId">The ID of the classifier to use.</param>
        /// <param name="text">The text to classify.</param>
        /// <returns>Returns false if we failed to submit the request.</returns>
        public bool Classify(SuccessCallback <ClassifyResult> successCallback, FailCallback failCallback, string classifierId, string text, Dictionary <string, object> customData = null)
        {
            if (successCallback == null)
            {
                throw new ArgumentNullException("successCallback");
            }
            if (failCallback == null)
            {
                throw new ArgumentNullException("failCallback");
            }
            if (string.IsNullOrEmpty(classifierId))
            {
                throw new ArgumentNullException("classifierId");
            }
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v1/classifiers");

            if (connector == null)
            {
                return(false);
            }

            ClassifyReq req = new ClassifyReq();

            req.SuccessCallback = successCallback;
            req.FailCallback    = failCallback;
            req.CustomData      = customData == null ? new Dictionary <string, object>() : customData;
            if (req.CustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
            {
                foreach (KeyValuePair <string, string> kvp in req.CustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary <string, string> )
                {
                    req.Headers.Add(kvp.Key, kvp.Value);
                }
            }
            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));
        }
        /// <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>
        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");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v1/classifiers");

            if (connector == null)
            {
                return(false);
            }

            ClassifyReq req = new ClassifyReq();

            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));
        }
Exemplo n.º 6
0
        /// <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>
        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));
        }