-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4694ec4
commit 81ff960
Showing
12 changed files
with
243 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Opw.PineBlog.Entities; | ||
using System.Collections.Generic; | ||
|
||
namespace Opw.PineBlog.Posts.Search | ||
{ | ||
public interface IPostRanker | ||
{ | ||
IEnumerable<Post> Rank(IEnumerable<Post> posts, string query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Opw.PineBlog.Entities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Opw.PineBlog.Posts.Search | ||
{ | ||
// TODO: add more test for ranking (hits count) | ||
public class PostRanker : IPostRanker | ||
{ | ||
public IEnumerable<Post> Rank(IEnumerable<Post> posts, string query) | ||
{ | ||
if (posts == null || !posts.Any()) | ||
{ | ||
return new List<Post>(); | ||
} | ||
|
||
var terms = query.ParseTerms(); | ||
var rankedPosts = new List<Tuple<Post, int>>(); | ||
|
||
foreach (var post in posts) | ||
{ | ||
var rank = 0; | ||
foreach (var term in terms) | ||
{ | ||
int hits; | ||
if (!string.IsNullOrWhiteSpace(post.Title) && post.Title.ToLower().Contains(term)) | ||
{ | ||
hits = Regex.Matches(post.Title.ToLower(), term).Count; | ||
rank += hits * 10; | ||
} | ||
if (!string.IsNullOrWhiteSpace(post.Categories) && post.Categories.ToLower().Contains(term)) | ||
{ | ||
hits = Regex.Matches(post.Categories.ToLower(), term).Count; | ||
rank += hits * 10; | ||
} | ||
if (!string.IsNullOrWhiteSpace(post.Description) && post.Description.ToLower().Contains(term)) | ||
{ | ||
hits = Regex.Matches(post.Description.ToLower(), term).Count; | ||
rank += hits * 3; | ||
} | ||
if (!string.IsNullOrWhiteSpace(post.Content) && post.Content.ToLower().Contains(term)) | ||
{ | ||
hits = Regex.Matches(post.Content.ToLower(), term).Count; | ||
rank += hits * 1; | ||
} | ||
} | ||
|
||
rankedPosts.Add(new Tuple<Post, int>(post, rank)); | ||
} | ||
|
||
return rankedPosts.OrderByDescending(t => t.Item2).Select(t => t.Item1).ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Opw.PineBlog.Core/Posts/Search/SearchQueryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Opw.PineBlog.Posts.Search | ||
{ | ||
public static class SearchQueryExtensions | ||
{ | ||
public static IEnumerable<string> ParseTerms(this string query) | ||
{ | ||
if (string.IsNullOrEmpty(query)) | ||
{ | ||
return Array.Empty<string>(); | ||
} | ||
|
||
// convert multiple spaces into one space | ||
query = Regex.Replace(query, @"\s+", " ").Trim(); | ||
return query.ToLower().Split(' ').ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/Opw.PineBlog.Core.Tests/Posts/Search/PostRankerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using FluentAssertions; | ||
using Opw.PineBlog.Entities; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Opw.PineBlog.Posts.Search | ||
{ | ||
public class PostRankerTests | ||
{ | ||
[Fact] | ||
public void Rank_Should_0Posts_WhenPostsNull() | ||
{ | ||
var query = "c# dotnet pineblog"; | ||
|
||
var results = new PostRanker().Rank(null, query); | ||
|
||
results.Should().HaveCount(0); | ||
} | ||
|
||
[Fact] | ||
public void Rank_Should_2Posts_WhenQueryNull() | ||
{ | ||
var posts = new List<Post> | ||
{ | ||
new Post { Slug = "1", Title = "pineblog", Categories = "pineblog", Description = "pineblog", Content = "pinelog" }, | ||
new Post { Slug = "2", Title = "pineblog", Categories = "pineblog", Description = "pineblog", Content = "pinelog" }, | ||
}; | ||
|
||
var results = new PostRanker().Rank(posts, null); | ||
|
||
results.Should().HaveCount(2); | ||
} | ||
|
||
[Fact] | ||
public void Rank_Should_2Posts_WhenPostProperiesNull() | ||
{ | ||
var query = "c# dotnet pineblog"; | ||
var posts = new List<Post> | ||
{ | ||
new Post { Slug = "1", Title = "pineblog", Categories = "pineblog", Description = "pineblog", Content = "pinelog" }, | ||
new Post { Slug = "2", Title = null, Categories = null, Description = null, Content = null }, | ||
}; | ||
|
||
var results = new PostRanker().Rank(posts, query); | ||
|
||
results.Should().HaveCount(2); | ||
} | ||
|
||
[Fact] | ||
public void Rank_Should_PostsRankedCorrectly_ForOneMatchingTerm() | ||
{ | ||
var query = "c# dotnet pineblog"; | ||
var posts = new List<Post> | ||
{ | ||
new Post { Slug = "5", Title = "xxx", Categories = "xxx", Description = "xxx", Content = "xxx" }, | ||
new Post { Slug = "4", Title = "pineblog", Categories = "xxx", Description = "xxx", Content = "xxx" }, | ||
new Post { Slug = "3", Title = "pineblog", Categories = "pineblog", Description = "xxx", Content = "xxx" }, | ||
new Post { Slug = "2", Title = "pineblog", Categories = "pineblog", Description = "pineblog", Content = "xxx" }, | ||
new Post { Slug = "1", Title = "pineblog", Categories = "pineblog", Description = "pineblog", Content = "pinelog" }, | ||
}; | ||
|
||
var results = new PostRanker().Rank(posts, query); | ||
|
||
results.Should().HaveCount(5); | ||
results.Select(p => p.Slug).Should().BeEquivalentTo(new string[] { "1", "2", "3", "4", "5" }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/Opw.PineBlog.Core.Tests/Posts/Search/SearchQueryExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Opw.PineBlog.Posts.Search | ||
{ | ||
public class SearchQueryExtensionsTests | ||
{ | ||
[Fact] | ||
public void ParseTerms_Should_ConvertMultipleSpaceIntoOne_ForQueryNull() | ||
{ | ||
string query = null; | ||
|
||
var result = query.ParseTerms(); | ||
|
||
result.Should().HaveCount(0); | ||
} | ||
|
||
[Fact] | ||
public void ParseTerms_Should_ConvertMultipleSpaceIntoOne_ForQuery() | ||
{ | ||
var query = "c# dotnet pineblog "; | ||
|
||
var result = query.ParseTerms(); | ||
|
||
result.Should().HaveCount(3); | ||
result.Should().BeEquivalentTo(new string[] { "c#", "dotnet", "pineblog" }); | ||
} | ||
|
||
[Fact] | ||
public void ParseTerms_Should_ToLower_ForQuery() | ||
{ | ||
var query = " C# DOTNET pineblog "; | ||
|
||
var result = query.ParseTerms(); | ||
|
||
result.Should().HaveCount(3); | ||
result.Should().BeEquivalentTo(new string[] { "c#", "dotnet", "pineblog" }); | ||
} | ||
|
||
[Fact] | ||
public void ParseTerms_Should_3Terms_ForQuery() | ||
{ | ||
var query = " C# DOTNET pineblog "; | ||
|
||
var result = query.ParseTerms(); | ||
|
||
result.Should().HaveCount(3); | ||
result.Should().BeEquivalentTo(new string[] { "c#", "dotnet", "pineblog" }); | ||
} | ||
} | ||
} |
Oops, something went wrong.