/// <summary> /// Identifies a language from the given text. /// </summary> /// <param name="text">The text sample to ID.</param> /// <param name="callback">The callback to receive the results.</param> /// <returns></returns> public bool Identify(string text, IdentifyCallback callback) { if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } if (callback == null) { throw new ArgumentNullException("callback"); } RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v2/identify"); if (connector == null) { return(false); } IdentifyReq req = new IdentifyReq(); req.Callback = callback; req.Send = Encoding.UTF8.GetBytes(text); req.Headers["Content-Type"] = "text/plain"; req.OnResponse = OnIdentifyResponse; return(connector.Send(req)); }
private void OnIdentifyResponse(RESTConnector.Request r, RESTConnector.Response resp) { IdentifyReq req = r as IdentifyReq; if (req == null) { throw new WatsonException("Unexpected Request type."); } if (resp.Success) { if (req.Callback != null) { req.Callback(Encoding.UTF8.GetString(resp.Data)); } } else { Log.Error("Translate", "Identify() failed: {0}", resp.Error); if (req.Callback != null) { req.Callback(null); } } }
/// <summary> /// Identifies a language from the given text. /// </summary> /// <param name="successCallback">The success callback.</param> /// <param name="failCallback">The fail callback.</param> /// <param name="text">The text sample to ID.</param> /// <returns></returns> public bool Identify(SuccessCallback <IdentifiedLanguages> successCallback, FailCallback failCallback, string text, Dictionary <string, object> customData = null) { if (successCallback == null) { throw new ArgumentNullException("successCallback"); } if (failCallback == null) { throw new ArgumentNullException("failCallback"); } if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v2/identify"); if (connector == null) { return(false); } IdentifyReq req = new IdentifyReq(); req.SuccessCallback = successCallback; req.FailCallback = failCallback; req.CustomData = customData == null ? new Dictionary <string, object>() : customData; req.Send = Encoding.UTF8.GetBytes(text); req.Headers["Content-Type"] = "text/plain"; req.Headers["Accept"] = "application/json"; req.OnResponse = OnIdentifyResponse; return(connector.Send(req)); }
private void OnIdentifyResponse(RESTConnector.Request req, RESTConnector.Response resp) { IdentifyReq result = new IdentifyReq(); fsData data = null; Dictionary <string, object> customData = ((IdentifyReq)req).CustomData; if (resp.Success) { try { fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } object obj = result; r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } customData.Add("json", data); } catch (Exception e) { Log.Error("LanguageTranslation.OnIdentifyResponse()", "OnIdentifyResponse Exception: {0}", e.ToString()); resp.Success = false; } } if (resp.Success) { if (((IdentifyReq)req).SuccessCallback != null) { ((IdentifyReq)req).SuccessCallback(result, customData); } } else { if (((IdentifyReq)req).FailCallback != null) { ((IdentifyReq)req).FailCallback(resp.Error, customData); } } }
/// <summary> /// Identifies a language from the given text. /// </summary> /// <param name="successCallback">The success callback.</param> /// <param name="failCallback">The fail callback.</param> /// <param name="text">The text sample to ID.</param> /// <returns></returns> public bool Identify(SuccessCallback <IdentifiedLanguages> successCallback, FailCallback failCallback, string text, Dictionary <string, object> customData = null) { if (successCallback == null) { throw new ArgumentNullException("successCallback"); } if (failCallback == null) { throw new ArgumentNullException("failCallback"); } if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v3/identify"); if (connector == null) { return(false); } IdentifyReq req = new IdentifyReq(); req.Parameters["version"] = VersionDate; 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.Send = Encoding.UTF8.GetBytes(text); req.Headers["Content-Type"] = "text/plain"; req.Headers["Accept"] = "application/json"; req.OnResponse = OnIdentifyResponse; req.Headers["X-IBMCloud-SDK-Analytics"] = "service_name=language_translator;service_version=v3;operation_id=Identify"; return(connector.Send(req)); }
/// <summary> /// Identifies a language from the given text. /// </summary> /// <param name="successCallback">The success callback.</param> /// <param name="failCallback">The fail callback.</param> /// <param name="text">The text sample to ID.</param> /// <returns></returns> public bool Identify(SuccessCallback <IdentifiedLanguages> successCallback, FailCallback failCallback, string text, Dictionary <string, object> customData = null) { if (successCallback == null) { throw new ArgumentNullException("successCallback"); } if (failCallback == null) { throw new ArgumentNullException("failCallback"); } if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } RESTConnector connector = RESTConnector.GetConnector(Credentials, "/v2/identify"); if (connector == null) { return(false); } IdentifyReq req = new IdentifyReq(); 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.Send = Encoding.UTF8.GetBytes(text); req.Headers["Content-Type"] = "text/plain"; req.Headers["Accept"] = "application/json"; req.OnResponse = OnIdentifyResponse; return(connector.Send(req)); }
private void OnIdentifyResponse(RESTConnector.Request req, RESTConnector.Response resp) { IdentifyReq identifyRequest = req as IdentifyReq; if (identifyRequest == null) { throw new WatsonException("Unexpected Request type."); } string identifiedLanguages; if (resp.Success) { identifiedLanguages = Encoding.UTF8.GetString(resp.Data); string customData = identifyRequest.Data; if (((IdentifyReq)req).Callback != null) { ((IdentifyReq)req).Callback(resp.Success ? identifiedLanguages : null, (!string.IsNullOrEmpty(customData) ? customData : identifiedLanguages)); } } }