Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2397 (improved solution contributed by Vipul Behl, closes #196): - do not add superfluous line breaks and space - fix units tests - also fix parse-html in addition to parse-tika
Sebastian Nagel48c38b03f3cf
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.javaindex 3c2aba0fc..0d6b2a9aa 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@@ -157,9 +157,10 @@ public class DOMContentUtils {text = text.replaceAll("\\s+", " ");text = text.trim();if (text.length() > 0) {- if (sb.length() > 0)- sb.append(' ');+ appendSpace(sb);sb.append(text);+ } else {+ appendParagraphSeparator(sb);}}}@@ -167,6 +168,48 @@ public class DOMContentUtils {return abort;}+ /**+ * Conditionally append a paragraph/line break to StringBuffer unless last+ * character a already indicates a paragraph break. Also remove trailing space+ * before paragraph break.+ *+ * @param buffer+ * StringBuffer to append paragraph break+ */+ private void appendParagraphSeparator(StringBuffer buffer) {+ if (buffer.length() == 0) {+ return;+ }+ char lastChar = buffer.charAt(buffer.length() - 1);+ if ('\n' != lastChar) {+ // remove white space before paragraph break+ while (lastChar == ' ') {+ buffer.deleteCharAt(buffer.length() - 1);+ lastChar = buffer.charAt(buffer.length() - 1);+ }+ if ('\n' != lastChar) {+ buffer.append('\n');+ }+ }+ }++ /**+ * Conditionally append a space to StringBuffer unless last character is a+ * space or line/paragraph break.+ *+ * @param buffer+ * StringBuffer to append space+ */+ private void appendSpace(StringBuffer buffer) {+ if (buffer.length() == 0) {+ return;+ }+ char lastChar = buffer.charAt(buffer.length() - 1);+ if (' ' != lastChar && '\n' != lastChar) {+ buffer.append(' ');+ }+ }+/*** This method takes a {@link StringBuffer} and a DOM {@link Node}, and will* append the content text found beneath the first <code>title</code> node todiff --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.javaindex b820e0c5f..a5b06c726 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@@ -162,11 +162,10 @@ public class DOMContentUtils {text = text.replaceAll("\\s+", " ");text = text.trim();if (text.length() > 0) {- if (sb.length() > 0)- sb.append(' ');+ appendSpace(sb);sb.append(text);} else {- sb.append("\n");+ appendParagraphSeparator(sb);}}}@@ -174,6 +173,48 @@ public class DOMContentUtils {return abort;}+ /**+ * Conditionally append a paragraph/line break to StringBuffer unless last+ * character a already indicates a paragraph break. Also remove trailing space+ * before paragraph break.+ *+ * @param buffer+ * StringBuffer to append paragraph break+ */+ private void appendParagraphSeparator(StringBuffer buffer) {+ if (buffer.length() == 0) {+ return;+ }+ char lastChar = buffer.charAt(buffer.length() - 1);+ if ('\n' != lastChar) {+ // remove white space before paragraph break+ while (lastChar == ' ') {+ buffer.deleteCharAt(buffer.length() - 1);+ lastChar = buffer.charAt(buffer.length() - 1);+ }+ if ('\n' != lastChar) {+ buffer.append('\n');+ }+ }+ }++ /**+ * Conditionally append a space to StringBuffer unless last character is a+ * space or line/paragraph break.+ *+ * @param buffer+ * StringBuffer to append space+ */+ private void appendSpace(StringBuffer buffer) {+ if (buffer.length() == 0) {+ return;+ }+ char lastChar = buffer.charAt(buffer.length() - 1);+ if (' ' != lastChar && '\n' != lastChar) {+ buffer.append(' ');+ }+ }+/*** This method takes a {@link StringBuffer} and a DOM {@link Node}, and will* append the content text found beneath the first <code>title</code> node todiff --git a/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java b/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.javaindex 17e386ad1..bbb0866ea 100644--- a/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java+++ b/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java@@ -46,7 +46,7 @@ public class TestZipParser {private String[] sampleFiles = { "test.zip" };- private String expectedText = "textfile.txt This is text file number 1 ";+ private String expectedText = "textfile.txt This is text file number 1";@Testpublic void testIt() throws ProtocolException, ParseException {@@ -64,7 +64,10 @@ public class TestZipParser {new CrawlDatum()).getContent();parse = new ParseUtil(conf).parseByExtensionId("parse-zip", content).get(content.getUrl());- Assert.assertTrue(parse.getText().equals(expectedText));+ Assert.assertTrue(+ "Extracted text does not start with <" + expectedText + ">: <"+ + parse.getText() + ">",+ parse.getText().startsWith(expectedText));}}