Example #1
0
        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));
        }