编者注:咱们发现了有趣的系列文章《30天学习30种新技术》,正在翻译,一天一篇更新,年终礼包。下面是第 18 天的内容。html
今天我决定学习如何使用Java作网页连接的文本和图像提取。在大多数内容发现网站上(如Prismatic)这是一个很是常见的需求,今天就是学习如何使用一个名为boilerpipe的Java库来完成这个任务。java
基本的Java知识是必需的,安装最新的Java开发工具包(JDK ),能够是OpenJDK 7或Oracle JDK 7。git
注册一个OpenShift账户,它是彻底免费的,能够分配给每一个用户1.5 GB的内存和3 GB的磁盘空间。web
安装RHC客户端工具,须要有ruby 1.8.7或更新的版本,若是已经有ruby gem,输入 sudo gem install rhc
,确保它是最新版本。要更新RHC的话,执行命令 sudo gem update rhc
,如需其余协助安装RHC命令行工具,请参阅该页面: https://www.openshift.com/developers/rhc-client-tools-installsegmentfault
经过 rhc setup
命令设置您的OpenShift账户,此命令将帮助你建立一个命名空间,并上传你的SSH keys到OpenShift服务器。api
首先从建立示例应用程序开始,把该应用称做 newsapp
。ruby
$ rhc create-app newsapp jbosseap
而后可使用以下命令:服务器
$ rhc create-app newsapp jbosseap -g medium
这样会建立一个应用程序容器,设置好全部须要的SELinux政策和cgroup配置,OpenShift也将建立一个私人git仓库并克隆到本地。最后,OpenShift会给外界提供一个DNS,该应用程序将在http://newsapp-{domain-name}.rhcloud.com/
下能够访问(将 domain-name
更换为本身的域名)。oracle
在 pom.xml
文件里添加以下依赖:app
<dependency> <groupId>de.l3s.boilerpipe</groupId> <artifactId>boilerpipe</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.13</version> </dependency>
同时也须要加一个新的库:
<repository> <id>boilerpipe-m2-repo</id> <url>http://boilerpipe.googlecode.com/svn/repo/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository>
经过更新 pom.xml
文件里的几个特性将Maven项目更新到Java 7:
<maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target>
如今就能够更新Maven项目了(右键单击>Maven>更新项目)。
使用CDI来进行依赖注入。CDI、上下文和依赖注入是一个Java EE 6规范,可以使依赖注入在Java EE 6的项目中。
在 src/main/webapp/WEB-INF
文件夹下建一个名为beans.xml
中一个新的XML文件。更换beans.xml中的如下内容:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>
如今建立一个Boilerpipe内容提取服务的服务类,这个类会用一个url,从这个url中提取标题和文章内容。
import java.net.URL; import java.util.Collections; import java.util.List; import com.newsapp.boilerpipe.image.Image; import com.newsapp.boilerpipe.image.ImageExtractor; import de.l3s.boilerpipe.BoilerpipeExtractor; import de.l3s.boilerpipe.document.TextDocument; import de.l3s.boilerpipe.extractors.ArticleExtractor; import de.l3s.boilerpipe.extractors.CommonExtractors; import de.l3s.boilerpipe.sax.BoilerpipeSAXInput; import de.l3s.boilerpipe.sax.HTMLDocument; import de.l3s.boilerpipe.sax.HTMLFetcher; public class BoilerpipeContentExtractionService { public Content content(String url) { try { final HTMLDocument htmlDoc = HTMLFetcher.fetch(new URL(url)); final TextDocument doc = new BoilerpipeSAXInput(htmlDoc.toInputSource()).getTextDocument(); String title = doc.getTitle(); String content = ArticleExtractor.INSTANCE.getText(doc); final BoilerpipeExtractor extractor = CommonExtractors.KEEP_EVERYTHING_EXTRACTOR; final ImageExtractor ie = ImageExtractor.INSTANCE; List<Image> images = ie.process(new URL(url), extractor); Collections.sort(images); String image = null; if (!images.isEmpty()) { image = images.get(0).getSrc(); } return new Content(title, content.substring(0, 200), image); } catch (Exception e) { return null; } } }
上述代码执行如下操做:
为启用JAX-RS,创建一个扩展 javax.ws.rs.core.Application
的类,并经过以下所示的 javax.ws.rs.ApplicationPath
注释指定应用程序路径。
import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api/v1") public class JaxrsInitializer extends Application{ }
建立ContentExtractionResource
类,它会返回一个JSON内容对象。建立一个名为ContentExtractionResource
的新类,并用以下所示的内容替换:
import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import com.newsapp.service.BoilerpipeContentExtractionService; import com.newsapp.service.Content; @Path("/content") public class ContentExtractionResource { @Inject private BoilerpipeContentExtractionService boilerpipeContentExtractionService; @GET @Produces(value = MediaType.APPLICATION_JSON) public Content extractContent(@QueryParam("url") String url) { return boilerpipeContentExtractionService.content(url); } }
最后,更改部署到OpenShift
$ git add . $ git commit -am "NewApp" $ git push
在代码push和部署完成后,咱们能够在 http://newsapp-{{domain-name}.rhcloud.com
查看正在运行的应用程序。个人示例应用程序展现以下。
今天就这些,欢迎反馈。
原文 Day 18: BoilerPipe--Article Extraction for Java Developers
翻译整理 SegmentFault