public void ParseExpression() { string expression = "(Levenstein * MinLength) + (FirstContains * MinLength * 2) + (Equality * MinLength * 4)"; string parsedExpression = StringComparisonExpression.Create(expression).Print(); Assert.AreEqual(expression, parsedExpression); }
/// <summary> /// Converts String.Compare expression to StringComparisonExpression /// </summary> /// <param name="leftString">String expression on the left side of the comparison</param> /// <param name="rightString">String expression on the right side of the comparison</param> /// <param name="operation">Comparison operation being performed</param> /// <param name="number">Number that String.Compare was being compared to, typically 0, 1, or -1.</param> private Expression ConvertStringCompareExpression(Expression leftString, Expression rightString, ExpressionType operation, int number) { if (number == 1) { if ((operation == ExpressionType.LessThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.LessThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.GreaterThanOrEqual)) { operation = ExpressionType.GreaterThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(System.Linq.Expressions.Expression.Constant(operation == ExpressionType.LessThanOrEqual, typeof(bool))); } } else if (number == -1) { if ((operation == ExpressionType.GreaterThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.GreaterThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.LessThanOrEqual)) { operation = ExpressionType.LessThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(System.Linq.Expressions.Expression.Constant(operation == ExpressionType.GreaterThanOrEqual, typeof(bool))); } } else if (number > 1) { // Always evaluates to true or false regardless of input, so return a constant expression return(System.Linq.Expressions.Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.LessThan) || (operation == ExpressionType.LessThanOrEqual), typeof(bool))); } else if (number < -1) { // Always evaluates to true or false regardless of input, so return a constant expression return(System.Linq.Expressions.Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.GreaterThan) || (operation == ExpressionType.GreaterThanOrEqual), typeof(bool))); } // If number == 0 we just leave operation unchanged return(StringComparisonExpression.Create(operation, leftString, rightString)); }
public void InternalRankWords() { string expression = "(Levenstein * MinLength) + (FirstContains * MinLength * 2) + (Equality * MinLength * 4)"; string word1 = "apple"; string word2 = "maple"; double rank = StringComparisonExpression.Create(expression).RateWordSimilarity(word1, word2); double expectedRank = 3.0d; Assert.AreEqual(expectedRank, rank); }
public void InternalRankSentences() { string expression = "(Levenstein * MinLength) + (FirstContains * MinLength * 2) + (Equality * MinLength * 4)"; string sentence1 = "Pinus torreyana var. insularis"; string sentence2 = "Mt. Atlas mastic tree"; double rank = StringComparisonExpression.Create(expression).RateSentenceSimilarity(sentence1, sentence2); double expectedRank = 3.5000000000000004d; Assert.AreEqual(expectedRank, rank); }
/// <summary> /// Converts String.Compare expression to StringComparisonExpression /// </summary> /// <param name="leftString">String expression on the left side of the comparison</param> /// <param name="rightString">String expression on the right side of the comparison</param> /// <param name="operation">Comparison operation being performed</param> /// <param name="number">Number that String.Compare was being compared to, typically 0, 1, or -1.</param> /// <param name="comparisonExpression">Indicated the comparison type. Most commonly used with case insensitive comparisons</param> private Expression ConvertStringCompareExpression(Expression leftString, Expression rightString, ExpressionType operation, int number, ConstantExpression comparisonExpression) { if (number == 1) { if ((operation == ExpressionType.LessThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.LessThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.GreaterThanOrEqual)) { operation = ExpressionType.GreaterThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant(operation == ExpressionType.LessThanOrEqual, typeof(bool))); } } else if (number == -1) { if ((operation == ExpressionType.GreaterThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.GreaterThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.LessThanOrEqual)) { operation = ExpressionType.LessThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant(operation == ExpressionType.GreaterThanOrEqual, typeof(bool))); } } else if (number > 1) { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.LessThan) || (operation == ExpressionType.LessThanOrEqual), typeof(bool))); } else if (number < -1) { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.GreaterThan) || (operation == ExpressionType.GreaterThanOrEqual), typeof(bool))); } // If number == 0 we just leave operation unchanged StringComparison?comparison = null; if (comparisonExpression != null) { comparison = (StringComparison)comparisonExpression.Value; if (comparison != StringComparison.Ordinal && comparison != StringComparison.OrdinalIgnoreCase) { var msg = string.Format("String comparison option {0} is not supported", comparison); throw new NotSupportedException(msg); } } return(StringComparisonExpression.Create(operation, leftString, rightString, comparison)); }