Skip to content

Commit 3b720bb

Browse files
committed
streamline getDates and refoactor showSourceText to includeSourceText for consistancy
1 parent ae32de2 commit 3b720bb

File tree

2 files changed

+41
-89
lines changed

2 files changed

+41
-89
lines changed

Examples/ServiceExamples/Scripts/ExampleAlchemyLanguage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ void Start () {
5353
// Log.Debug("ExampleAlchemyLanguage", "Failed to get concepts HTML POST!");
5454

5555
// Get Date URL POST
56-
// if(!m_AlchemyLanguage.GetDatesURL(OnGetDates, m_ExampleURL_watsonJeopardy, null, true))
56+
// if(!m_AlchemyLanguage.GetDates(OnGetDates, m_ExampleURL_watsonJeopardy))
5757
// Log.Debug("ExampleAlchemyLanguage", "Failed to get dates by URL POST");
5858

5959
// Get Date Text POST
60-
// if(!m_AlchemyLanguage.GetDatesText(OnGetDates, m_ExampleText_watsonJeopardy, null, true))
60+
// if(!m_AlchemyLanguage.GetDates(OnGetDates, m_ExampleText_watsonJeopardy))
6161
// Log.Debug("ExampleAlchemyLanguage", "Failed to get dates by text POST");
6262

6363
// Get Date HTML POST
64-
// if(!m_AlchemyLanguage.GetDatesHTML(OnGetDates, watson_beats_jeopardy_html, null, true))
64+
// if(!m_AlchemyLanguage.GetDates(OnGetDates, watson_beats_jeopardy_html))
6565
// Log.Debug("ExampleAlchemyLanguage", "Failed to get dates by HTML POST");
6666

6767
// Get Emotions URL POST

Scripts/Services/AlchemyAPI/AlchemyLanguage.cs

Lines changed: 38 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -242,101 +242,53 @@ private void OnGetRankedConceptsResponse(RESTConnector.Request req, RESTConnecto
242242
private const string SERVICE_GET_DATES_TEXT = "/calls/text/TextExtractDates";
243243
public delegate void OnGetDates(DateData dateData, string data);
244244

245-
public bool GetDatesURL(OnGetDates callback, string url, string anchorDate = default(string), bool includeSourceText = false, string customData = default(string))
245+
public bool GetDates(OnGetDates callback, string source, string anchorDate = default(string), bool includeSourceText = false, string customData = default(string))
246246
{
247247
if (callback == null)
248248
throw new ArgumentNullException("callback");
249-
if (string.IsNullOrEmpty(url))
250-
throw new WatsonException("Please provide a URL for GetDatesURL.");
251-
if (string.IsNullOrEmpty(mp_ApiKey))
252-
SetCredentials();
253-
if(string.IsNullOrEmpty(anchorDate))
254-
anchorDate = GetCurrentDatetime();
255-
256-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_GET_DATES_URL);
257-
if(connector == null)
258-
return false;
259-
260-
GetDatesRequest req = new GetDatesRequest();
261-
req.Callback = callback;
262-
req.Data = string.IsNullOrEmpty(customData) ? url : customData;
263-
264-
req.Parameters["apikey"] = mp_ApiKey;
265-
req.Parameters["outputMode"] = "json";
266-
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
267-
268-
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
269-
req.Forms = new Dictionary<string, RESTConnector.Form>();
270-
req.Forms["url"] = new RESTConnector.Form(url);
271-
req.Forms["anchorDate"] = new RESTConnector.Form(anchorDate);
272-
273-
req.OnResponse = OnGetDatesResponse;
274-
return connector.Send(req);
275-
}
276-
277-
public bool GetDatesText(OnGetDates callback, string text, string anchorDate = default(string), bool includeSourceText = false, string customData = default(string))
278-
{
279-
if (callback == null)
280-
throw new ArgumentNullException("callback");
281-
if (string.IsNullOrEmpty(text))
282-
throw new WatsonException("Please provide text for GetDatesText.");
249+
if (string.IsNullOrEmpty(source))
250+
throw new WatsonException("Please provide a source for GetAuthors.");
283251
if (string.IsNullOrEmpty(mp_ApiKey))
284252
SetCredentials();
285253
if(string.IsNullOrEmpty(anchorDate))
286254
anchorDate = GetCurrentDatetime();
287255

288-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_GET_DATES_TEXT);
289-
if(connector == null)
290-
return false;
291-
292256
GetDatesRequest req = new GetDatesRequest();
293257
req.Callback = callback;
294-
req.Data = string.IsNullOrEmpty(customData) ? text : customData;
258+
req.Data = string.IsNullOrEmpty(customData) ? source : customData;
295259

296260
req.Parameters["apikey"] = mp_ApiKey;
297261
req.Parameters["outputMode"] = "json";
298262
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
299263

300264
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
301265
req.Forms = new Dictionary<string, RESTConnector.Form>();
302-
req.Forms["text"] = new RESTConnector.Form(text);
303266
req.Forms["anchorDate"] = new RESTConnector.Form(anchorDate);
304267

305-
req.OnResponse = OnGetDatesResponse;
306-
return connector.Send(req);
307-
}
308-
309-
public bool GetDatesHTML(OnGetDates callback, string htmlFilePath, string anchorDate = default(string), bool includeSourceText = false, string customData = default(string))
310-
{
311-
if (callback == null)
312-
throw new ArgumentNullException("callback");
313-
if (string.IsNullOrEmpty(htmlFilePath))
314-
throw new WatsonException("Please provide text for GetDatesHTML.");
315-
if (string.IsNullOrEmpty(mp_ApiKey))
316-
SetCredentials();
317-
if(string.IsNullOrEmpty(anchorDate))
318-
anchorDate = GetCurrentDatetime();
319-
320-
string htmlData = default(string);
321-
htmlData = File.ReadAllText(htmlFilePath);
268+
string service;
269+
string normalizedSource = source.Trim().ToLower();
270+
if(normalizedSource.StartsWith("http://") || normalizedSource.StartsWith("https://"))
271+
{
272+
service = SERVICE_GET_DATES_URL;
273+
req.Forms["url"] = new RESTConnector.Form(source);
274+
}
275+
else if(Path.GetExtension(normalizedSource).EndsWith(".html") && !normalizedSource.StartsWith("http://") && !normalizedSource.StartsWith("https://"))
276+
{
277+
service = SERVICE_GET_DATES_HTML;
278+
string htmlData = default(string);
279+
htmlData = File.ReadAllText(source);
280+
req.Forms["html"] = new RESTConnector.Form(htmlData);
281+
}
282+
else
283+
{
284+
service = SERVICE_GET_DATES_TEXT;
285+
req.Forms["text"] = new RESTConnector.Form(source);
286+
}
322287

323-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_GET_DATES_HTML);
288+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, service);
324289
if(connector == null)
325290
return false;
326291

