Exemple #1
0
        /// <summary>
        /// Determine the type of each of the source elements and fill the tokens collection
        /// </summary>
        /// <param name="elements">Array of split elements</param>
        void Tokenise(string[] elements)
        {
            tokens = new List <Token>(elements.Length);

            int position = 0;

            foreach (string el in elements)
            {
                // ensure the word is in lower case and has no space
                string element = el.ToLower().Trim();

                if (String.IsNullOrWhiteSpace(element))
                {
                    continue;
                }

                if (WordStore.IsIgnored(element))
                {
                    // token added as an ignored word
                    tokens.Add(new Token(element, TokenType.Ignored, position));
                    IgnoreCount++;
                }
                else
                {
                    // this is a preposition
                    if (WordStore.IsPreposition(element))
                    {
                        tokens.Add(new Token(element, TokenType.Preposition, position));
                    }
                    // this is a direction
                    else if (WordStore.IsDirection(element))
                    {
                        tokens.Add(new Token(element, TokenType.Direction, position));
                    }
                    // this is a command, but only accept the first command found
                    else if (CommandManager.IsCommand(element, position) && Command == default(Token))
                    {
                        Command = new Token(element, TokenType.Command, position);
                        tokens.Add(Command);
                    }
                    else
                    {
                        tokens.Add(new Token(element, TokenType.Unrecognised, position));
                    }
                }

                position++; // TODO: these two vars could be consolidated, but does it look stupid passing WordCount as position?
                WordCount++;
            }
            elements = null;
        }