Skip to content

Commit de27fb3

Browse files
committed
analyze working properly
1 parent 98f288c commit de27fb3

File tree

3 files changed

+173
-24
lines changed

3 files changed

+173
-24
lines changed

Examples/ServiceExamples/Scripts/ExampleNaturalLanguageUnderstandingV1.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*
1616
*/
1717

18+
using FullSerializer;
1819
using IBM.Watson.DeveloperCloud.Logging;
1920
using IBM.Watson.DeveloperCloud.Services.NaturalLanguageUnderstanding.v1;
2021
using UnityEngine;
2122

2223
public class ExampleNaturalLanguageUnderstandingV1 : MonoBehaviour
2324
{
2425
NaturalLanguageUnderstanding m_NaturalLanguageUnderstanding = new NaturalLanguageUnderstanding();
26+
private static fsSerializer sm_Serializer = new fsSerializer();
2527

26-
void Start ()
28+
void Start ()
2729
{
2830
LogSystem.InstallDefaultReactors();
2931

@@ -34,19 +36,23 @@ void Start ()
3436
Parameters parameters = new Parameters()
3537
{
3638
text = "Analyze various features of text content at scale. Provide text, raw HTML, or a public URL, and IBM Watson Natural Language Understanding will give you results for the features you request. The service cleans HTML content before analysis by default, so the results can ignore most advertisements and other unwanted content.",
37-
url = null,
38-
html = null,
3939
return_analyzed_text = true,
40-
language = "en"
41-
//features = new Features()
42-
//{
43-
// entities = new EntitiesOptions()
44-
// {
45-
// limit = 50,
46-
// sentiment = true,
47-
// emotion = true,
48-
// }
49-
//}
40+
language = "en",
41+
features = new Features()
42+
{
43+
entities = new EntitiesOptions()
44+
{
45+
limit = 50,
46+
sentiment = true,
47+
emotion = true,
48+
},
49+
keywords = new KeywordsOptions()
50+
{
51+
limit = 50,
52+
sentiment = true,
53+
emotion = true
54+
}
55+
}
5056
};
5157

5258
Log.Debug("ExampleNaturalLanguageUnderstandingV1", "attempting to analyze...");
@@ -56,11 +62,15 @@ void Start ()
5662

5763
private void OnGetModels(ListModelsResults resp, string customData)
5864
{
59-
Log.Debug("ExampleNaturalLanguageUnderstandingV1", "ListModelsResult: {0}", JsonUtility.ToJson(resp));
65+
fsData data = null;
66+
sm_Serializer.TrySerialize(resp, out data).AssertSuccess();
67+
Log.Debug("ExampleNaturalLanguageUnderstandingV1", "ListModelsResult: {0}", data.ToString());
6068
}
6169

6270
private void OnAnalyze(AnalysisResults resp, string customData)
6371
{
64-
Log.Debug("ExampleNaturalLanguageUnderstandingV1", "AnalysisResults: {0}", JsonUtility.ToJson(resp));
72+
fsData data = null;
73+
sm_Serializer.TrySerialize(resp, out data).AssertSuccess();
74+
Log.Debug("ExampleNaturalLanguageUnderstandingV1", "AnalysisResults: {0}", data.ToString());
6575
}
6676
}

Scripts/Services/NaturalLanguageUnderstanding/v1/DataModels.cs

Lines changed: 148 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*
1616
*/
1717

18+
using System;
1819
using FullSerializer;
20+
using System.Collections.Generic;
1921

2022
namespace IBM.Watson.DeveloperCloud.Services.NaturalLanguageUnderstanding.v1
2123
{
@@ -484,7 +486,7 @@ public class DeletedResponse
484486
public string deleted { get; set; }
485487
}
486488

487-
[fsObject]
489+
[fsObject(Converter = typeof(ParametersConverter))]
488490
public class Parameters
489491
{
490492
/// <summary>
@@ -494,12 +496,10 @@ public class Parameters
494496
/// <summary>
495497
/// The HTML file to analyze
496498
/// </summary>
497-
[fsIgnore]
498499
public string html { get; set; }
499500
/// <summary>
500501
/// The web page to analyze
501502
/// </summary>
502-
[fsIgnore]
503503
public string url { get; set; }
504504
/// <summary>
505505
/// Specific features to analyze the document for
@@ -508,26 +508,26 @@ public class Parameters
508508
/// <summary>
509509
/// Remove website elements, such as links, ads, etc
510510
/// </summary>
511-
public bool clean { get; set; }
511+
public bool? clean { get; set; }
512512
/// <summary>
513513
/// XPath query for targeting nodes in HTML
514514
/// </summary>
515515
public string xpath { get; set; }
516516
/// <summary>
517517
/// Whether to use raw HTML content if text cleaning fails
518518
/// </summary>
519-
public bool fallback_to_raw { get; set; }
519+
public bool? fallback_to_raw { get; set; }
520520
/// <summary>
521521
/// Whether or not to return the analyzed text
522522
/// </summary>
523-
public bool return_analyzed_text { get; set; }
523+
public bool? return_analyzed_text { get; set; }
524524
/// <summary>
525525
/// ISO 639-1 code indicating the language to use in the analysis
526526
/// </summary>
527527
public string language { get; set; }
528528
}
529529

530-
[fsObject]
530+
[fsObject(Converter = typeof(FeaturesConverter))]
531531
public class Features
532532
{
533533
/// <summary>
@@ -685,4 +685,145 @@ public class NaturalLanguageUnderstandingVersion
685685
public const string Version = "2017-02-27";
686686
}
687687
#endregion
688+
689+
#region Parameters Converter
690+
public class ParametersConverter : fsConverter
691+
{
692+
private static fsSerializer sm_Serializer = new fsSerializer();
693+
694+
public override bool CanProcess(Type type)
695+
{
696+
return type == typeof(Parameters);
697+
}
698+
699+
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
700+
{
701+
throw new NotImplementedException();
702+
}
703+
704+
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
705+
{
706+
Parameters parameters = (Parameters)instance;
707+
serialized = null;
708+
709+
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
710+
if (parameters.text != null)
711+
serialization.Add("text", new fsData(parameters.text));
712+
713+
if (parameters.url != null)
714+
serialization.Add("url", new fsData(parameters.url));
715+
716+
if (parameters.html != null)
717+
serialization.Add("html", new fsData(parameters.html));
718+
719+
if (parameters.clean != null)
720+
serialization.Add("clean", new fsData((bool)parameters.clean));
721+
722+
if (parameters.xpath != null)
723+
serialization.Add("xpath", new fsData(parameters.xpath));
724+
725+
if (parameters.fallback_to_raw != null)
726+
serialization.Add("fallback_to_raw", new fsData((bool)parameters.fallback_to_raw));
727+
728+
if (parameters.return_analyzed_text != null)
729+
serialization.Add("return_analyzed_text", new fsData((bool)parameters.return_analyzed_text));
730+
731+
if (parameters.xpath != null)
732+
serialization.Add("xpath", new fsData(parameters.xpath));
733+
734+
fsData tempData = null;
735+
sm_Serializer.TrySerialize(parameters.features, out tempData);
736+
serialization.Add("features", tempData);
737+
738+
serialized = new fsData(serialization);
739+
740+
return fsResult.Success;
741+
}
742+
}
743+
#endregion
744+
745+
#region Features Converter
746+
public class FeaturesConverter : fsConverter
747+
{
748+
private static fsSerializer sm_Serializer = new fsSerializer();
749+
750+
public override bool CanProcess(Type type)
751+
{
752+
return type == typeof(Parameters);
753+
}
754+
755+
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
756+
{
757+
throw new NotImplementedException();
758+
}
759+
760+
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
761+
{
762+
Features features = (Features)instance;
763+
serialized = null;
764+
765+
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
766+
767+
fsData tempData = null;
768+
769+
if (features.concepts != null)
770+
{
771+
sm_Serializer.TrySerialize(features.concepts, out tempData);
772+
serialization.Add("concepts", tempData);
773+
}
774+
775+
if (features.emotion != null)
776+
{
777+
sm_Serializer.TrySerialize(features.emotion, out tempData);
778+
serialization.Add("emotion", tempData);
779+
}
780+
781+
if (features.entities != null)
782+
{
783+
sm_Serializer.TrySerialize(features.entities, out tempData);
784+
serialization.Add("entities", tempData);
785+
}
786+
787+
if (features.keywords != null)
788+
{
789+
sm_Serializer.TrySerialize(features.keywords, out tempData);
790+
serialization.Add("keywords", tempData);
791+
}
792+
793+
if (features.metadata != null)
794+
{
795+
sm_Serializer.TrySerialize(features.metadata, out tempData);
796+
serialization.Add("metadata", tempData);
797+
}
798+
799+
if (features.relations != null)
800+
{
801+
sm_Serializer.TrySerialize(features.relations, out tempData);
802+
serialization.Add("relations", tempData);
803+
}
804+
805+
if (features.semantic_roles != null)
806+
{
807+
sm_Serializer.TrySerialize(features.semantic_roles, out tempData);
808+
serialization.Add("semantic_roles", tempData);
809+
}
810+
811+
if (features.sentiment != null)
812+
{
813+
sm_Serializer.TrySerialize(features.sentiment, out tempData);
814+
serialization.Add("sentiment", tempData);
815+
}
816+
817+
if (features.categories != null)
818+
{
819+
sm_Serializer.TrySerialize(features.categories, out tempData);
820+
serialization.Add("categories", tempData);
821+
}
822+
823+
serialized = new fsData(serialization);
824+
825+
return fsResult.Success;
826+
}
827+
}
828+
#endregion
688829
}

Scripts/Services/NaturalLanguageUnderstanding/v1/NaturalLanguageUnderstanding.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ public class NaturalLanguageUnderstanding : IWatsonService
6666
req._Parameters = parameters;
6767
req.Data = customData;
6868
req.OnResponse = OnAnalyzeResponse;
69-
7069
req.Headers["Content-Type"] = "application/json";
7170
req.Headers["Accept"] = "application/json";
7271
req.Parameters["version"] = NaturalLanguageUnderstandingVersion.Version;
7372

7473
fsData data = null;
7574
fsResult r = sm_Serializer.TrySerialize(parameters, out data);
76-
7775
string sendjson = data.ToString();
7876
req.Send = Encoding.UTF8.GetBytes(sendjson);
7977
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_ANALYZE);

0 commit comments

Comments
 (0)