Skip to content

Commit 81ebdf3

Browse files
committed
Analyze request data models, analyze method
1 parent a2adc97 commit 81ebdf3

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

Scripts/Services/NaturalLanguageUnderstanding/v1/DataModels.cs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,193 @@ public class DeletedResponse
484484
public string deleted { get; set; }
485485
}
486486

487+
[fsObject]
488+
public class Parameters
489+
{
490+
/// <summary>
491+
/// The plain text to analyze
492+
/// </summary>
493+
public string text { get; set; }
494+
/// <summary>
495+
/// The HTML file to analyze
496+
/// </summary>
497+
public string html { get; set; }
498+
/// <summary>
499+
/// The web page to analyze
500+
/// </summary>
501+
public string url { get; set; }
502+
/// <summary>
503+
/// Specific features to analyze the document for
504+
/// </summary>
505+
public Features features { get; set; }
506+
/// <summary>
507+
/// Remove website elements, such as links, ads, etc
508+
/// </summary>
509+
public bool clean { get; set; }
510+
/// <summary>
511+
/// XPath query for targeting nodes in HTML
512+
/// </summary>
513+
public string xpath { get; set; }
514+
/// <summary>
515+
/// Whether to use raw HTML content if text cleaning fails
516+
/// </summary>
517+
public bool fallback_to_raw { get; set; }
518+
/// <summary>
519+
/// Whether or not to return the analyzed text
520+
/// </summary>
521+
public bool return_analyzed_text { get; set; }
522+
/// <summary>
523+
/// ISO 639-1 code indicating the language to use in the analysis
524+
/// </summary>
525+
public string language { get; set; }
526+
}
527+
528+
[fsObject]
529+
public class Features
530+
{
531+
/// <summary>
532+
/// Whether or not to return the concepts that are mentioned in the analyzed text
533+
/// </summary>
534+
public ConceptsOptions concepts { get; set; }
535+
/// <summary>
536+
/// Whether or not to extract the emotions implied in the analyzed text
537+
/// </summary>
538+
public EmotionOptions emotion { get; set; }
539+
/// <summary>
540+
/// Whether or not to extract detected entity objects from the analyzed text
541+
/// </summary>
542+
public EntitiesOptions entities { get; set; }
543+
/// <summary>
544+
/// Whether or not to return the keywords in the analyzed text
545+
/// </summary>
546+
public KeywordsOptions keywords { get; set; }
547+
/// <summary>
548+
/// Whether or not the author, publication date, and title of the analyzed text should be returned.This parameter is only available for URL and HTML input
549+
/// </summary>
550+
public MetadataOptions metadata { get; set; }
551+
/// <summary>
552+
/// Whether or not to return the relationships between detected entities in the analyzed text
553+
/// </summary>
554+
public RelationsOptions relations { get; set; }
555+
/// <summary>
556+
/// Whether or not to return the subject-action-object relations from the analyzed text
557+
/// </summary>
558+
public SemanticRolesOptions semantic_roles { get; set; }
559+
/// <summary>
560+
/// Whether or not to return the overall sentiment of the analyzed text
561+
/// </summary>
562+
public SentimentOptions sentiment { get; set; }
563+
/// <summary>
564+
/// Whether or not to return the high level category the content is categorized as (i.e.news, art)
565+
/// </summary>
566+
public CategoriesOptions categories { get; set; }
567+
}
568+
569+
[fsObject]
570+
public class ConceptsOptions
571+
{
572+
/// <summary>
573+
/// Maximum number of concepts to return
574+
/// </summary>
575+
public int limit { get; set; }
576+
}
577+
578+
[fsObject]
579+
public class EmotionOptions
580+
{
581+
/// <summary>
582+
/// Set this to false to hide document-level emotion results
583+
/// </summary>
584+
public bool document { get; set; }
585+
/// <summary>
586+
/// Emotion results will be returned for each target string that is found in the document
587+
/// </summary>
588+
public string[] targets { get; set; }
589+
}
590+
591+
[fsObject]
592+
public class EntitiesOptions
593+
{
594+
/// <summary>
595+
/// Maximum number of entities to return
596+
/// </summary>
597+
public int limit { get; set; }
598+
/// <summary>
599+
/// Enter a custom model ID to override the standard entity detection model
600+
/// </summary>
601+
public string model { get; set; }
602+
/// <summary>
603+
/// Set this to true to return sentiment information for detected entities
604+
/// </summary>
605+
public bool sentiment { get; set; }
606+
/// <summary>
607+
/// Set this to true to analyze emotion for detected keywords
608+
/// </summary>
609+
public bool emotion { get; set; }
610+
}
611+
612+
[fsObject]
613+
public class KeywordsOptions
614+
{
615+
/// <summary>
616+
/// Maximum number of keywords to return
617+
/// </summary>
618+
public int limit { get; set; }
619+
/// <summary>
620+
/// Set this to true to return sentiment information for detected keywords
621+
/// </summary>
622+
public bool sentiment { get; set; }
623+
/// <summary>
624+
/// Set this to true to analyze emotion for detected keywords
625+
/// </summary>
626+
public bool emotion { get; set; }
627+
}
628+
629+
[fsObject]
630+
public class MetadataOptions { }
631+
632+
[fsObject]
633+
public class RelationsOptions
634+
{
635+
/// <summary>
636+
/// Enter a custom model ID to override the default model
637+
/// </summary>
638+
public string model { get; set; }
639+
}
640+
641+
[fsObject]
642+
public class SemanticRolesOptions
643+
{
644+
/// <summary>
645+
/// Maximum number of semantic_roles results to return
646+
/// </summary>
647+
public int limit { get; set; }
648+
/// <summary>
649+
/// Set this to true to return keyword information for subjects and objects
650+
/// </summary>
651+
public bool keywords { get; set; }
652+
/// <summary>
653+
/// Set this to true to return entity information for subjects and objects
654+
/// </summary>
655+
public bool entities { get; set; }
656+
}
657+
658+
[fsObject]
659+
public class SentimentOptions
660+
{
661+
/// <summary>
662+
/// Set this to false to hide document-level sentiment results
663+
/// </summary>
664+
public bool document { get; set; }
665+
/// <summary>
666+
/// Sentiment results will be returned for each target string that is found in the document
667+
/// </summary>
668+
public string[] targets { get; set; }
669+
}
670+
671+
[fsObject]
672+
public class CategoriesOptions { }
673+
487674
#region Version
488675
/// <summary>
489676
/// The Discovery version.

