/
Cleanser.cs
134 lines (113 loc) · 4.29 KB
/
Cleanser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using AngleSharp;
using AngleSharp.Dom;
using System.Text.RegularExpressions;
namespace IndexCreator
{
public abstract class CleanserBase
{
public abstract (string, string) Cleanse(string content, string fileName = "");
public static string CleanupContent(string content)
{
var output = content;
output = Regex.Replace(output, @"[^\S\n]{2,}", " ", RegexOptions.Multiline);
output = Regex.Replace(output, @"-{2,}", "--", RegexOptions.Multiline);
output = Regex.Replace(output, @"^\s$\n", "\n", RegexOptions.Multiline);
output = Regex.Replace(output, @"\n{2,}", "\n", RegexOptions.Multiline);
return output.Trim();
}
string GetFirstLineWithProperty(string content, string property = "title: ")
{
foreach (string line in content.Split(Environment.NewLine))
{
if (line.StartsWith(property))
return line.Substring(property.Length).Trim();
}
return string.Empty;
}
string GetFirstAlphanumLine(string content)
{
foreach (string line in content.Split(Environment.NewLine))
{
if (line.Any(char.IsLetterOrDigit))
return line.Trim();
}
return string.Empty;
}
}
public class TextCleanser : CleanserBase
{
public TextCleanser() : base() { }
string GetFirstAlphanumLine(string content)
{
foreach (string line in content.Split(Environment.NewLine))
{
if (line.Any(c => Char.IsLetterOrDigit(c)))
return line.Trim();
}
return string.Empty;
}
string GetFirstLineWithProperty(string content, string property = "title: ")
{
foreach (string line in content.Split(Environment.NewLine))
{
if (line.StartsWith(property))
return line.Substring(property.Length).Trim();
}
return string.Empty;
}
public override (string, string) Cleanse(string content, string fileName = "")
{
string title = GetFirstLineWithProperty(content) ?? GetFirstAlphanumLine(content);
return (CleanupContent(content), title ?? fileName);
}
}
public class HtmlCleanser : CleanserBase
{
static readonly int titleMaxTokens = 128;
TokenEstimator tokenEstimator;
public HtmlCleanser() : base()
{
tokenEstimator = new TokenEstimator();
}
public override (string, string) Cleanse(string content, string fileName = "")
{
var title = ExtractTitle(content, fileName);
return (CleanupContent(content), title);
}
string ExtractTitle(string content, string fileName)
{
var context = BrowsingContext.New(Configuration.Default);
var document = context.OpenAsync(req => req.Content(content)).Result;
var titleElement = document.QuerySelector("title");
if (titleElement != null)
{
return titleElement.TextContent;
}
var h1Element = document.QuerySelector("h1");
if (h1Element != null)
{
return h1Element.TextContent;
}
var h2Element = document.QuerySelector("h2");
if (h2Element != null)
{
return h2Element.TextContent;
}
//if title is still not found, guess using the next string
var title = GetNextStrippedString(document);
title = tokenEstimator.ConstructTokensWithSize(title, titleMaxTokens);
if (string.IsNullOrEmpty(title))
title = fileName;
return title;
}
public string GetNextStrippedString(IDocument document)
{
var allTextNodes = document.All
.Where(n => n.NodeType == NodeType.Text && n.TextContent.Trim() != "")
.Select(n => n.TextContent.Trim());
// Get the first non-empty string
var nextStrippedString = allTextNodes.FirstOrDefault() ?? "";
return nextStrippedString;
}
}
}