コード例 #1
0
        private (bool, string) TryParseProgramExecution()
        {
            XmlNodeList executionKeywords = XmlManager.GetFirstLevelNodeChildren("query-identifiers.xml", "program-execution-words");

            if (executionKeywords == null)
            {
                return(false, null);
            }

            string[] commandComponents = command.Split(' ');

            bool nameStarted = false;

            string name = "";

            foreach (string value in commandComponents)
            {
                if (nameStarted)
                {
                    name += $"{(name == "" ? "" : " ")}{value}";
                }
                else if (IsProgramExecutionKeyword(value, executionKeywords))
                {
                    nameStarted = true;
                }
            }

            return(name != "", name);
        }
コード例 #2
0
        private (bool, string) TryParseInformationLookup()
        {
            GUIController.LogOutput("Parsing information lookup");

            XmlNodeList questionWords = XmlManager.GetFirstLevelNodeChildren("query-identifiers.xml", "question-words");

            if (questionWords == null)
            {
                return(false, null);
            }

            string[] commandComponents = command.Split(' ');

            string information = "";

            string tempInformation = "";

            List <string> previousWords = new List <string>();

            bool informationStarted = false;

            string afterParams = "";

            foreach (string value in commandComponents)
            {
                if (!informationStarted)
                {
                    previousWords.Add("");

                    (bool success, string afterParams)lookupKeywordMatch = (false, null);

                    for (int i = 0; i < previousWords.Count; i++)
                    {
                        previousWords[i] += $"{(previousWords[i] == "" ? "" : " ")}{value}";

                        if (!lookupKeywordMatch.success)
                        {
                            lookupKeywordMatch = IsInformationLookupKeyword(previousWords[i], questionWords);
                        }
                    }

                    if (lookupKeywordMatch.success)
                    {
                        afterParams = lookupKeywordMatch.afterParams;
                    }
                    else
                    {
                        information += value;

                        informationStarted = true;
                    }
                }
                else
                {
                    if (afterParams != "" && afterParams.Length >= (tempInformation.Length + value.Length))
                    {
                        if (afterParams.Substring(0, tempInformation.Length + value.Length) == tempInformation + value)
                        {
                            if (tempInformation + value == afterParams)
                            {
                                break;
                            }
                            else
                            {
                                tempInformation += $"{(tempInformation == "" ? "" : " ")}{value}";
                            }

                            continue;
                        }
                    }

                    if (information != "" && tempInformation != "")
                    {
                        tempInformation = $" {tempInformation}";
                    }

                    information += $"{tempInformation}{(information == "" ? "" : " ")}{value}";

                    tempInformation = "";
                }
            }

            if (debug)
            {
                GUIController.LogOutput($"AfterParams: '{afterParams}'\nInformation: '{information}'\nSuccess: {informationStarted}");
            }

            return(informationStarted, information);
        }
コード例 #3
0
        /// <summary>
        /// Determines whether the command is a mathematical operation
        /// </summary>
        /// <returns>True if the command is a mathematical operation</returns>
        private (bool, string) TryConstructMathsExpression()
        {
            XmlDocument commandWords = XmlManager.LoadDocument("mathematical-command-words.xml");

            XmlNodeList mathsIdentifiers = XmlManager.GetFirstLevelNodeChildren("query-identifiers.xml", "maths-question-words");

            if (mathsIdentifiers == null)
            {
                return(false, null);
            }

            List <string> filteredCommand = NumericalWordParser.ParseAllNumericalWords(this.command).Split(' ').ToList();

            if (debug)
            {
                DisplayArray(filteredCommand);
            }

            int identifierOffset = 0;

            string currentIdentifier = "";

            while (filteredCommand.Count > 0 && identifierOffset < filteredCommand.Count)
            {
                if (currentIdentifier != "")
                {
                    currentIdentifier += " ";
                }

                currentIdentifier += filteredCommand[identifierOffset];

                if (IsQueryIdentifier(currentIdentifier, mathsIdentifiers))
                {
                    for (int i = 0; i < identifierOffset + 1; i++)
                    {
                        filteredCommand.RemoveAt(0);
                    }

                    identifierOffset = 0;

                    currentIdentifier = "";

                    continue;
                }

                identifierOffset++;
            }

            if (debug)
            {
                DisplayArray(filteredCommand);
            }

            string[] commandComponents = filteredCommand.ToArray();

            string equation = ""; // Construct this whenever a keyword or number is found

            bool previousWasNumber = false;

            List <string> previousWords = new List <string>();

            string keywordFirstWord = "";

            string symbolToAppend = "";

            bool dualOperator = false;

            bool awaitingMatch = false;

            foreach (string value in commandComponents)
            {
                if (IsNumerical(value))
                {
                    if (awaitingMatch)
                    {
                        return(false, null);
                    }

                    if (symbolToAppend != "")
                    {
                        symbolToAppend += " ";
                    }

                    equation += $"{symbolToAppend}";

                    symbolToAppend = "";

                    if (previousWasNumber)
                    {
                        if (dualOperator)
                        {
                            equation += " ";
                        }
                        else
                        {
                            return(false, null);
                        }
                    }

                    previousWasNumber = true;

                    previousWords = new List <string>();

                    equation += value;
                }
                else
                {
                    previousWords.Add(value);

                    bool keywordMatched = false;

                    for (int i = 0; i < previousWords.Count; i++)
                    {
                        if (previousWords[i] != value)
                        {
                            previousWords[i] += $" {value}";
                        }

                        (bool isKeyword, string operation, string matchedKeyword)keyword = IsMathsKeyword(ref commandWords, previousWords[i]);

                        if (keyword.isKeyword)
                        {
                            keywordMatched = true;

                            awaitingMatch = false;

                            string operandIdentifier = keyword.operation.Split('_')[2];

                            dualOperator = operandIdentifier == "1" || operandIdentifier == "2";

                            previousWasNumber = false;

                            string firstWord = keyword.matchedKeyword.Split(' ')[0];

                            if (keywordFirstWord != firstWord)
                            {
                                equation += $"{symbolToAppend}";
                            }

                            symbolToAppend = $"{(equation != "" ? " " : "")}{keyword.operation}";

                            keywordFirstWord = firstWord;

                            break;
                        }
                    }

                    if (!keywordMatched)
                    {
                        awaitingMatch = true;
                    }
                }
            }

            if (awaitingMatch)
            {
                return(false, null);
            }

            equation += symbolToAppend;

            if (debug)
            {
                GUIController.LogOutput($"Equation: '{equation}'");
            }

            return(true, equation);
        }