private WordNode(string word, int index, bool isrecognized, WordNode parent) { SenenceIndex = index; IsRecognizedWord = isrecognized; Parent = parent; Word = string.IsNullOrWhiteSpace(word) ? throw new Exception() : IsRecognizedWord?word.ToLower() : word.ToUpper(); }
private static IEnumerable <WordNode> FindLeafNodes(WordNode node) { if (node.Children.Count == 0) { return new[] { node } } ; else { return(node.Children.SelectMany(FindLeafNodes)); } }
public static WordNode RecognizedWord(string word, int index, WordNode parent = null) { return(new WordNode(word, index, true, parent)); }
public static IEnumerable <WordNode> BuildSentenceTree( string @string, int index, List <string> orderedDictionary, WordNode parent) { if (index == @string.Length) { yield break; } var found = false; for (int cnt = 0; cnt < orderedDictionary.Count; cnt++) { var dictionaryWord = orderedDictionary[cnt]; if (IsMatch(@string, index, dictionaryWord)) { found = true; var recognized = WordNode.RecognizedWord( @string.Substring(index, dictionaryWord.Length), index, parent); recognized.Children.AddRange(BuildSentenceTree( @string, index + dictionaryWord.Length, orderedDictionary, recognized)); yield return(recognized); } } if (found) { yield break; } //couldnt find any words from the dictionary, so find the next full unrecognizable word for (int cnt = index + 1; cnt < @string.Length; cnt++) { if (HasMatch(@string, cnt, orderedDictionary)) { var unrecognized = WordNode.UnrecognizedWord( @string.Substring(index, cnt - index), index, parent); unrecognized.Children.AddRange(BuildSentenceTree( @string, index + unrecognized.Word.Length, orderedDictionary, unrecognized)); yield return(unrecognized); yield break; } } //couldn't find anything yield return(WordNode.UnrecognizedWord( @string.Substring(index), index, parent)); }