Exemplo n.º 1
0
        /// <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());
        }
Exemplo n.º 2
0
        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);
        }