internal override WordGroupMatch Match(TripletWordGroup[] groups, int groupIndex) { //mapping of ids to matched groups var matchMapping = new Dictionary<string, IEnumerable<TripletWordGroup>>(); var currentGroupIndex = groupIndex; //process all constraints for (var i = 0; i < _constraints.Length; ++i) { var constraint = _constraints[i]; var nextConstraint = i + 1 >= _constraints.Length ? null : _constraints[i + 1]; var matchedGroups = new List<TripletWordGroup>(); matchMapping[constraint.Id] = matchedGroups; var isSatisfied = false; while (currentGroupIndex < groups.Length) { var currentGroup = groups[currentGroupIndex]; var isMatch = constraint.Match(currentGroup); var isNextMatch = nextConstraint != null && nextConstraint.Match(currentGroup); if (isSatisfied && isNextMatch) //we are greedy - matching as small groups as possible //leting next constraint to match break; if (isMatch) { isSatisfied = true; matchedGroups.Add(currentGroup); ++currentGroupIndex; if (!constraint.IsMultiMatch) //we can match only one word to this constraint break; } else { // current constraint cannot be further satisfied by current group break; } } if (!isSatisfied) //constraint was not satisfied. return null; } var context = new ParsingContext(matchMapping); var triplet = _factory(context); var tripletGroup = new TripletWordGroup(triplet); var match = new WordGroupMatch(tripletGroup, currentGroupIndex - groupIndex); return match; }
/// <inheritdoc/> internal override WordGroupMatch Match(TripletWordGroup[] groups, int groupIndex) { if (groupIndex + Length > groups.Length) //the input is not long enough return null; //predicate rules match one to one to groups var substitutedGroups = new List<TripletWordGroup>(); for (var i = 0; i < _constraints.Length; ++i) { var constraint = _constraints[i]; var group = groups[groupIndex + i]; if (!constraint.Match(group)) //the constraint does'nt match return null; if (constraint.CanBeSubstituted) substitutedGroups.Add(group); } var context = new ParsingContext(substitutedGroups.ToArray()); var predicate = _factory(context); var predicateGroup = new TripletWordGroup(predicate); return new WordGroupMatch(predicateGroup, Length); }