Skip to content

Commit 9321bf0

Browse files
committed
GetCustomCorpora
1 parent e8410b1 commit 9321bf0

File tree

3 files changed

+123
-6
lines changed

3 files changed

+123
-6
lines changed

Examples/ServiceExamples/Scripts/ExampleSpeechToText.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class ExampleSpeechToText : MonoBehaviour
2626
private AudioClip m_AudioClip = new AudioClip();
2727
private SpeechToText m_SpeechToText = new SpeechToText();
2828

29-
private string m_CreatedSessionID;
30-
3129
private string m_CreatedCustomizationID;
3230

3331
void Start()
@@ -102,6 +100,12 @@ private void TestResetCustomization(string customizationID)
102100
m_SpeechToText.ResetCustomization(HandleResetCustomization, customizationID);
103101
}
104102

103+
private void TestGetCustomCorpora(string customizationID)
104+
{
105+
Log.Debug("ExampleSpeechToText", "Attempting to get custom corpora for {0}", customizationID);
106+
m_SpeechToText.GetCustomCorpora(HandleGetCustopmCorpora, customizationID);
107+
}
108+
105109
private void HandleGetModels(Model[] models)
106110
{
107111
if (models != null)
@@ -221,7 +225,9 @@ private void HandleGetCustomization(Customization customization, string customDa
221225
{
222226
Log.Debug("ExampleSpeechToText", "Customization - name: {0} | description: {1} | status: {2}", customization.name, customization.description, customization.status);
223227
Log.Debug("ExampleSpeechToText", "GetCustomization() succeeded!");
224-
TestDeleteCustomization(m_CreatedCustomizationID);
228+
229+
// test get custom corpora
230+
TestGetCustomCorpora(m_CreatedCustomizationID);
225231
}
226232
else
227233
{
@@ -276,4 +282,31 @@ private void HandleResetCustomization(bool success, string customData)
276282
Log.Debug("ExampleSpeechToText", "Failed to reset customization!");
277283
}
278284
}
285+
286+
private void HandleGetCustopmCorpora(Corpora corpora, string customData)
287+
{
288+
if (!string.IsNullOrEmpty(customData))
289+
Log.Debug("ExampleSpeechToText", "CustomData: {0}", customData);
290+
291+
if(corpora != null)
292+
{
293+
if(corpora.corpora.Length > 0)
294+
{
295+
foreach (Corpus corpus in corpora.corpora)
296+
Log.Debug("ExampleSpeechToText", "Corpus - name: {0} | total_words: {1} | out_of_vocabulary_words: {2} | staus: {3}",
297+
corpus.name, corpus.total_words, corpus.out_of_vocabulary_words, corpus.status);
298+
}
299+
else
300+
{
301+
Log.Debug("ExampleSpeechToText", "There are no custom corpora!");
302+
}
303+
304+
Log.Debug("ExampleSpeechToText", "GetCustomCorpora() succeeded!");
305+
TestDeleteCustomization(m_CreatedCustomizationID);
306+
}
307+
else
308+
{
309+
Log.Debug("ExampleSpeechToText", "Failed to get custom corpora!");
310+
}
311+
}
279312
}

Scripts/Services/SpeechToText/SpeechToText.cs

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,95 @@ private void OnUpgradeCustomizationResp(RESTConnector.Request req, RESTConnector
12391239
}
12401240
#endregion
12411241

1242-
#region Custom Corpora
1242+
#region Get Custom Corpora
1243+
/// <summary>
1244+
/// This callback is used by the GetCustomCorpora() function.
1245+
/// </summary>
1246+
/// <param name="corpora">The corpora</param>
1247+
/// <param name="data">Optional custom data.</param>
1248+
public delegate void GetCustomCorporaCallback(Corpora corpora, string data);
1249+
1250+
/// <summary>
1251+
/// Lists information about all corpora that have been added to the specified custom language model. The information includes the total number of words and out-of-vocabulary (OOV) words, name, and status of each corpus. Only the owner of a custom model can use this method to list the model's corpora.
1252+
/// Note: This method is currently a beta release that is available for US English only.
1253+
/// </summary>
1254+
/// <param name="callback">The callback.</param>
1255+
/// <param name="language">The language for which custom models are to be returned. Currently, only en-US (the default) is supported.</param>
1256+
/// <param name="customData">Optional custom data.</param>
1257+
/// <returns></returns>
1258+
public bool GetCustomCorpora(GetCustomCorporaCallback callback, string customizationID, string customData = default(string))
1259+
{
1260+
if (callback == null)
1261+
throw new ArgumentNullException("callback");
1262+
if (string.IsNullOrEmpty(customizationID))
1263+
throw new ArgumentNullException("A customizationID is required to GetCustomCorpora");
1264+
1265+
GetCustomCorporaReq req = new GetCustomCorporaReq();
1266+
req.Callback = callback;
1267+
req.Data = customData;
1268+
req.CustomizationID = customizationID;
1269+
req.OnResponse = OnGetCustomCorporaResp;
1270+
1271+
string service = "/v1/customizations/{0}/corpora";
1272+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(service, customizationID));
1273+
if (connector == null)
1274+
return false;
1275+
1276+
return connector.Send(req);
1277+
}
1278+
1279+
private class GetCustomCorporaReq : RESTConnector.Request
1280+
{
1281+
public GetCustomCorporaCallback Callback { get; set; }
1282+
public string CustomizationID { get; set; }
1283+
public string Data { get; set; }
1284+
}
1285+
1286+
private void OnGetCustomCorporaResp(RESTConnector.Request req, RESTConnector.Response resp)
1287+
{
1288+
Corpora corpora = new Corpora();
1289+
if (resp.Success)
1290+
{
1291+
try
1292+
{
1293+
fsData data = null;
1294+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
1295+
if (!r.Succeeded)
1296+
throw new WatsonException(r.FormattedMessages);
1297+
1298+
object obj = corpora;
1299+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
1300+
if (!r.Succeeded)
1301+
throw new WatsonException(r.FormattedMessages);
1302+
}
1303+
catch (Exception e)
1304+
{
1305+
Log.Error("Speech To Text", "OnGetCustomCorporaResp Exception: {0}", e.ToString());
1306+
resp.Success = false;
1307+
}
1308+
}
1309+
1310+
if (((GetCustomCorporaReq)req).Callback != null)
1311+
((GetCustomCorporaReq)req).Callback(resp.Success ? corpora : null, ((GetCustomCorporaReq)req).Data);
1312+
}
1313+
#endregion
1314+
1315+
#region Delete Custom Corpora
1316+
#endregion
1317+
1318+
#region Add Custom Corpora
1319+
#endregion
1320+
1321+
#region Get Custom Words
1322+
#endregion
1323+
1324+
#region Add Custom Words
1325+
#endregion
1326+
1327+
#region Delete Custom Words
12431328
#endregion
12441329

1245-
#region Custom Words
1330+
#region Get Custom Word
12461331
#endregion
12471332

12481333
#region IWatsonService interface

Scripts/Utilities/Utility.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
using System.Net.NetworkInformation;
2727
using System.Text.RegularExpressions;
2828
using System.Runtime.InteropServices;
29-
using System.Reflection;
3029

3130
namespace IBM.Watson.DeveloperCloud.Utilities
3231
{

0 commit comments

Comments
 (0)