Skip to content

Commit

Permalink
Temporarily remove Left_Weighted functionality
Browse files Browse the repository at this point in the history
This enables the release of the new Levenshtein search features without
exposing incomplete methods for left weighted searching
  • Loading branch information
ninjanye committed Nov 1, 2016
1 parent 5022896 commit ca33ddd
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,5 @@ public void ToRanked_SearchedForData_RankedEndsWithSearch()
//Assert
Assert.AreEqual(14, result.Count());
}

[Test]
public void ToRanked_SearchedForData_LeftWeightedRankedResults()
{
//Arrange

//Act
var result = _context.TestModels.Search(x => x.StringOne)
.Containing("wordmatch")
.ToRanked(RankedType.LeftWeighted);

//Assert
Assert.AreEqual(10, result.First().Hits);
}
}
}
4 changes: 2 additions & 2 deletions NinjaNye.SearchExtensions.Tests/Fluent/FluentRankedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void ToRanked_SearchedForData_RankedResultIsReturned()
[Test]
public void ToRanked_CorrectRankReturned()
{
var result = _testData.Search(x => x.Name).ContainingAll("wee").ToRanked(RankedType.LeftWeighted);
var result = _testData.Search(x => x.Name).ContainingAll("wee").ToRanked();
var first = result.OrderByDescending(r => r.Hits).First();
//as 'wee' is one char into string, it should add (7 - 1) to the hit count. - should add 6
Assert.AreEqual(7, first.Hits);

result = _testData.Search(x => x.Name).ContainingAll("ete").ToRanked(RankedType.LeftWeighted);
result = _testData.Search(x => x.Name).ContainingAll("ete").ToRanked();
first = result.OrderByDescending(r => r.Hits).First();

//as 'ete' is three char into string, it should add (7 - 3) to the hit count. - should add 4
Expand Down
5 changes: 2 additions & 3 deletions NinjaNye.SearchExtensions/EnumerableStringSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public IEnumerable<T> ReverseSoundex(params string[] terms)
/// Enumerable of ranked items. Each item will contain
/// the amount of hits found across the defined properties
/// </returns>
public IEnumerable<IRanked<T>> ToRanked(RankedType type = RankedType.Default)
public IEnumerable<IRanked<T>> ToRanked()
{
Expression combinedHitExpression = null;
foreach (var propertyToSearch in Properties)
Expand All @@ -253,8 +253,7 @@ public IEnumerable<IRanked<T>> ToRanked(RankedType type = RankedType.Default)
{
var searchTerm = _searchTerms[j];
var nullSafeExpresion = BuildNullSafeExpresion(propertyToSearch);
var hitCountExpression = type == RankedType.Default ? EnumerableExpressionHelper.CalculateHitCount(nullSafeExpresion, searchTerm, _searchOptions)
: EnumerableExpressionHelper.CalculateHitCount_LeftWeighted(nullSafeExpresion, searchTerm, _searchOptions);
var hitCountExpression = EnumerableExpressionHelper.CalculateHitCount(nullSafeExpresion, searchTerm, _searchOptions);
combinedHitExpression = ExpressionHelper.AddExpressions(combinedHitExpression, hitCountExpression);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,6 @@ public static Expression CalculateHitCount<T>(Expression<Func<T, string>> string
return hitCountExpression;
}

/// <summary>
/// Calculates how many search hits occured for a given property - artificially weighted for results at start of
/// </summary>
/// <param name="stringProperty">string property to analyse</param>
/// <param name="searchTerm">search term to count</param>
/// <returns>Expression equivalent to: [property].Length - ([property].Replace([searchTerm], "").Length) / [searchTerm].Length</returns>
public static Expression CalculateHitCount_LeftWeighted<T>(Expression<Func<T, string>> stringProperty, string searchTerm)
{
var hitCountExpression = CalculateHitCount(stringProperty, searchTerm);

Expression searchTermExpression = Expression.Constant(searchTerm);
var coalesceExpression = Expression.Coalesce(stringProperty.Body, ExpressionMethods.EmptyStringExpression);
var indexOfExpresion = Expression.Call(coalesceExpression, ExpressionMethods.IndexOfMethod, searchTermExpression);
var equalityCheckExpression = Expression.Equal(indexOfExpresion, Expression.Constant(-1));
MemberExpression lengthExpression = Expression.Property(stringProperty.Body, ExpressionMethods.StringLengthProperty);
var leftWeightExpressionWithValue = Expression.Subtract(lengthExpression, indexOfExpresion);
var leftWeightExpressionWithNoValue = Expression.Constant(0);

var leftWeightExpression = Expression.Condition(equalityCheckExpression, leftWeightExpressionWithNoValue, leftWeightExpressionWithValue);

var finalHitCounterExpressionOffset = Expression.Add(hitCountExpression, leftWeightExpression);
return finalHitCounterExpressionOffset;
}

/// <summary>
/// Calculates how many search hits occured for a given property
/// </summary>
Expand All @@ -103,31 +79,6 @@ public static Expression CalculateHitCount<T>(Expression<Func<T, string>> string
return hitCountExpression;
}

/// <summary>
/// Calculates how many search hits occured for a given property
/// </summary>
/// <returns>Expression equivalent to: [property].Length - ([property].Replace([searchTerm], "").Length) / [searchTerm].Length</returns>
public static Expression CalculateHitCount_LeftWeighted<T>(Expression<Func<T, string>> stringProperty, string searchTerm, SearchOptions searchOptions)
{
Expression searchTermExpression = Expression.Constant(searchTerm);
Expression searchTermLengthExpression = Expression.Constant(searchTerm.Length);
MemberExpression lengthExpression = Expression.Property(stringProperty.Body, ExpressionMethods.StringLengthProperty);

var replaceExpression = Expression.Call(ExpressionMethods.CustomReplaceMethod, stringProperty.Body, searchTermExpression, ExpressionMethods.EmptyStringExpression, searchOptions.ComparisonTypeExpression);
var replacedLengthExpression = Expression.Property(replaceExpression, ExpressionMethods.StringLengthProperty);

var characterDiffExpression = Expression.Subtract(lengthExpression, replacedLengthExpression);
var hitCountExpression = Expression.Divide(characterDiffExpression, searchTermLengthExpression);

var coalesceExpression = Expression.Coalesce(stringProperty.Body, ExpressionMethods.EmptyStringExpression);
var indexOfExpresion = Expression.Call(coalesceExpression, ExpressionMethods.IndexOfMethod, searchTermExpression);
var leftWeightExpression = Expression.Subtract(lengthExpression, indexOfExpresion);

var finalHitCounterExpressionOffset = Expression.Add(hitCountExpression, leftWeightExpression);
return finalHitCounterExpressionOffset;
}


/// <summary>
/// Calculates the Levenshtein distance between a given property and a search term
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion NinjaNye.SearchExtensions/NinjaNye.SearchExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QueryableSearchBase.cs" />
<Compile Include="QueryableChildSelector.cs" />
<Compile Include="RankedType.cs" />
<Compile Include="SearchBase.cs" />
<Compile Include="SearchOptions.cs" />
<Compile Include="SearchTermCollection.cs" />
Expand Down
5 changes: 2 additions & 3 deletions NinjaNye.SearchExtensions/QueryableStringSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public IEnumerable<T> Soundex(params string[] terms)
/// the amount of hits found across the defined properties.
/// </returns>
/// <remarks>Only works in conjunction with string searches</remarks>
public IQueryable<IRanked<T>> ToRanked(RankedType type = RankedType.Default)
public IQueryable<IRanked<T>> ToRanked()
{
Expression combinedHitExpression = null;
foreach (var propertyToSearch in Properties)
Expand All @@ -258,8 +258,7 @@ public IQueryable<IRanked<T>> ToRanked(RankedType type = RankedType.Default)
for (int j = 0; j < _searchTerms.Count; j++)
{
var searchTerm = _searchTerms[j];
var hitCountExpression = type == RankedType.Default ? EnumerableExpressionHelper.CalculateHitCount(nullSafeExpression, searchTerm)
: EnumerableExpressionHelper.CalculateHitCount_LeftWeighted(nullSafeExpression, searchTerm);
var hitCountExpression = EnumerableExpressionHelper.CalculateHitCount(nullSafeExpression, searchTerm);
combinedHitExpression = ExpressionHelper.AddExpressions(combinedHitExpression, hitCountExpression);
}
}
Expand Down
8 changes: 0 additions & 8 deletions NinjaNye.SearchExtensions/RankedType.cs

This file was deleted.

0 comments on commit ca33ddd

Please sign in to comment.