add string match comparison for NextPage

This commit is contained in:
2014-02-17 12:23:59 +01:00
parent 665b3eb9fc
commit 526abc27f5
2 changed files with 48 additions and 3 deletions
@@ -542,6 +542,14 @@ namespace ReadSharp.Ports.NReadability
linkObj.Score -= 200;
}
double urlComparity = StringCompare(url, linkHref);
/* If the next page differs from the original too much */
if (StringCompare(url, linkHref) < 80)
{
linkObj.Score -= 200;
}
/* If any ancestor node contains page or paging or paginat */
XElement parentNode = linkElement.Parent;
bool positiveNodeMatch = false;
@@ -654,6 +662,37 @@ namespace ReadSharp.Ports.NReadability
return null;
}
internal static double StringCompare(string a, string b)
{
// Same string, no iteration needed.
if (a == b)
{
return 100;
}
// One is empty, second is not
if ((a.Length == 0) || (b.Length == 0))
{
return 0;
}
double maxLen = a.Length > b.Length ? a.Length : b.Length;
int minLen = a.Length < b.Length ? a.Length : b.Length;
int sameCharAtIndex = 0;
// Compare char by char
for (int i = 0; i < minLen; i++)
{
if (a[i] == b[i])
{
sameCharAtIndex++;
}
}
return sameCharAtIndex / maxLen * 100;
}
/// <summary>
/// Find a cleaned up version of the current URL, to use for comparing links for possible next-pageyness.
/// </summary>
+9 -3
View File
@@ -149,9 +149,6 @@ namespace ReadSharp.Tests
result = await reader.Read(new Uri("http://www.sueddeutsche.de/wirtschaft/netzbetreiber-und-die-energiewende-im-kampf-gegen-blackouts-und-buergerproteste-1.1880754"));
Assert.Equal(result.NextPage.ToString(), "http://www.sueddeutsche.de/wirtschaft/netzbetreiber-und-die-energiewende-im-kampf-gegen-blackouts-und-buergerproteste-1.1880754-2");
result = await reader.Read(new Uri("http://www.pcwelt.de/ratgeber/Acht_Tipps_fuer_die_sichere_Cloud-Google_Drive__Microsoft_Skydrive__Teamdrive-8205869.html?redirect=1"));
Assert.Equal(result.NextPage.ToString(), "http://www.pcwelt.de/ratgeber/Google_Drive__Microsoft_Skydrive__Teamdrive-Tipp_3_-_Server-Standort_beachten-8205887.html");
result = await reader.Read(new Uri("http://arstechnica.com/apple/2014/01/two-steps-forward-a-review-of-the-2013-mac-pro/"));
Assert.Equal(result.NextPage.ToString(), "http://arstechnica.com/apple/2014/01/two-steps-forward-a-review-of-the-2013-mac-pro/2");
}
@@ -161,6 +158,15 @@ namespace ReadSharp.Tests
{
Article result = await reader.Read(new Uri("http://www.buzzfeed.com/mattlynley/the-16-most-interesting-things-to-come-out-of-bill-gates-qa"));
Assert.Null(result.NextPage);
result = await reader.Read(new Uri("http://arstechnica.com/apple/2014/01/two-steps-forward-a-review-of-the-2013-mac-pro/7/"));
Assert.Null(result.NextPage);
result = await reader.Read(new Uri("http://www.sueddeutsche.de/wirtschaft/netzbetreiber-und-die-energiewende-im-kampf-gegen-blackouts-und-buergerproteste-1.1880754-2"));
Assert.Null(result.NextPage);
result = await reader.Read(new Uri("http://www.zeit.de/gesellschaft/2014-02/alice-schwarzer-steuerhinterziehung-doppelmoral/seite-2"));
Assert.Null(result.NextPage);
}
[Fact]