/// <summary> /// Add entity as a child to current entity. /// </summary> /// <param name="entity"></param> public void AddToCurrentEntity(LucyEntity entity) { var token = this.GetFirstTokenEntity(entity.Start); if (token != null) { var prevToken = token.Previous; if (prevToken != null) { // merge entities if they are contigious and have the same resolution. var existingEntity = CurrentEntity.Children.Where(e => e.Type == entity.Type && e.End == prevToken.End).SingleOrDefault(); if (existingEntity != null && JToken.FromObject(entity.Resolution ?? "").ToString() == JToken.FromObject(existingEntity.Resolution ?? "").ToString()) { var mergedEntity = Merge(entity, existingEntity); CurrentEntity.Children.Remove(existingEntity); CurrentEntity.Children.Add(mergedEntity); return; } } } CurrentEntity.Children.Add(entity); this.MergeEntities(CurrentEntity.Children); this.ResolveEntities(CurrentEntity.Children); CurrentEntity.End = CurrentEntity.Children.Max(e => e.End); CurrentEntity.Score = CurrentEntity.GetAllEntities().Count() + ((float)(CurrentEntity.End - CurrentEntity.Start) / this.Text.Length); }
/// <summary> /// Format entities as fixed width string. /// </summary> /// <param name="text">original text</param> /// <param name="entities">entities</param> /// <returns></returns> public static string VisualizeEntity(string text, LucyEntity entity, bool showSpans = true, bool showHierarchy = true) { if (entity == null) { return("No entity"); } StringBuilder sb = new StringBuilder(); if (showSpans) { sb.AppendLine($"==== {entity.Type} ({entity.Score})"); sb.AppendLine(text); var allEntities = new List <LucyEntity>(entity.GetAllEntities()) { entity }; foreach (var grp in allEntities.GroupBy(e => e.Type.ToLower()) .OrderBy(grp => grp.Max(v => v.End - v.Start)) .ThenBy(grp => grp.Min(v => v.Start))) { sb.Append(FormatEntitiesOfSameTypeAsLine(text, grp)); } sb.AppendLine(); } if (showHierarchy) { sb.AppendLine(FormatEntityChildren(string.Empty, entity)); } return(sb.ToString()); }
public void AddNewEntity(LucyEntity entity) { if (entity != null) { NewEntities.Add(entity); } }
private LucyEntity Merge(LucyEntity entity, LucyEntity alternateEntity) { var mergedEntity = new LucyEntity() { Type = entity.Type, Start = Math.Min(entity.Start, alternateEntity.Start), End = Math.Max(entity.End, alternateEntity.End), }; mergedEntity.Text = this.Text.Substring(mergedEntity.Start, mergedEntity.End - mergedEntity.Start); if (entity.Resolution != null && alternateEntity.Resolution == null) { mergedEntity.Resolution = entity.Resolution; } else if (entity.Resolution == null && alternateEntity.Resolution != null) { mergedEntity.Resolution = alternateEntity.Resolution; } else if (entity.Resolution == alternateEntity.Resolution) { mergedEntity.Resolution = entity.Resolution; } else { string resolutionText1 = entity.Resolution?.ToString(); string resolutionTest2 = entity.Resolution?.ToString(); if (resolutionText1.Length > resolutionTest2.Length) { mergedEntity.Resolution = resolutionText1; } else { mergedEntity.Resolution = resolutionTest2; } } if (entity.Children != null) { foreach (var child in entity.Children) { mergedEntity.Children.Add(child); } } if (alternateEntity.Children != null) { foreach (var child in alternateEntity.Children) { mergedEntity.Children.Add(child); } } if (mergedEntity.Children.Count > 1) { this.MergeEntities(mergedEntity.Children); this.ResolveEntities(mergedEntity.Children); } mergedEntity.Score = mergedEntity.GetAllEntities().Count() + ((float)(mergedEntity.End - mergedEntity.Start) / this.Text.Length); return(mergedEntity); }
public bool Contains(LucyEntity entity) { if (this == entity) { return(true); } if (Children.Contains(entity)) { return(true); } return(Children.Any(e => e.Contains(entity))); }
public override bool Equals(object obj) { LucyEntity entity2 = obj as LucyEntity; if (entity2 == null) { return(false); } if (this.Start != entity2.Start) { return(false); } if (this.End != entity2.End) { return(false); } if (!String.Equals(this.Type, entity2.Type, StringComparison.OrdinalIgnoreCase)) { return(false); } if (!String.Equals(this.Text, entity2.Text, StringComparison.OrdinalIgnoreCase)) { return(false); } if ((this.Resolution != null && entity2.Resolution == null) || (entity2.Resolution == null && entity2.Resolution != null) || (this.Resolution?.GetType().Name != entity2.Resolution?.GetType().Name)) { return(false); } if (this.Resolution?.ToString() != entity2.Resolution?.ToString()) { return(false); } if (this.Children.Count != entity2.Children.Count) { return(false); } foreach (var child in this.Children) { if (entity2.Children.Contains(child) == false) { return(false); } } return(true); }
private static string FormatEntityChildren(string prefix, LucyEntity entity) { StringBuilder sb = new StringBuilder(); sb.AppendLine($"{prefix} {entity}"); if (String.IsNullOrEmpty(prefix)) { prefix = "=> "; } foreach (var child in entity.Children.OrderBy(e => e.Start)) { sb.Append(FormatEntityChildren(" " + prefix, child)); } return(sb.ToString()); }
/// <summary> /// Has this token been matched by an entity pattern? /// </summary> /// <param name="tokenEntity"></param> /// <returns></returns> public bool IsTokenMatched(LucyEntity tokenEntity) { return(this.Entities.Any(e => e.Start == tokenEntity.Start)); }