-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
DocumentValuator.java
166 lines (133 loc) · 5.07 KB
/
DocumentValuator.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package nu.marginalia.converting.processor.logic;
import crawlercommons.utils.Strings;
import nu.marginalia.crawling.model.CrawledDocument;
import nu.marginalia.model.html.HtmlStandard;
import nu.marginalia.converting.model.DisqualifiedException;
import nu.marginalia.model.crawl.HtmlFeature;
import org.jetbrains.annotations.NotNull;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.NodeVisitor;
import java.util.List;
import java.util.Set;
public class DocumentValuator {
public double getQuality(CrawledDocument crawledDocument,
HtmlStandard htmlStandard,
Document parsedDocument,
int textLength) throws DisqualifiedException {
double scriptPenalty = getScriptPenalty(parsedDocument);
double chatGptPenalty = getChatGptContentFarmPenalty(parsedDocument);
int rawLength = crawledDocument.documentBody.length();
if (textLength == 0) {
throw new DisqualifiedException(DisqualifiedException.DisqualificationReason.LENGTH);
}
return Math.log(textLength / (double) (1+rawLength))*htmlStandard.scale
+ htmlStandard.offset
- scriptPenalty
- chatGptPenalty;
}
private double getChatGptContentFarmPenalty(Document parsedDocument) {
// easily 90% of modern AI-authored content farm spam has these nonsense headers
boolean benefitsOf = false, keyBenefits = false, keyTakeaways = false;
outer:
for (String tagName : List.of("h1", "h2", "h3")) {
for (var elem : parsedDocument.getElementsByTag(tagName)) {
if (benefitsOf && keyBenefits && keyTakeaways)
break outer;
String text = elem.text().toLowerCase();
benefitsOf = benefitsOf || text.startsWith("benefits of");
keyBenefits = keyBenefits || text.startsWith("key benefits");
keyTakeaways = keyTakeaways || text.startsWith("key takeaways");
}
}
double penalty = 0;
if (benefitsOf) penalty += 10;
if (keyBenefits) penalty += 5;
if (keyTakeaways) penalty += 5;
return penalty;
}
private int getScriptPenalty(Document parsed) {
var scriptVisitor = new ScriptVisitor();
parsed.getElementsByTag("script").traverse(scriptVisitor);
int value = scriptVisitor.score();
for (var links : parsed.head().getElementsByTag("link")) {
if (links.hasAttr("onerror") || links.hasAttr("onload")) {
value += 1;
}
}
return value;
}
public double adjustQuality(double quality, Set<HtmlFeature> features) {
double adjustment = 0;
if (features.contains(HtmlFeature.TRACKING_ADTECH)) {
adjustment -= 2.5;
}
if (features.contains(HtmlFeature.TRACKING)) {
adjustment -= 2.5;
}
if (features.contains(HtmlFeature.AFFILIATE_LINK)) {
adjustment -= 1.5;
}
if (features.contains(HtmlFeature.GA_SPAM)) {
adjustment -= 1;
}
if (features.contains(HtmlFeature.COOKIES)) {
adjustment -= 1;
}
if (features.contains(HtmlFeature.KEBAB_CASE_URL)) {
adjustment -= 2;
}
if (features.contains(HtmlFeature.COOKIELAW)) {
adjustment -= 1;
}
if (features.contains(HtmlFeature.PARDOT)) {
adjustment -= 1;
}
if (features.contains(HtmlFeature.QUANTCAST)) {
adjustment -= 1;
}
if (features.contains(HtmlFeature.WEBMENTION)) {
adjustment += 1;
}
if (features.contains(HtmlFeature.INDIEAUTH)) {
adjustment += 1;
}
if (quality + adjustment > 0) {
return 0;
}
if (quality + adjustment < -15) {
return -15;
}
return quality + adjustment;
}
public static class ScriptVisitor implements NodeVisitor {
boolean hasBadScript = false;
int scriptLength = 0;
double penalty = 0.;
public int score() {
return (int)(penalty + (hasBadScript?1:0) + (scriptLength)/1000.);
}
@Override
public void head(@NotNull Node node, int depth) {
if (node instanceof Element el) {
visitTag(el);
}
}
public void visitTag(Element el) {
String srcAttr = el.attr("src");
if (srcAttr.contains("wp-content") || srcAttr.contains("wp-includes") || srcAttr.contains("jquery")) {
penalty += 0.49;
} else if (!Strings.isBlank(srcAttr)) {
penalty += 1;
} else {
var wt = el.wholeText();
scriptLength += wt.length();
penalty += 0.25;
if (!hasBadScript) {
hasBadScript = wt.contains(".createElement(");
}
}
}
}
}