예제 #1
0
        public void LucyEntityComparerTests_SimpleEquals()
        {
            LucyEntity one = new LucyEntity()
            {
                Start      = 10,
                End        = 12,
                Type       = "a",
                Text       = "a",
                Resolution = "a"
            };
            LucyEntity two = new LucyEntity()
            {
                Start      = 10,
                End        = 12,
                Type       = "a",
                Text       = "a",
                Resolution = "a"
            };

            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Start = 11;
            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            two.Start = 11;
            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Type = "b";
            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            two.Type = "b";
            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Resolution = "b";
            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            two.Resolution = "b";
            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Resolution = null;
            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            two.Resolution = null;
            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Text = "b";
            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            two.Text = "b";
            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());
        }
예제 #2
0
        public void LucyEntityComparerTests_Children()
        {
            LucyEntity one = new LucyEntity()
            {
                Start      = 10,
                End        = 12,
                Type       = "a",
                Text       = "a",
                Resolution = "a"
            };
            LucyEntity two = new LucyEntity()
            {
                Start      = 10,
                End        = 12,
                Type       = "a",
                Text       = "a",
                Resolution = "a",
                Children   = new LucyEntitySet()
                {
                    new LucyEntity()
                    {
                        Start = 15,
                        End   = 20,
                        Type  = "b",
                        Text  = "b"
                    }
                }
            };

            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
            one.Children.Add(new LucyEntity()
            {
                Start = 15,
                End   = 20,
                Type  = "b",
                Text  = "b"
            });

            Assert.IsTrue(one.Equals(two));
            Assert.AreEqual(one.GetHashCode(), two.GetHashCode());

            one.Children.Clear();
            one.Children.Add(new LucyEntity()
            {
                Start = 15,
                End   = 21,
                Type  = "b",
                Text  = "b"
            });

            Assert.IsFalse(one.Equals(two));
            Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode());
        }
예제 #3
0
        private static IEnumerable <LucyEntity> GetEntitiesFromObject(Activity activity, JObject entitiesObject)
        {
            dynamic instanceObject = entitiesObject.Property("$instance");

            if (instanceObject != null)
            {
                foreach (var prop in instanceObject.Value.Properties())
                {
                    dynamic instances   = prop.Value;
                    dynamic resolutions = entitiesObject[prop.Name];

                    // get resolution
                    for (int i = 0; i < resolutions.Count; i++)
                    {
                        dynamic resolution = (JToken)resolutions[i];
                        dynamic instance   = instances[i];

                        var start     = (int)instance.startIndex;
                        var end       = (int)instance.endIndex;
                        var newEntity = new LucyEntity()
                        {
                            Type  = ((string)instance.type).StartsWith("builtin.") ? ((string)instance.type).Replace("builtin.", "") : instance.type,
                            Start = start,
                            End   = end,
                            // Score = metadata.Score,
                            Text = activity.Text?.Substring(start, end - start)
                        };

                        if (resolution is JObject && resolution.Property("$instance") != null)
                        {
                            foreach (var entity in GetEntitiesFromObject(activity, resolution))
                            {
                                newEntity.Children.Add(entity);
                            }
                        }
                        else
                        {
                            newEntity.Resolution = resolution;
                        }

                        yield return(newEntity);
                    }
                }
            }
        }
예제 #4
0
        public void LucyEntityComparerTests_LucyEntitySet()
        {
            var one = new LucyEntity()
            {
                Start = 13,
                End   = 20,
                Type  = "b",
                Text  = "b"
            };
            var two = new LucyEntity()
            {
                Start = 15,
                End   = 20,
                Type  = "b",
                Text  = "b"
            };
            var set = new LucyEntitySet()
            {
                JObject.FromObject(two).ToObject <LucyEntity>()
            };

            Assert.IsFalse(set.Contains(one));
            Assert.IsTrue(set.Contains(two));
        }
예제 #5
0
        public override MatchResult Matches(MatchContext context, TokenEntity startToken, PatternMatcher nextPatternMatcher)
        {
            var tokenEntity = startToken;

            if (tokenEntity != null)
            {
                if (nextPatternMatcher != null)
                {
                    MatchResult nextPatternMatch = nextPatternMatcher?.Matches(context, tokenEntity, null);
                    if (nextPatternMatch.Matched && nextPatternMatch.NextToken != tokenEntity)
                    {
                        return(new MatchResult(false, this)
                        {
                            NextPatternMatch = nextPatternMatch
                        });
                    }
                }

                if (!context.IsTokenMatched(tokenEntity))
                {
                    // if last child is a wildcard and it's end matches the last token's end
                    // then we will merge the wildcards together.
                    var previousToken  = context.GetPrevTokenEntity(tokenEntity);
                    var wildcardEntity = context.CurrentEntity.Children.FirstOrDefault(wildcard => wildcard.Type == this.entityType && wildcard.End == previousToken.End);
                    if (wildcardEntity != null)
                    {
                        var newEntity = new LucyEntity()
                        {
                            Type       = entityType,
                            Start      = wildcardEntity.Start,
                            End        = tokenEntity.End,
                            Score      = ((float)tokenEntity.End - wildcardEntity.Start) / context.Text.Length / 2,
                            Text       = context.Text.Substring(wildcardEntity.Start, tokenEntity.End - wildcardEntity.Start),
                            Resolution = context.Text.Substring(wildcardEntity.Start, tokenEntity.End - wildcardEntity.Start),
                        };

                        // remove old entity
                        context.CurrentEntity.Children.Remove(wildcardEntity);

                        // add new merged wildcard entity "joe" "smith" => "joe smith"
                        context.AddToCurrentEntity(newEntity);
                    }
                    else
                    {
                        var newEntity = new LucyEntity()
                        {
                            Type       = entityType,
                            Start      = tokenEntity.Start,
                            End        = tokenEntity.End,
                            Score      = ((float)tokenEntity.End - tokenEntity.Start) / context.Text.Length / 2,
                            Text       = context.Text.Substring(tokenEntity.Start, tokenEntity.End - tokenEntity.Start),
                            Resolution = context.Text.Substring(tokenEntity.Start, tokenEntity.End - tokenEntity.Start)
                        };
                        context.AddToCurrentEntity(newEntity);
                    }

                    return(new MatchResult(true, this, context.GetNextTokenEntity(tokenEntity), tokenEntity.Start, tokenEntity.End));
                }
            }

            return(new MatchResult(false, this));
        }