Apache Nutch is an extensible and scalable web crawler
NUTCH-961 Expose Tika's Boilerpipe support
Markus Jelsma5784b64a5d75
diff --git a/CHANGES.txt b/CHANGES.txtindex 048a4d6c4..5b98b3de8 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -1,5 +1,7 @@Nutch Change Log+* NUTCH-961 Expose Tika's Boilerpipe support (Gabriele Kahlout, Vincent Slot, markus)+* NUTCH-1233 Rely on Tika for outlink extraction (markus)* NUTCH-2210 Upgrade to Tika 1.12 (markus)diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex 2ff2ae912..c6d4dce83 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -1300,6 +1300,22 @@ CAUTION: Set the parser.timeout to -1 or a bigger value than 30, when using this</description></property>+<property>+ <name>tika.extractor</name>+ <value>none</value>+ <description>+ Which text extraction algorithm to use. Valid values are: boilerpipe or none.+ </description>+</property>++<property>+ <name>tika.extractor.boilerpipe.algorithm</name>+ <value>ArticleExtractor</value>+ <description>+ Which Boilerpipe algorithm to use. Valid values are: DefaultExtractor, ArticleExtractor+ or CanolaExtractor.+ </description>+</property><!-- urlfilter plugin properties -->diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/BoilerpipeExtractorRepository.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/BoilerpipeExtractorRepository.javanew file mode 100644index 000000000..7c0d71bb2--- /dev/null+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/BoilerpipeExtractorRepository.java@@ -0,0 +1,62 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.nutch.parse.tika;++import java.lang.ClassLoader;+import java.lang.InstantiationException;+import java.util.HashMap;+import org.apache.commons.logging.Log;+import org.apache.commons.logging.LogFactory;+import org.apache.tika.parser.html.BoilerpipeContentHandler;+import de.l3s.boilerpipe.BoilerpipeExtractor;+import de.l3s.boilerpipe.extractors.*;++class BoilerpipeExtractorRepository {++ public static final Log LOG = LogFactory.getLog(BoilerpipeExtractorRepository.class);+ public static final HashMap<String, BoilerpipeExtractor> extractorRepository = new HashMap<String, BoilerpipeExtractor>();++ /**+ * Returns an instance of the specified extractor+ */+ public static synchronized BoilerpipeExtractor getExtractor(String boilerpipeExtractorName) {+ // Check if there's no instance of this extractor+ if (!extractorRepository.containsKey(boilerpipeExtractorName)) {+ // FQCN+ boilerpipeExtractorName = "de.l3s.boilerpipe.extractors." + boilerpipeExtractorName;++ // Attempt to load the class+ try {+ ClassLoader loader = BoilerpipeExtractor.class.getClassLoader();+ Class extractorClass = loader.loadClass(boilerpipeExtractorName);++ // Add an instance to the repository+ extractorRepository.put(boilerpipeExtractorName, (BoilerpipeExtractor)extractorClass.newInstance());++ } catch (ClassNotFoundException e) {+ LOG.error("BoilerpipeExtractor " + boilerpipeExtractorName + " not found!");+ } catch (InstantiationException e) {+ LOG.error("Could not instantiate " + boilerpipeExtractorName);+ } catch (Exception e) {+ LOG.error(e);+ }+ }++ return extractorRepository.get(boilerpipeExtractorName);+ }++}diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.javaindex fba758498..5d7eca9d8 100644--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java@@ -40,6 +40,7 @@ import org.apache.nutch.protocol.Content;import org.apache.tika.config.TikaConfig;import org.apache.tika.metadata.Metadata;import org.apache.tika.mime.MediaType;+import org.apache.tika.parser.html.BoilerpipeContentHandler;import org.apache.tika.parser.ParseContext;import org.apache.tika.parser.Parser;import org.apache.tika.parser.html.HtmlMapper;@@ -72,6 +73,9 @@ public class TikaParser implements org.apache.nutch.parse.Parser {@SuppressWarnings("deprecation")public ParseResult getParse(Content content) {String mimeType = content.getContentType();++ boolean useBoilerpipe = getConf().get("tika.extractor", "none").equals("boilerpipe");+ String boilerpipeExtractorName = getConf().get("tika.extractor.boilerpipe.algorithm", "ArticleExtractor");URL base;try {@@ -101,13 +105,25 @@ public class TikaParser implements org.apache.nutch.parse.Parser {doc.setErrorChecking(false);DocumentFragment root = doc.createDocumentFragment();- DOMBuilder domhandler = new DOMBuilder(doc, root);+ ContentHandler domHandler;++ // Check whether to use Tika's BoilerplateContentHandler+ if (useBoilerpipe) {+ BoilerpipeContentHandler bpHandler = new BoilerpipeContentHandler((ContentHandler)new DOMBuilder(doc, root),+ BoilerpipeExtractorRepository.getExtractor(boilerpipeExtractorName));+ bpHandler.setIncludeMarkup(true);+ domHandler = (ContentHandler)bpHandler;+ } else {+ DOMBuilder domBuilder = new DOMBuilder(doc, root);+ domBuilder.setUpperCaseElementNames(upperCaseElementNames);+ domBuilder.setDefaultNamespaceURI(XHTMLContentHandler.XHTML);+ domHandler = (ContentHandler)domBuilder;+ }+LinkContentHandler linkContentHandler = new LinkContentHandler();- domhandler.setUpperCaseElementNames(upperCaseElementNames);- domhandler.setDefaultNamespaceURI(XHTMLContentHandler.XHTML);ParseContext context = new ParseContext();- TeeContentHandler teeContentHandler = new TeeContentHandler(domhandler, linkContentHandler);+ TeeContentHandler teeContentHandler = new TeeContentHandler(domHandler, linkContentHandler);if (HTMLMapper != null)context.set(HtmlMapper.class, HTMLMapper);