327-
GetDatesRequest req = new GetDatesRequest();
328-
req.Callback = callback;
329-
req.Data = string.IsNullOrEmpty(customData) ? htmlFilePath : customData;
330-
331-
req.Parameters["apikey"] = mp_ApiKey;
332-
req.Parameters["outputMode"] = "json";
333-
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
334-
335-
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
336-
req.Forms = new Dictionary<string, RESTConnector.Form>();
337-
req.Forms["html"] = new RESTConnector.Form(htmlData);
338-
req.Forms["anchorDate"] = new RESTConnector.Form(anchorDate);
339-
340292
req.OnResponse = OnGetDatesResponse;
341293
return connector.Send(req);
342294
}
@@ -486,7 +438,7 @@ public bool ExtractEntities(OnGetEntities callback, string source,
486438
bool includeLinkedData = true,
487439
bool includeQuotations = false,
488440
bool analyzeSentiment = false,
489-
bool showSourceText = false,
441+
bool includeSourceText = false,
490442
bool extractStructuredEntities = true,
491443
string customData = default(string))
492444
{
@@ -510,7 +462,7 @@ public bool ExtractEntities(OnGetEntities callback, string source,
510462
req.Parameters["linkedData"] = Convert.ToInt32(includeLinkedData).ToString();
511463
req.Parameters["quotations"] = Convert.ToInt32(includeQuotations).ToString();
512464
req.Parameters["sentiment"] = Convert.ToInt32(analyzeSentiment).ToString();
513-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
465+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
514466
req.Parameters["structuredEntities"] = Convert.ToInt32(extractStructuredEntities).ToString();
515467

516468
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
@@ -656,7 +608,7 @@ public bool ExtractKeywords(OnGetKeywords callback, string source,
656608
int maxRetrieve = 50,
657609
bool includeKnowledgeGraph = false,
658610
bool analyzeSentiment = false,
659-
bool showSourceText = false,
611+
bool includeSourceText = false,
660612
string customData = default(string))
661613
{
662614
if (callback == null)
@@ -675,7 +627,7 @@ public bool ExtractKeywords(OnGetKeywords callback, string source,
675627
req.Parameters["maxRetrieve"] = Convert.ToInt32(maxRetrieve).ToString();
676628
req.Parameters["knowledgeGraph"] = Convert.ToInt32(includeKnowledgeGraph).ToString();
677629
req.Parameters["sentiment"] = Convert.ToInt32(analyzeSentiment).ToString();
678-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
630+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
679631
req.Parameters["keywordExtractMode"] = "strict";
680632

681633
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
@@ -1026,7 +978,7 @@ public bool GetRelations(OnGetRelations callback, string source,
1026978
bool includeLinkedData = true,
1027979
bool analyzeSentiment = false,
1028980
bool excludeEntitiesInSentiment = false,
1029-
bool showSourceText = false,
981+
bool includeSourceText = false,
1030982
string customData = default(string))
1031983
{
1032984
if (callback == null)
@@ -1052,7 +1004,7 @@ public bool GetRelations(OnGetRelations callback, string source,
10521004
req.Parameters["linkedData"] = Convert.ToInt32(includeLinkedData).ToString();
10531005
req.Parameters["sentiment"] = Convert.ToInt32(analyzeSentiment).ToString();
10541006
req.Parameters["sentimentExcludeEntities"] = Convert.ToInt32(excludeEntitiesInSentiment).ToString();
1055-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
1007+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
10561008
req.Parameters["keywordExtractMode"] = "strict";
10571009

10581010
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
@@ -1127,7 +1079,7 @@ private void OnGetRelationsResponse(RESTConnector.Request req, RESTConnector.Res
11271079
private const string SERVICE_GET_TEXT_SENTIMENT_TEXT = "/calls/text/TextGetTextSentiment";
11281080
public delegate void OnGetTextSentiment(SentimentData sentimentData, string data);
11291081

1130-
public bool GetTextSentiment(OnGetTextSentiment callback, string source, bool showSourceText = false, string customData = default(string))
1082+
public bool GetTextSentiment(OnGetTextSentiment callback, string source, bool includeSourceText = false, string customData = default(string))
11311083
{
11321084
if (callback == null)
11331085
throw new ArgumentNullException("callback");
@@ -1142,7 +1094,7 @@ private void OnGetRelationsResponse(RESTConnector.Request req, RESTConnector.Res
11421094

11431095
req.Parameters["apikey"] = mp_ApiKey;
11441096
req.Parameters["outputMode"] = "json";
1145-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
1097+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
11461098

11471099
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
11481100
req.Forms = new Dictionary<string, RESTConnector.Form>();
@@ -1216,7 +1168,7 @@ private void OnGetTextSentimentResponse(RESTConnector.Request req, RESTConnector
12161168
private const string SERVICE_GET_TARGETED_SENTIMENT_TEXT = "/calls/text/TextGetTargetedSentiment";
12171169
public delegate void OnGetTargetedSentiment(TargetedSentimentData targetedSentimentData, string data);
12181170

1219-
public bool GetTargetedSentiment(OnGetTargetedSentiment callback, string source, string targets, bool showSourceText = false, string customData = default(string))
1171+
public bool GetTargetedSentiment(OnGetTargetedSentiment callback, string source, string targets, bool includeSourceText = false, string customData = default(string))
12201172
{
12211173
if (callback == null)
12221174
throw new ArgumentNullException("callback");
@@ -1233,7 +1185,7 @@ private void OnGetTextSentimentResponse(RESTConnector.Request req, RESTConnector
12331185

12341186
req.Parameters["apikey"] = mp_ApiKey;
12351187
req.Parameters["outputMode"] = "json";
1236-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
1188+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
12371189

12381190
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
12391191
req.Forms = new Dictionary<string, RESTConnector.Form>();
@@ -1308,7 +1260,7 @@ private void OnGetTargetedSentimentResponse(RESTConnector.Request req, RESTConne
13081260
private const string SERVICE_GET_RANKED_TAXONOMY_TEXT = "/calls/text/TextGetRankedTaxonomy";
13091261
public delegate void OnGetRankedTaxonomy(TaxonomyData taxonomyData, string data);
13101262

1311-
public bool GetRankedTaxonomy(OnGetRankedTaxonomy callback, string source, bool showSourceText = false, string customData = default(string))
1263+
public bool GetRankedTaxonomy(OnGetRankedTaxonomy callback, string source, bool includeSourceText = false, string customData = default(string))
13121264
{
13131265
if (callback == null)
13141266
throw new ArgumentNullException("callback");
@@ -1323,7 +1275,7 @@ private void OnGetTargetedSentimentResponse(RESTConnector.Request req, RESTConne
13231275

13241276
req.Parameters["apikey"] = mp_ApiKey;
13251277
req.Parameters["outputMode"] = "json";
1326-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
1278+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
13271279

13281280
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
13291281
req.Forms = new Dictionary<string, RESTConnector.Form>();
@@ -1627,7 +1579,7 @@ private void OnGetTitleResponse(RESTConnector.Request req, RESTConnector.Respons
16271579
public delegate void OnGetCombinedData(CombinedCallData combinedData, string data);
16281580

16291581
public bool GetCombinedData(OnGetCombinedData callback, string source,
1630-
bool showSourceText = false,
1582+
bool includeSourceText = false,
16311583
bool extractAuthors = false,
16321584
bool extractConcepts = true,
16331585
bool extractDates = false,
@@ -1672,7 +1624,7 @@ public bool GetCombinedData(OnGetCombinedData callback, string source,
16721624

16731625
req.Parameters["apikey"] = mp_ApiKey;
16741626
req.Parameters["outputMode"] = "json";
1675-
req.Parameters["showSourceText"] = Convert.ToInt32(showSourceText).ToString();
1627+
req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
16761628

16771629
List<string> requestServices = new List<string>();
16781630
if(extractAuthors)

0 commit comments

Comments
 (0)