public override MatchResult Matches(MatchContext context, TokenEntity startToken, PatternMatcher nextPatterMatcher) { var tokenEntity = startToken; var matchResult = new MatchResult(false, this, tokenEntity); if (tokenEntity != null) { var entity = context.Entities.FirstOrDefault(le => le.Start == tokenEntity.Start && le.Type == EntityType); if (entity != null) { // add the matched entity to the children of the currentEntity. context.AddToCurrentEntity(entity); matchResult.Matched = true; matchResult.End = entity.End; matchResult.NextToken = context.GetFirstTokenEntity(entity.End); } } return(matchResult); }
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)); }