internal IEnumerable <SemanticItem> Generate(SemanticItem input) { var inputText = input.InstantiateInputWithEntityVariables(); var inputTokens = tokenize(inputText); initializeParts(inputText); while (true) { var item = generateCurrentState(inputTokens, _pattern.Constraints); if (item != null) { yield return(item); } var i = 0; while (!_parts[i].ShiftNext()) { i += 1; if (i >= _parts.Length) { yield break; } _parts[i - 1].Reset(); } } }
private IEnumerable <SemanticItem> fetchMatchingEntries(SemanticItem queryItem) { if (queryItem.Question == null) { throw new NotImplementedException(); } if (queryItem.Answer != null) { throw new NotImplementedException(); } var result = new List <SemanticItem>(); if (queryItem.Question == Question.IsItTrue) { //TODO conditions should be more general thing var inputConditions = new HashSet <string>(queryItem.Constraints.Conditions); var condition = queryItem.InstantiateInputWithEntityVariables(); logDependency(condition); if (inputConditions.Contains(condition)) { //condition is met because of input result.Add(SemanticItem.Entity(YesAnswer)); } var negatedCondition = Constraints.Negate(condition); if (inputConditions.Contains(negatedCondition)) { //we have got a negative result result.Add(SemanticItem.Entity(NoAnswer)); } } if (result.Count > 0) { return(result); } foreach (var item in GetData()) { if (item.Question != queryItem.Question) { continue; } /*if ((item.Constraints.Input == null) != (queryItem.Constraints == null)) * //when input is provided it should be considered * continue;*/ if (item.Constraints.Input != null) { var matcher = new InputMatcher(); var itemMatches = matcher.Match(item, queryItem).ToArray(); var matchedItems = new List <SemanticItem>(); foreach (var match in itemMatches) { if (meetConditions(queryItem, match)) { matchedItems.Add(match); } } result.AddRange(matchedItems.OrderByDescending(i => rank(i))); } else { if (meetConditions(queryItem, item)) { result.Add(item); } } if (result.Count > 0) { break; } } return(result); }