Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2611: Add line-breaks when parsing HTML block-level elements

Yossi Tamari3712fe8ed9f3
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index cb2d2df50..ccce56b20 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -1388,6 +1388,16 @@
be set in the metadata with this name into the outlink</description>
</property>
+<property>
+ <name>parser.html.line.separators</name>
+ <value>article,aside,blockquote,canvas,dd,div,dl,dt,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hr,li,main,nav,noscript,ol,output,p,pre,section,table,tfoot,ul,video</value>
+ <description>Comma separated list of HTML tags. Newline will be added to the
+ parsed text after these tages.
+ The default list above are the block-level HTML elements.
+ Tags must be in lower case.
+ To disable this feature, leave the list empty.</description>
+</property>
+
<property>
<name>htmlparsefilter.order</name>
<value></value>
diff --git a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java
index 731003c88..95a419add 100644
--- a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java
+++ b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/DOMContentUtils.java
@@ -22,6 +22,8 @@ import java.net.MalformedURLException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.nutch.parse.Outlink;
import org.apache.nutch.util.NodeWalker;
@@ -44,6 +46,7 @@ public class DOMContentUtils {
private String srcTagMetaName;
private boolean keepNodenames;
+ private Set<String> blockNodes;
public static class LinkParams {
public String elName;
@@ -99,6 +102,7 @@ public class DOMContentUtils {
srcTagMetaName = this.conf
.get("parser.html.outlinks.htmlnode_metadata_name");
keepNodenames = (srcTagMetaName != null && srcTagMetaName.length() > 0);
+ blockNodes = new HashSet<>(conf.getTrimmedStringCollection("parser.html.line.separators"));
}
/**
@@ -145,6 +149,13 @@ public class DOMContentUtils {
Node currentNode = walker.nextNode();
String nodeName = currentNode.getNodeName();
short nodeType = currentNode.getNodeType();
+ Node previousSibling = currentNode.getPreviousSibling();
+ if (previousSibling != null
+ && blockNodes.contains(previousSibling.getNodeName().toLowerCase())) {
+ appendParagraphSeparator(sb);
+ } else if (blockNodes.contains(nodeName.toLowerCase())) {
+ appendParagraphSeparator(sb);
+ }
if ("script".equalsIgnoreCase(nodeName)) {
walker.skipChildren();
diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java
index d40958912..34da6a014 100644
--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java
+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/DOMContentUtils.java
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.MapWritable;
@@ -47,6 +48,7 @@ public class DOMContentUtils {
private String srcTagMetaName;
private boolean keepNodenames;
+ private Set<String> blockNodes;
private static class LinkParams {
private String elName;
@@ -104,6 +106,7 @@ public class DOMContentUtils {
srcTagMetaName = this.conf
.get("parser.html.outlinks.htmlnode_metadata_name");
keepNodenames = (srcTagMetaName != null && srcTagMetaName.length() > 0);
+ blockNodes = new HashSet<>(conf.getTrimmedStringCollection("parser.html.line.separators"));
}
/**
@@ -150,6 +153,13 @@ public class DOMContentUtils {
Node currentNode = walker.nextNode();
String nodeName = currentNode.getNodeName();
short nodeType = currentNode.getNodeType();
+ Node previousSibling = currentNode.getPreviousSibling();
+ if (previousSibling != null
+ && blockNodes.contains(previousSibling.getNodeName().toLowerCase())) {
+ appendParagraphSeparator(sb);
+ } else if (blockNodes.contains(nodeName.toLowerCase())) {
+ appendParagraphSeparator(sb);
+ }
if ("script".equalsIgnoreCase(nodeName)) {
walker.skipChildren();