Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions rosette_api/EntitiesResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class EntitiesResponse : RosetteResponse
private const String countKey = "count";
private const String confidenceKey = "confidence";
private const String dbpediaTypeKey = "dbpediaType";
private const String mentionOffsetsKey = "mentionOffsets";
private const String linkingConfidenceKey = "linkingConfidence";
private const String salienceKey = "salience";

/// <summary>
/// Creates an EntitiesResponse from the API's raw output
Expand All @@ -50,7 +53,11 @@ public EntitiesResponse(HttpResponseMessage apiResult) : base(apiResult)
Nullable<int> count = result.Properties().Where((p) => String.Equals(p.Name, countKey)).Any() ? result[countKey].ToObject<int?>() : null;
Nullable<double> confidence = result.Properties().Where((p) => String.Equals(p.Name, confidenceKey)).Any() ? result[confidenceKey].ToObject<double?>() : null;
String dbpediaType = result.Properties().Where((p) => String.Equals(p.Name, dbpediaTypeKey, StringComparison.OrdinalIgnoreCase)).Any() ? result[dbpediaTypeKey].ToString() : null;
entities.Add(new RosetteEntity(mention, normalized, entityID, type, count, confidence, dbpediaType));
JArray mentionOffsetsArr = result.Properties().Where((p) => String.Equals(p.Name, dbpediaTypeKey, StringComparison.OrdinalIgnoreCase)).Any() ? result[mentionOffsetsKey] as JArray : null;
List<MentionOffset> mentionOffsets = mentionOffsetsArr != null ? mentionOffsetsArr.ToObject<List<MentionOffset>>() : null;
Nullable<double> linkingConfidence = result.Properties().Where((p) => String.Equals(p.Name, linkingConfidenceKey, StringComparison.OrdinalIgnoreCase)).Any() ? result[linkingConfidenceKey].ToObject<double?>() : null;
Nullable<double> salience = result.Properties().Where((p) => String.Equals(p.Name, salienceKey, StringComparison.OrdinalIgnoreCase)).Any() ? result[salienceKey].ToObject<double?>() : null;
entities.Add(new RosetteEntity(mention, normalized, entityID, type, count, confidence, dbpediaType, mentionOffsets, linkingConfidence, salience));
}
this.Entities = entities;
}
Expand Down Expand Up @@ -79,9 +86,8 @@ public override bool Equals(object obj)
{
EntitiesResponse other = obj as EntitiesResponse;
List<bool> conditions = new List<bool>() {
this.Entities != null && other.Entities != null ? this.Entities.SequenceEqual(other.Entities) : this.Entities == other.Entities,
this.Entities != null && other.Entities != null ? this.Entities.SequenceEqual(other.Entities) : ReferenceEquals(this.Entities, other.Entities),
this.ResponseHeaders != null && other.ResponseHeaders != null ? this.ResponseHeaders.Equals(other.ResponseHeaders) : this.ResponseHeaders == other.ResponseHeaders,
this.GetHashCode() == other.GetHashCode()
};
return conditions.All(condition => condition);
}
Expand Down
79 changes: 79 additions & 0 deletions rosette_api/MentionOffset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using Newtonsoft.Json;

