From ccdfca8f515b15b038a9918e3b026a9dca5ff4da Mon Sep 17 00:00:00 2001 From: Li Xu Date: Fri, 9 Jun 2017 09:24:32 -0400 Subject: [PATCH] WS-1155: I think this is all (#75) * WS-1155: I think this is all * WS-1155: change all decimal to double * WS-1155: fixes --- rosette_api/EntitiesResponse.cs | 4 +++- rosette_api/RosetteEntity.cs | 14 ++++++++++++-- rosette_api/SentimentResponse.cs | 18 ++++++++++-------- rosette_apiUnitTests/rosette_apiUnitTests.cs | 14 +++++++------- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/rosette_api/EntitiesResponse.cs b/rosette_api/EntitiesResponse.cs index 94895d55..124afd56 100644 --- a/rosette_api/EntitiesResponse.cs +++ b/rosette_api/EntitiesResponse.cs @@ -29,6 +29,7 @@ public class EntitiesResponse : RosetteResponse private const String normalizedKey = "normalized"; private const String entityIDKey = "entityId"; private const String countKey = "count"; + private const String confidenceKey = "confidence"; /// /// Creates a CategoriesResponse from the API's raw output @@ -46,7 +47,8 @@ public EntitiesResponse(HttpResponseMessage apiResult) : base(apiResult) EntityID entityID = entityIDStr != null ? new EntityID(entityIDStr) : null; String normalized = result.Properties().Where((p) => String.Equals(p.Name, normalizedKey, StringComparison.OrdinalIgnoreCase)).Any() ? result[normalizedKey].ToString() : null; Nullable count = result.Properties().Where((p) => String.Equals(p.Name, countKey)).Any() ? result[countKey].ToObject() : null; - entities.Add(new RosetteEntity(mention, normalized, entityID, type, count)); + Nullable confidence = result.Properties().Where((p) => String.Equals(p.Name, confidenceKey)).Any() ? result[confidenceKey].ToObject() : null; + entities.Add(new RosetteEntity(mention, normalized, entityID, type, count, confidence)); } this.Entities = entities; } diff --git a/rosette_api/RosetteEntity.cs b/rosette_api/RosetteEntity.cs index 12a3497b..76453675 100644 --- a/rosette_api/RosetteEntity.cs +++ b/rosette_api/RosetteEntity.cs @@ -114,6 +114,12 @@ public class RosetteEntity [JsonProperty("count")] public Nullable Count { get; set; } + /// + /// Gets or sets the confidence of the extracted entity + /// + [JsonProperty("confidence")] + public Nullable Confidence { get; set; } + /// /// Creates an entity /// @@ -122,13 +128,15 @@ public class RosetteEntity /// The mention's id /// The entity type /// The number of times this entity appeared in the input to the API - public RosetteEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable count) + /// The confidence of this entity appeared in the input to the API + public RosetteEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable count, Nullable confidence) { this.Mention = mention; this.NormalizedMention = normalizedMention; this.ID = id; this.Count = count; this.EntityType = entityType; + this.Confidence = confidence; } /// @@ -147,6 +155,7 @@ public override bool Equals(object obj) this.EntityType == other.EntityType, this.Mention == other.Mention, this.NormalizedMention == other.NormalizedMention, + this.Confidence == other.Confidence, this.GetHashCode() == other.GetHashCode() }; return conditions.All(condition => condition); @@ -168,7 +177,8 @@ public override int GetHashCode() int h2 = this.ID != null ? this.ID.GetHashCode() : 1; int h3 = this.Count != null ? this.Count.GetHashCode() : 1; int h4 = this.EntityType != null ? this.EntityType.GetHashCode() : 1; - return h0 ^ h1 ^ h2 ^ h3 ^ h4; + int h5 = this.Confidence != null ? this.Confidence.GetHashCode() : 1; + return h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5; } /// diff --git a/rosette_api/SentimentResponse.cs b/rosette_api/SentimentResponse.cs index 21f1784f..17dd85e0 100644 --- a/rosette_api/SentimentResponse.cs +++ b/rosette_api/SentimentResponse.cs @@ -49,7 +49,7 @@ public SentimentResponse(HttpResponseMessage apiResult) List entitySentiments = new List(); JObject docResult = this.ContentDictionary.ContainsKey(docKey) ? this.ContentDictionary[docKey] as JObject : new JObject(); string docSentiment = docResult.Properties().Where((p) => p.Name == labelKey).Any() ? docResult[labelKey].ToString() : null; - decimal? docSentimentConfidence = docResult.Properties().Where((p) => p.Name == confidenceKey).Any() ? docResult[confidenceKey].ToObject() : new decimal?(); + double? docSentimentConfidence = docResult.Properties().Where((p) => p.Name == confidenceKey).Any() ? docResult[confidenceKey].ToObject() : new double?(); this.DocSentiment = docSentiment != null && docSentimentConfidence != null ? new RosetteSentiment(docSentiment, docSentimentConfidence) : null; JArray enumerableResults = this.ContentDictionary.ContainsKey(entitiesKey) ? this.ContentDictionary[entitiesKey] as JArray : new JArray(); foreach (JObject result in enumerableResults) @@ -61,12 +61,12 @@ public SentimentResponse(HttpResponseMessage apiResult) EntityID entityID = entityIDStr != null ? new EntityID(entityIDStr) : null; Nullable count = result.Properties().Where((p) => p.Name == countKey).Any() ? result[countKey].ToObject() : null; string sentiment = null; - Nullable confidence = null; + Nullable confidence = null; if (result.Properties().Where((p) => p.Name == sentimentKey).Any()) { JObject entitySentiment = result[sentimentKey].ToObject(); sentiment = entitySentiment.Properties().Where((p) => p.Name == labelKey).Any() ? entitySentiment[labelKey].ToString() : null; - confidence = entitySentiment.Properties().Where((p) => p.Name == confidenceKey).Any() ? entitySentiment[confidenceKey].ToObject() : new decimal?(); + confidence = entitySentiment.Properties().Where((p) => p.Name == confidenceKey).Any() ? entitySentiment[confidenceKey].ToObject() : new double?(); } entitySentiments.Add(new RosetteSentimentEntity(mention, normalizedMention, entityID, type, count, sentiment, confidence)); } @@ -155,7 +155,7 @@ public enum SentimentLabel /// On a scale of 0-1, the confidence in the Label's correctness. /// [JsonProperty(confidenceKey)] - public Nullable Confidence; + public Nullable Confidence; /// /// The label indicating the sentiment /// @@ -168,7 +168,7 @@ public enum SentimentLabel /// /// The sentiment label: "pos", "neu", or "neg" /// An indicator of confidence in the label being correct. A range from 0-1. - public RosetteSentiment(String sentiment, Nullable confidence) + public RosetteSentiment(String sentiment, Nullable confidence) { switch (sentiment) { @@ -247,7 +247,7 @@ public class RosetteSentimentEntity : RosetteEntity /// The number of times the entity appeared in the text /// The contextual sentiment of the entity /// The confidence that the sentiment was correctly identified - public RosetteSentimentEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable count, String sentiment, Nullable confidence) : base(mention, normalizedMention, id, entityType, count) + public RosetteSentimentEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable count, String sentiment, Nullable confidence) : base(mention, normalizedMention, id, entityType, count, confidence) { this.Sentiment = new SentimentResponse.RosetteSentiment(sentiment, confidence); } @@ -265,7 +265,8 @@ public bool EntityEquals(RosetteEntity other) this.Mention != null && other.Mention != null ? this.Mention.Equals(other.Mention) : this.Mention == other.Mention, this.ID != null && other.ID != null ? this.ID.Equals(other.ID) : this.ID == other.ID, this.EntityType != null && other.EntityType != null ? this.EntityType.Equals(other.EntityType) : this.EntityType == other.EntityType, - this.Count != null && other.Count != null ? this.Count.Equals(other.Count) : this.Count == other.Count + this.Count != null && other.Count != null ? this.Count.Equals(other.Count) : this.Count == other.Count, + this.Confidence != null && other.Confidence != null ? this.Confidence.Equals(other.Confidence) : this.Confidence == other.Confidence }; return conditions.All(condition => condition); } @@ -305,7 +306,8 @@ public override int GetHashCode() int h3 = this.Mention != null ? this.Mention.GetHashCode() : 1; int h4 = this.NormalizedMention != null ? this.NormalizedMention.GetHashCode() : 1; int h5 = this.Sentiment != null ? this.Sentiment.GetHashCode() : 1; - return h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5; + int h6 = this.Confidence != null ? this.Confidence.GetHashCode() : 1; + return h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6; } /// diff --git a/rosette_apiUnitTests/rosette_apiUnitTests.cs b/rosette_apiUnitTests/rosette_apiUnitTests.cs index d643c7dc..13feb652 100755 --- a/rosette_apiUnitTests/rosette_apiUnitTests.cs +++ b/rosette_apiUnitTests/rosette_apiUnitTests.cs @@ -13,7 +13,7 @@ using System.Web.Script.Serialization; using Newtonsoft.Json; -namespace rosette_apiUnitTests { +namespace rosette_apiUnitTests { [TestFixture] public class rosetteResponseTests { @@ -555,8 +555,8 @@ public void Categories_File_Test() { public void EntityTestFull() { Init(); - RosetteEntity e0 = new RosetteEntity("Dan Akroyd", "Dan Akroyd", new EntityID("Q105221"), "PERSON", 2); - RosetteEntity e1 = new RosetteEntity("The Hollywood Reporter", "The Hollywood Reporter", new EntityID("Q61503"), "ORGANIZATION", 1); + RosetteEntity e0 = new RosetteEntity("Dan Akroyd", "Dan Akroyd", new EntityID("Q105221"), "PERSON", 2, 0.99); + RosetteEntity e1 = new RosetteEntity("The Hollywood Reporter", "The Hollywood Reporter", new EntityID("Q61503"), "ORGANIZATION", 1, null); List entities = new List() { e0, e1 }; string headersAsString = " { \"Content-Type\": \"application/json\", \"date\": \"Thu, 11 Aug 2016 15:47:32 GMT\", \"server\": \"openresty\", \"strict-transport-security\": \"max-age=63072000; includeSubdomains; preload\", \"x-rosetteapi-app-id\": \"1409611723442\", \"x-rosetteapi-concurrency\": \"50\", \"x-rosetteapi-request-id\": \"d4176692-4f14-42d7-8c26-4b2d8f7ff049\", \"content-length\": \"72\", \"connection\": \"Close\" }"; Dictionary responseHeaders = new JavaScriptSerializer().Deserialize>(headersAsString); @@ -873,7 +873,7 @@ public void NameDeduplication_Content_Test() { Assert.AreEqual(response.Content["response"], "OK"); # pragma warning restore 618 } - + [Test] public void NameDeduplication_Content_NoThreshold_Test() { _mockHttp.When(_testUrl + "name-deduplication").Respond(HttpStatusCode.OK, "application/json", "{'response': 'OK'}"); @@ -1151,9 +1151,9 @@ public void Sentences_File_Test() { public void SentimentTestFull() { Init(); - SentimentResponse.RosetteSentiment docSentiment = new SentimentResponse.RosetteSentiment("pos", (decimal)0.7962072011038756); - RosetteSentimentEntity e0 = new RosetteSentimentEntity("Dan Akroyd", "Dan Akroyd", new EntityID("Q105221"), "PERSON", 2, "neg", (decimal)0.5005508052749595); - RosetteSentimentEntity e1 = new RosetteSentimentEntity("The Hollywood Reporter", "The Hollywood Reporter", new EntityID("Q61503"), "ORGANIZATION", 1, "pos", (decimal)0.5338094035254866); + SentimentResponse.RosetteSentiment docSentiment = new SentimentResponse.RosetteSentiment("pos", (double)0.7962072011038756); + RosetteSentimentEntity e0 = new RosetteSentimentEntity("Dan Akroyd", "Dan Akroyd", new EntityID("Q105221"), "PERSON", 2, "neg", (double)0.5005508052749595); + RosetteSentimentEntity e1 = new RosetteSentimentEntity("The Hollywood Reporter", "The Hollywood Reporter", new EntityID("Q61503"), "ORGANIZATION", 1, "pos", (double)0.5338094035254866); List entities = new List() { e0, e1 }; string headersAsString = " { \"Content-Type\": \"application/json\", \"date\": \"Thu, 11 Aug 2016 15:47:32 GMT\", \"server\": \"openresty\", \"strict-transport-security\": \"max-age=63072000; includeSubdomains; preload\", \"x-rosetteapi-app-id\": \"1409611723442\", \"x-rosetteapi-concurrency\": \"50\", \"x-rosetteapi-request-id\": \"d4176692-4f14-42d7-8c26-4b2d8f7ff049\", \"content-length\": \"72\", \"connection\": \"Close\" }"; Dictionary responseHeaders = new JavaScriptSerializer().Deserialize>(headersAsString);