add word count, plain content and contentExtracted bool.

This commit is contained in:
2014-01-30 11:14:14 +01:00
parent 70b3b1c9b9
commit 1b91582025
6 changed files with 146 additions and 0 deletions
+10
View File
@@ -99,6 +99,16 @@ namespace ReadSharp.Tests
}
[Fact]
public async Task ArePropertiesCorrectlyAssignedWithEmptyArticle()
{
Article result = await reader.Read(new Uri("https://docs.google.com/presentation/d/1n4NyG4uPRjAA8zn_pSQ_Ket0RhcWC6QlZ6LMjKeECo0/preview?sle=true#slide=id.g178014302_016"));
Assert.False(result.ContentExtracted);
Assert.True(result.WordCount == 7);
Assert.Equal(result.Title, result.PlainContent);
}
[Fact]
public async Task TestCzechCharsets()
{
+90
View File
@@ -0,0 +1,90 @@
using HtmlAgilityPack;
using System;
using System.IO;
namespace ReadSharp
{
internal class HtmlUtilities
{
public static string ConvertToPlainText(string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
StringWriter sw = new StringWriter();
ConvertTo(doc.DocumentNode, sw);
sw.Flush();
return sw.ToString();
}
public static string Cut(string text, int length)
{
if (!String.IsNullOrEmpty(text) && text.Length > length)
{
text = text.Substring(0, length - 4) + " ...";
}
return text;
}
private static void ConvertContentTo(HtmlNode node, TextWriter outText)
{
foreach (HtmlNode subnode in node.ChildNodes)
{
ConvertTo(subnode, outText);
}
}
private static void ConvertTo(HtmlNode node, TextWriter outText)
{
string html;
switch (node.NodeType)
{
case HtmlNodeType.Comment:
// don't output comments
break;
case HtmlNodeType.Document:
ConvertContentTo(node, outText);
break;
case HtmlNodeType.Text:
// script and style must not be output
string parentName = node.ParentNode.Name;
if ((parentName == "script") || (parentName == "style"))
break;
// get text
html = ((HtmlTextNode)node).Text;
// is it in fact a special closing node output as text?
if (HtmlNode.IsOverlappedClosingElement(html))
break;
// check the text is meaningful and not a bunch of whitespaces
if (html.Trim().Length > 0)
{
outText.Write(HtmlEntity.DeEntitize(html));
}
break;
case HtmlNodeType.Element:
switch (node.Name)
{
case "p":
// treat paragraphs as crlf
outText.Write("\r\n");
break;
}
if (node.HasChildNodes)
{
ConvertContentTo(node, outText);
}
break;
}
}
}
}
+24
View File
@@ -33,6 +33,30 @@ namespace ReadSharp
/// </value>
public string Content { get; set; }
/// <summary>
/// Could the parser extract any contents?
/// </summary>
/// <value>
/// <c>true</c> if [content extracted]; otherwise, <c>false</c>.
/// </value>
public bool ContentExtracted { get; set; }
/// <summary>
/// Plain content without HTML tags.
/// </summary>
/// <value>
/// The plain content.
/// </value>
public string PlainContent { get; set; }
/// <summary>
/// Gets or sets the word count (based on the PlainContent).
/// </summary>
/// <value>
/// The word count.
/// </value>
public int WordCount { get; set; }
/// <summary>
/// Gets or sets the front image.
/// </summary>
+4
View File
@@ -69,8 +69,12 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Models\Response.cs" />
<Compile Include="ReadException.cs" />
<Compile Include="HtmlUtilities.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack-PCL">
<HintPath>..\packages\HtmlAgilityPack-PCL.1.4.6\lib\HtmlAgilityPack-PCL.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\portable-net40+sl4+win8+wp71\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
+17
View File
@@ -176,12 +176,29 @@ namespace ReadSharp
};
}).ToList();
// get word count and plain text
string plainContent;
int wordCount = 0;
try
{
plainContent = HtmlUtilities.ConvertToPlainText(transcodingResult.ExtractedContent);
wordCount = !String.IsNullOrEmpty(plainContent) ? plainContent.Split(' ', '\n').Length : 0;
}
catch
{
plainContent = null;
}
// create article
return new Article()
{
Title = transcodingResult.ExtractedTitle,
Description = transcodingResult.ExtractedDescription,
Content = transcodingResult.ExtractedContent,
ContentExtracted = transcodingResult.ContentExtracted ? wordCount > 0 : false,
PlainContent = plainContent,
WordCount = wordCount,
FrontImage = transcodingResult.ExtractedImage,
Images = images,
Favicon = transcodingResult.ExtractedFavicon,
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack-PCL" version="1.4.6" targetFramework="portable-win+net403+sl40+wp71" />
<package id="Microsoft.Bcl" version="1.1.6" targetFramework="portable-win+net403+sl40+wp71" />
<package id="Microsoft.Bcl.Async" version="1.0.165" targetFramework="portable-win+net403+sl40+wp71" />
<package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="portable-win+net403+sl40+wp71" />