Scripts/Services/NaturalLanguageUnderstanding/v1/NaturalLanguageUnderstanding.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
using IBM.Watson.DeveloperCloud.Connection;
2020
using IBM.Watson.DeveloperCloud.Logging;
2121
using IBM.Watson.DeveloperCloud.Utilities;
22+
using MiniJSON;
2223
using System;
24+
using System.Collections.Generic;
2325
using System.Text;
2426

2527
namespace IBM.Watson.DeveloperCloud.Services.NaturalLanguageUnderstanding.v1
@@ -36,6 +38,83 @@ public class NaturalLanguageUnderstanding : IWatsonService
3638
#endregion
3739

3840
#region Analyze
41+
/// <summary>
42+
/// The callback used by AddEnvironment().
43+
/// </summary>
44+
/// <param name="resp">The Environment response.</param>
45+
/// <param name="customData">Optional custom data.</param>
46+
public delegate void OnAnalyze(AnalysisResults resp, string customData);
47+
48+
/// <summary>
49+
/// Creates a new environment. You can only create one environment per service instance.An attempt to create another environment
50+
/// will result in an error. The size of the new environment can be controlled by specifying the size parameter.
51+
/// </summary>
52+
/// <param name="callback">The OnAddEnvironment callback.</param>
53+
/// <param name="addEnvironmentData">The AddEnvironmentData.</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 Analyze(OnAnalyze callback, Parameters parameters, string customData = default(string))
57+
{
58+
if (callback == null)
59+
throw new ArgumentNullException("callback");
60+
if (parameters == null)
61+
throw new ArgumentNullException("parameters");
62+
63+
AnalyzeRequest req = new AnalyzeRequest();
64+
req.Callback = callback;
65+
req._Parameters = parameters;
66+
req.Data = customData;
67+
req.OnResponse = OnAnalyzeResponse;
68+
69+
req.Headers["Content-Type"] = "application/json";
70+
req.Headers["Accept"] = "application/json";
71+
req.Parameters["version"] = NaturalLanguageUnderstandingVersion.Version;
72+
string sendjson = Json.Serialize(parameters);
73+
req.Send = Encoding.UTF8.GetBytes(sendjson);
74+
75+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_ANALYZE);
76+
if (connector == null)
77+
return false;
78+
79+
return connector.Send(req);
80+
}
81+
82+
private class AnalyzeRequest : RESTConnector.Request
83+
{
84+
public string Data { get; set; }
85+
public Parameters _Parameters { get; set; }
86+
public OnAnalyze Callback { get; set; }
87+
}
88+
89+
private void OnAnalyzeResponse(RESTConnector.Request req, RESTConnector.Response resp)
90+
{
91+
AnalysisResults analysisResults = new AnalysisResults();
92+
93+
if (resp.Success)
94+
{
95+
try
96+
{
97+
fsData data = null;
98+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
99+
100+
if (!r.Succeeded)
101+
throw new WatsonException(r.FormattedMessages);
102+
103+
object obj = analysisResults;
104+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
105+
if (!r.Succeeded)
106+
throw new WatsonException(r.FormattedMessages);
107+
}
108+
catch (Exception e)
109+
{
110+
Log.Error("Discovery", "OnAnalyzeResponse Exception: {0}", e.ToString());
111+
resp.Success = false;
112+
}
113+
}
114+
115+
if (((AnalyzeRequest)req).Callback != null)
116+
((AnalyzeRequest)req).Callback(resp.Success ? analysisResults : null, ((AnalyzeRequest)req).Data);
117+
}
39118
#endregion
40119

41120
#region Get Models

0 commit comments

Comments
 (0)