namespace rosette_api {
/// <summary>
/// Offsets of a piece of text in the document
/// </summary>
[JsonObject(MemberSerialization.OptOut)]
public class MentionOffset : IEquatable<MentionOffset> {

/// <summary>
/// ctor of MentionOffset
/// </summary>
/// <param name="startOffset">offset of the start of text</param>
/// <param name="endOffset">offset of the end of text</param>
public MentionOffset(int startOffset, int endOffset) {
StartOffset = startOffset;
EndOffset = endOffset;
}

/// <summary>
/// Start offset
/// </summary>
[JsonProperty("startOffset")]
public int StartOffset { get; set; }

/// <summary>
/// End offset
/// </summary>
[JsonProperty("endOffset")]
public int EndOffset { get; set; }

/// <summary>
/// Equality
/// </summary>
/// <param name="other">Offset to compare for equality</param>
/// <returns>bool</returns>
public bool Equals(MentionOffset other) {
if (other == null) {
return false;
}
return StartOffset.Equals(other.StartOffset) && EndOffset.Equals(other.EndOffset);
}

/// <summary>
/// Override for Equals
/// </summary>
/// <param name="obj">Object to check for equality</param>
/// <returns>bool</returns>
public override bool Equals(Object obj) {
if (Object.ReferenceEquals(obj, null)) {
return false;
}

MentionOffset mentionOffsetObj = obj as MentionOffset;
if (mentionOffsetObj == null) {
return false;
} else {
return Equals(mentionOffsetObj);
}
}

/// <summary>
/// Override for GetHashCode specific to MentionOffset
/// </summary>
/// <returns>int</returns>
public override int GetHashCode() {
return StartOffset.GetHashCode() ^ EndOffset.GetHashCode();
}

/// <summary>
/// ToString override
/// </summary>
/// <returns>This MentionOffset in JSON form</returns>
public override string ToString() {
return JsonConvert.SerializeObject(this);
}
}
}
49 changes: 43 additions & 6 deletions rosette_api/RosetteEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace rosette_api
/// A class for representing an identified entity and its properties
/// </summary>
[JsonObject(MemberSerialization.OptOut)]
public class RosetteEntity
public class RosetteEntity : IEquatable<RosetteEntity>
{
/// <summary>
/// The Location entity type
Expand Down Expand Up @@ -119,13 +119,31 @@ public class RosetteEntity
/// </summary>
[JsonProperty("confidence")]
public Nullable<double> Confidence { get; set; }

/// <summary>
/// Gets or sets the dbpediaType of the extracted entity
/// </summary>
[JsonProperty("dbpediaType", NullValueHandling = NullValueHandling.Ignore)]
public String DBpediaType { get; set; }

/// <summary>
/// Gets or sets the offsets of the extracted entity
/// </summary>
[JsonProperty("mentionOffsets")]
public List<MentionOffset> MentionOffsets { get; set; }

/// <summary>
/// Gets or sets the linking confidence of the extracted entity
/// </summary>
[JsonProperty("linkingConfidence", NullValueHandling = NullValueHandling.Ignore)]
public Nullable<double> LinkingConfidence { get; set; }

/// <summary>
/// Gets or sets the salience of the extracted entity
/// </summary>
[JsonProperty("salience", NullValueHandling = NullValueHandling.Ignore)]
public Nullable<double> Salience { get; set; }

/// <summary>
/// Creates an entity
/// </summary>
Expand All @@ -136,8 +154,12 @@ public class RosetteEntity
/// <param name="count">The number of times this entity appeared in the input to the API</param>
/// <param name="confidence">The confidence of this entity appeared in the input to the API</param>
/// <param name="dbpediaType">The DBpedia type of the entity</param>
/// <param name="mentionOffsets">The mention offsets of the entity</param>
/// <param name="linkingConfidence">The linking confidence of the entity</param>
/// <param name="salience">The salience of the entity</param>
public RosetteEntity(string mention, string normalizedMention, EntityID id, string entityType, int? count,
double? confidence, string dbpediaType)
double? confidence, string dbpediaType, List<MentionOffset> mentionOffsets, double? linkingConfidence,
double? salience)
{
this.Mention = mention;
this.NormalizedMention = normalizedMention;
Expand All @@ -146,16 +168,28 @@ public RosetteEntity(string mention, string normalizedMention, EntityID id, stri
this.EntityType = entityType;
this.Confidence = confidence;
this.DBpediaType = dbpediaType;
this.MentionOffsets = mentionOffsets;
this.LinkingConfidence = linkingConfidence;
this.Salience = salience;
}

/// <summary>
/// Equals for RosetteEntity
/// </summary>
/// <param name="other">RosetteEntity</param>
/// <returns></returns>
protected bool Equals(RosetteEntity other)
public bool Equals(RosetteEntity other)
{
return string.Equals(Mention, other.Mention) && string.Equals(NormalizedMention, other.NormalizedMention) && Equals(ID, other.ID) && string.Equals(EntityType, other.EntityType) && Count == other.Count && Confidence.Equals(other.Confidence) && string.Equals(DBpediaType, other.DBpediaType);
return string.Equals(Mention, other.Mention)
&& string.Equals(NormalizedMention, other.NormalizedMention)
&& Equals(ID, other.ID)
&& string.Equals(EntityType, other.EntityType)
&& Count == other.Count
&& Confidence.Equals(other.Confidence)
&& string.Equals(DBpediaType, other.DBpediaType)
&& MentionOffsets.SequenceEqual(other.MentionOffsets)
&& LinkingConfidence.Equals(other.LinkingConfidence)
&& Salience.Equals(other.Salience);
}

/// <summary>
Expand All @@ -168,7 +202,7 @@ public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((RosetteEntity) obj);
return Equals(obj as RosetteEntity);
}

/// <summary>
Expand All @@ -186,6 +220,9 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ Count.GetHashCode();
hashCode = (hashCode * 397) ^ Confidence.GetHashCode();
hashCode = (hashCode * 397) ^ (DBpediaType != null ? DBpediaType.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (MentionOffsets != null ? MentionOffsets.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (LinkingConfidence != null ? LinkingConfidence.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Salience != null ? Salience.GetHashCode() : 0);
return hashCode;
}
}
Expand Down
Loading