Exemple #1
0
        public override Sentence Parse(string sentence, ParseTableReader reader)
        {
            original = sentence;
            string[] tokenStrings = sentence.Split(new char[] { ' ' }).Where(s => s != "").ToArray <string>();

            List <Token> parsedTokens = new List <Token>();

            type = SentenceTypes.CommodityDefinition;
            for (int i = 0; i < tokenStrings.Length; i++)
            {
                int result;
                if (reader.GetTranslationForAlienDigit(tokenStrings[i].Trim().ToLower()) != Char.MinValue)
                {
                    if (i == 0 || parsedTokens.Last().Type == TokenTypes.AlienDigit)
                    {
                        parsedTokens.Add(new Token(tokenStrings[i], TokenTypes.AlienDigit));
                        alienDigits += reader.GetTranslationForAlienDigit(tokenStrings[i]);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }

                else if (tokenStrings[i].Equals("is", StringComparison.CurrentCultureIgnoreCase))
                {
                    parsedTokens.Add(new Token(tokenStrings[i], TokenTypes.Equality));
                }
                else if (tokenStrings[i].Equals("Credits", StringComparison.CurrentCultureIgnoreCase))
                {
                    parsedTokens.Add(new Token(tokenStrings[i], TokenTypes.Unit));
                }
                else if (int.TryParse(tokenStrings[i], out result))
                {
                    if (parsedTokens.Last().Type == TokenTypes.Equality && RomanNumber.Parse(alienDigits) > 0)
                    {
                        parsedTokens.Add(new Token(tokenStrings[i], TokenTypes.UnitPrice));
                        unitPrice = float.Parse(tokenStrings[i]) / RomanNumber.Parse(alienDigits);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                else if (parsedTokens.Last()?.Type == TokenTypes.AlienDigit)
                {
                    parsedTokens.Add(new Token(tokenStrings[i], TokenTypes.Commodity));
                    commodityName = tokenStrings[i];
                }
            }
            return(this);
        }
Exemple #2
0
        public override Sentence Parse(string sentence, ParseTableReader reader)
        {
            original = sentence;
            string pattern        = @"(how much|how many credits|how many Credits) (is)";
            string pattern2       = @"(\w+)+";
            var    tokens1        = Regex.Split(sentence, pattern).Where(s => s != string.Empty).ToList <string>();
            string combinedTokens = tokens1.Last();

            string[]     tokenStrings = Regex.Split(combinedTokens, pattern2).Where(s => s.Trim() != string.Empty && s.Trim() != "?").ToArray <string>();
            List <Token> tokens       = new List <Token>();

            for (int i = 0; i < tokenStrings.Length; i++)
            {
                if (reader.GetTranslationForAlienDigit(tokenStrings[i]) != Char.MinValue)
                {
                    if (i == 0 || tokens.Last().Type == Enums.TokenTypes.AlienDigit)
                    {
                        tokens.Add(new Token(tokenStrings[i], Enums.TokenTypes.AlienDigit));
                        alienDigits += reader.GetTranslationForAlienDigit(tokenStrings[i]);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                else if (reader.IsValidCommodity(tokenStrings[i]))
                {
                    if (tokens.Last().Type == Enums.TokenTypes.AlienDigit && RomanNumber.Parse(alienDigits) > 0)
                    {
                        if (i == tokenStrings.Length - 1) //if commodity definition, then it has to be the last
                        {
                            commodityName = tokenStrings[i];
                            type          = Enums.SentenceTypes.CommodityValueQuery;
                            tokens.Add(new Token(commodityName, Enums.TokenTypes.AlienDigit));
                        }
                        else
                        {
                            throw new FormatException();
                        }
                    }
                }
                else
                {
                    throw new FormatException();
                }
                if (i == tokens.Count - 1)
                {
                    RomanNumber.Parse(alienDigits); //would throw exception if alien digits dont obey the roman number rules
                }
            }
            return(this);
        }
 /// <summary>
 /// evaluate a parsed query statement
 /// </summary>
 /// <param name="query"></param>
 /// <returns></returns>
 public int Evaluate(Sentence query)
 {
     if (query.Type == SentenceTypes.CommodityValueQuery)
     {
         var   t             = (query as Query).GetCommodityQueryTerms();
         int   numberOfItems = RomanNumber.Parse(t.Item1);
         float unitPrice     = parseTables.GetReader().GetCommodityUnitPrice(t.Item2);
         return((int)(numberOfItems * unitPrice));
     }
     else if (query.Type == SentenceTypes.RomanNumberTranslationQuery)
     {
         var t = (query as Query).GetAlienDigitsForQuery();
         return(RomanNumber.Parse(t));
     }
     else
     {
         throw new ArgumentException("object of type query is expected");
     }
 }