private static void DisplayToken(IToken token) { var score = TokenWeightResolver.Invoke(token); var tokenInfo = $"{token.GetType().Name}"; var valueInfo = "[value = {0}]"; var scoreInfo = $"[score = {score}]"; valueInfo = token switch { LiteralToken literalToken => string.Format(valueInfo, $"'{literalToken.Value}'"), LiteralAmountToken literalAmountToken => string.Format(valueInfo, literalAmountToken.Amount), FractionalAmountToken fractionalAmountToken => string.Format(valueInfo, GetRepresentation(fractionalAmountToken)), RangeAmountToken rangeAmountToken => string.Format(valueInfo, GetRepresentation(rangeAmountToken)), UnitToken unitToken => string.Format(valueInfo, unitToken.Unit), FormToken formToken => string.Format(valueInfo, formToken.Form), IngredientToken ingredientToken => string.Format(valueInfo, ingredientToken.Ingredient), _ => string.Empty }; var output = $"-> {tokenInfo}\n\t{valueInfo}\n\t{scoreInfo}"; Console.WriteLine(output); }
private IEnumerable <Token> RecognizeAmountUnitTokens(IEnumerable <Token> tokens, int start = 0) { if (start >= tokens.Count()) { return(tokens); } AmountToken amount = null; UnitToken unit = null; var first = tokens.Count() - 1; var i = start; for (; i < tokens.Count(); i++) { var token = tokens.ElementAt(i); if (amount == null) { if (token.Type == TokenType.Number && !(token is AmountUnitToken)) { first = i; if (token is AmountToken) { amount = (AmountToken)token; } else { amount = new AmountToken(token); } } } else { if (token.Type == TokenType.Symbol && token.Value == "-") { continue; } if (token.Type == TokenType.Unit) { unit = (UnitToken)token; } break; } if (token.Type == TokenType.Unit) { if (amount == null) { first = i; } unit = (UnitToken)token; break; } } List <Token> adjusted = null; if (amount != null) { adjusted = tokens.Take(first).ToList(); adjusted.Add(new AmountUnitToken( amount.Amount, unit, (unit == null ? new Token[] { amount } : new Token[] { amount, unit }))); adjusted.AddRange(tokens.Skip(first + 1 + (unit == null ? 0 : i - first))); } else if (unit != null) { adjusted = tokens.Take(first).ToList(); adjusted.Add(new AmountUnitToken( 1f, unit, new Token[] { unit })); adjusted.AddRange(tokens.Skip(first + 1)); } return(RecognizeAmountUnitTokens(adjusted ?? tokens, first + 1)); }
public AmountUnitToken(float amount, UnitToken unit, params Token[] tokens) : base(amount, tokens) { Unit = unit; }