Skip to content

Commit

Permalink
WS-1155: I think this is all (#75)
Browse files Browse the repository at this point in the history
* WS-1155: I think this is all

* WS-1155: change all decimal to double

* WS-1155: fixes
  • Loading branch information
Li Xu authored and cp2boston committed Jun 9, 2017
1 parent 734e8d8 commit ccdfca8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
4 changes: 3 additions & 1 deletion rosette_api/EntitiesResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
/// Creates a CategoriesResponse from the API's raw output
Expand All @@ -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<int> count = result.Properties().Where((p) => String.Equals(p.Name, countKey)).Any() ? result[countKey].ToObject<int?>() : null;
entities.Add(new RosetteEntity(mention, normalized, entityID, type, count));
Nullable<double> confidence = result.Properties().Where((p) => String.Equals(p.Name, confidenceKey)).Any() ? result[confidenceKey].ToObject<double?>() : null;
entities.Add(new RosetteEntity(mention, normalized, entityID, type, count, confidence));
}
this.Entities = entities;
}
Expand Down
14 changes: 12 additions & 2 deletions rosette_api/RosetteEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public class RosetteEntity
[JsonProperty("count")]
public Nullable<int> Count { get; set; }

/// <summary>
/// Gets or sets the confidence of the extracted entity
/// </summary>
[JsonProperty("confidence")]
public Nullable<double> Confidence { get; set; }

/// <summary>
/// Creates an entity
/// </summary>
Expand All @@ -122,13 +128,15 @@ public class RosetteEntity
/// <param name="id">The mention's id</param>
/// <param name="entityType">The entity type</param>
/// <param name="count">The number of times this entity appeared in the input to the API</param>
public RosetteEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable<int> count)
/// <param name="confidence">The confidence of this entity appeared in the input to the API</param>
public RosetteEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable<int> count, Nullable<double> confidence)
{
this.Mention = mention;
this.NormalizedMention = normalizedMention;
this.ID = id;
this.Count = count;
this.EntityType = entityType;
this.Confidence = confidence;
}

/// <summary>
Expand All @@ -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);
Expand All @@ -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;
}

/// <summary>
Expand Down
18 changes: 10 additions & 8 deletions rosette_api/SentimentResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public SentimentResponse(HttpResponseMessage apiResult)
List<RosetteSentimentEntity> entitySentiments = new List<RosetteSentimentEntity>();
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<decimal?>() : new decimal?();
double? docSentimentConfidence = docResult.Properties().Where((p) => p.Name == confidenceKey).Any() ? docResult[confidenceKey].ToObject<double?>() : 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)
Expand All @@ -61,12 +61,12 @@ public SentimentResponse(HttpResponseMessage apiResult)
EntityID entityID = entityIDStr != null ? new EntityID(entityIDStr) : null;
Nullable<int> count = result.Properties().Where((p) => p.Name == countKey).Any() ? result[countKey].ToObject<int?>() : null;
string sentiment = null;
Nullable<decimal> confidence = null;
Nullable<double> confidence = null;
if (result.Properties().Where((p) => p.Name == sentimentKey).Any())
{
JObject entitySentiment = result[sentimentKey].ToObject<JObject>();
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<decimal?>() : new decimal?();
confidence = entitySentiment.Properties().Where((p) => p.Name == confidenceKey).Any() ? entitySentiment[confidenceKey].ToObject<double?>() : new double?();
}
entitySentiments.Add(new RosetteSentimentEntity(mention, normalizedMention, entityID, type, count, sentiment, confidence));
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public enum SentimentLabel
/// On a scale of 0-1, the confidence in the Label's correctness.
/// </summary>
[JsonProperty(confidenceKey)]
public Nullable<decimal> Confidence;
public Nullable<double> Confidence;
/// <summary>
/// The label indicating the sentiment
/// </summary>
Expand All @@ -168,7 +168,7 @@ public enum SentimentLabel
/// </summary>
/// <param name="sentiment">The sentiment label: "pos", "neu", or "neg"</param>
/// <param name="confidence">An indicator of confidence in the label being correct. A range from 0-1.</param>
public RosetteSentiment(String sentiment, Nullable<decimal> confidence)
public RosetteSentiment(String sentiment, Nullable<double> confidence)
{
switch (sentiment)
{
Expand Down Expand Up @@ -247,7 +247,7 @@ public class RosetteSentimentEntity : RosetteEntity
/// <param name="count">The number of times the entity appeared in the text</param>
/// <param name="sentiment">The contextual sentiment of the entity</param>
/// <param name="confidence">The confidence that the sentiment was correctly identified</param>
public RosetteSentimentEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable<int> count, String sentiment, Nullable<decimal> confidence) : base(mention, normalizedMention, id, entityType, count)
public RosetteSentimentEntity(String mention, String normalizedMention, EntityID id, String entityType, Nullable<int> count, String sentiment, Nullable<double> confidence) : base(mention, normalizedMention, id, entityType, count, confidence)
{
this.Sentiment = new SentimentResponse.RosetteSentiment(sentiment, confidence);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions rosette_apiUnitTests/rosette_apiUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace rosette_apiUnitTests {
namespace rosette_apiUnitTests {

[TestFixture]
public class rosetteResponseTests {
Expand Down Expand Up @@ -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<RosetteEntity> entities = new List<RosetteEntity>() { 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<string, string> responseHeaders = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(headersAsString);
Expand Down Expand Up @@ -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'}");
Expand Down Expand Up @@ -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<RosetteSentimentEntity> entities = new List<RosetteSentimentEntity>() { 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<string, string> responseHeaders = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(headersAsString);
Expand Down

0 comments on commit ccdfca8

Please sign in to comment.