|
15 | 15 | * |
16 | 16 | */ |
17 | 17 |
|
18 | | -using System.Collections; |
19 | | -using System.Collections.Generic; |
20 | | -using UnityEngine; |
21 | | - |
22 | | -public class NaturalLanguageUnderstanding : MonoBehaviour { |
23 | | - |
24 | | - // Use this for initialization |
25 | | - void Start () { |
26 | | - |
27 | | - } |
28 | | - |
29 | | - // Update is called once per frame |
30 | | - void Update () { |
31 | | - |
32 | | - } |
| 18 | +using FullSerializer; |
| 19 | +using IBM.Watson.DeveloperCloud.Connection; |
| 20 | +using IBM.Watson.DeveloperCloud.Logging; |
| 21 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 22 | +using System; |
| 23 | +using System.Text; |
| 24 | + |
| 25 | +namespace IBM.Watson.DeveloperCloud.Services.NaturalLanguageUnderstanding.v1 |
| 26 | +{ |
| 27 | + public class NaturalLanguageUnderstanding : IWatsonService |
| 28 | + { |
| 29 | + #region Private Data |
| 30 | + private const string SERVICE_ID = "NaturalLanguageUnderstandingV1"; |
| 31 | + private static fsSerializer sm_Serializer = new fsSerializer(); |
| 32 | + |
| 33 | + private const string SERVICE_ANALYZE = "/v1/analyze"; |
| 34 | + private const string SERVICE_MODELS = "/v1/models"; |
| 35 | + private const string SERVICE_MODEL = "/v1/models/{0}"; |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region Analyze |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Get Models |
| 42 | + /// <summary> |
| 43 | + /// The callback used by GetModels(). |
| 44 | + /// </summary> |
| 45 | + /// <param name="resp">The GetModels response.</param> |
| 46 | + /// <param name="customData">Optional data.</param> |
| 47 | + public delegate void OnGetModels(ListModelsResults resp, string customData); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Lists available models for Relations and Entities features, including Watson Knowledge Studio |
| 51 | + /// custom models that you have created and linked to your Natural Language Understanding service. |
| 52 | + /// </summary> |
| 53 | + /// <param name="callback">The OnGetModels callback.</param> |
| 54 | + /// <param name="customData">Optional custom data.</param> |
| 55 | + /// <returns>True if the call succeeds, false if the call is unsuccessful.</returns> |
| 56 | + public bool GetModels(OnGetModels callback, string customData = default(string)) |
| 57 | + { |
| 58 | + if (callback == null) |
| 59 | + throw new ArgumentNullException("callback"); |
| 60 | + |
| 61 | + GetModelsRequest req = new GetModelsRequest(); |
| 62 | + req.Callback = callback; |
| 63 | + req.Data = customData; |
| 64 | + req.Parameters["version"] = NaturalLanguageUnderstandingVersion.Version; |
| 65 | + req.OnResponse = OnGetModelsResponse; |
| 66 | + |
| 67 | + RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MODELS); |
| 68 | + if (connector == null) |
| 69 | + return false; |
| 70 | + |
| 71 | + return connector.Send(req); |
| 72 | + } |
| 73 | + |
| 74 | + private class GetModelsRequest : RESTConnector.Request |
| 75 | + { |
| 76 | + public string Data { get; set; } |
| 77 | + public OnGetModels Callback { get; set; } |
| 78 | + } |
| 79 | + |
| 80 | + private void OnGetModelsResponse(RESTConnector.Request req, RESTConnector.Response resp) |
| 81 | + { |
| 82 | + ListModelsResults modelData = new ListModelsResults(); |
| 83 | + |
| 84 | + if (resp.Success) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + fsData data = null; |
| 89 | + fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data); |
| 90 | + |
| 91 | + if (!r.Succeeded) |
| 92 | + throw new WatsonException(r.FormattedMessages); |
| 93 | + |
| 94 | + object obj = modelData; |
| 95 | + r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj); |
| 96 | + if (!r.Succeeded) |
| 97 | + throw new WatsonException(r.FormattedMessages); |
| 98 | + } |
| 99 | + catch (Exception e) |
| 100 | + { |
| 101 | + Log.Error("Discovery", "OnGetModelssResponse Exception: {0}", e.ToString()); |
| 102 | + resp.Success = false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (((GetModelsRequest)req).Callback != null) |
| 107 | + ((GetModelsRequest)req).Callback(resp.Success ? modelData : null, ((GetModelsRequest)req).Data); |
| 108 | + |
| 109 | + } |
| 110 | + #endregion |
| 111 | + |
| 112 | + #region Delete Model |
| 113 | + /// <summary> |
| 114 | + /// The callback used by DeleteModel(). |
| 115 | + /// </summary> |
| 116 | + /// <param name="success">The success of the call.</param> |
| 117 | + /// <param name="customData">Optional custom data.</param> |
| 118 | + public delegate void OnDeleteModel(bool success, string customData); |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Deletes the specified model. |
| 122 | + /// </summary> |
| 123 | + /// <param name="callback">The callback.</param> |
| 124 | + /// <param name="modelId">The model identifier.</param> |
| 125 | + /// <param name="customData">Optional custom data.</param> |
| 126 | + /// <returns>True if the call succeeds, false if the call is unsuccessful.</returns> |
| 127 | + public bool DeleteModel(OnDeleteModel callback, string modelId, string customData = default(string)) |
| 128 | + { |
| 129 | + if (callback == null) |
| 130 | + throw new ArgumentNullException("callback"); |
| 131 | + |
| 132 | + if (string.IsNullOrEmpty(modelId)) |
| 133 | + throw new ArgumentNullException("modelId"); |
| 134 | + |
| 135 | + DeleteModelRequest req = new DeleteModelRequest(); |
| 136 | + req.Callback = callback; |
| 137 | + req.ModelId = modelId; |
| 138 | + req.Data = customData; |
| 139 | + req.Parameters["version"] = NaturalLanguageUnderstandingVersion.Version; |
| 140 | + req.OnResponse = OnDeleteModelResponse; |
| 141 | + req.Delete = true; |
| 142 | + |
| 143 | + RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(SERVICE_MODEL, modelId)); |
| 144 | + if (connector == null) |
| 145 | + return false; |
| 146 | + |
| 147 | + return connector.Send(req); |
| 148 | + } |
| 149 | + |
| 150 | + private class DeleteModelRequest : RESTConnector.Request |
| 151 | + { |
| 152 | + public string Data { get; set; } |
| 153 | + public string ModelId { get; set; } |
| 154 | + public OnDeleteModel Callback { get; set; } |
| 155 | + } |
| 156 | + |
| 157 | + private void OnDeleteModelResponse(RESTConnector.Request req, RESTConnector.Response resp) |
| 158 | + { |
| 159 | + if (((DeleteModelRequest)req).Callback != null) |
| 160 | + ((DeleteModelRequest)req).Callback(resp.Success, ((DeleteModelRequest)req).Data); |
| 161 | + } |
| 162 | + #endregion |
| 163 | + |
| 164 | + #region IWatsonService Interface |
| 165 | + /// <exclude /> |
| 166 | + public string GetServiceID() |
| 167 | + { |
| 168 | + return SERVICE_ID; |
| 169 | + } |
| 170 | + /// <exclude /> |
| 171 | + public void GetServiceStatus(ServiceStatus callback) |
| 172 | + { |
| 173 | + if (Config.Instance.FindCredentials(SERVICE_ID) != null) |
| 174 | + new CheckServiceStatus(this, callback); |
| 175 | + else |
| 176 | + callback(SERVICE_ID, false); |
| 177 | + } |
| 178 | + |
| 179 | + private class CheckServiceStatus |
| 180 | + { |
| 181 | + private NaturalLanguageUnderstanding m_Service = null; |
| 182 | + private ServiceStatus m_Callback = null; |
| 183 | + |
| 184 | + public CheckServiceStatus(NaturalLanguageUnderstanding service, ServiceStatus callback) |
| 185 | + { |
| 186 | + m_Service = service; |
| 187 | + m_Callback = callback; |
| 188 | + |
| 189 | + if (!m_Service.GetModels(OnGetModels, "CheckServiceStatus")) |
| 190 | + m_Callback(SERVICE_ID, false); |
| 191 | + } |
| 192 | + |
| 193 | + private void OnGetModels(ListModelsResults modelsData, string customData) |
| 194 | + { |
| 195 | + if (m_Callback != null) |
| 196 | + m_Callback(SERVICE_ID, modelsData != null); |
| 197 | + } |
| 198 | + } |
| 199 | + #endregion |
| 200 | + } |
33 | 201 | } |
0 commit comments