您的位置:首页 > 其它

基于Heritrix+Lucene的搜索引擎构建(3)——页面信息内容抽取

2013-01-06 23:06 555 查看
搜索引擎无非是提供对Web内容的方便检索,以至于能够便捷的获取浏览到相关的页面。

因此,在通过Heritrix等网络蜘蛛获取Web资源以后,首要的任务就是抽取Web页面的内容。

基于java的页面抽取工具有很多,例如,抽取HTML页面的有HtmlParser、Jsoup等,至于Word、Excel等文件的内容,也有相应的工具。

关于HtmlParser、Jsoup等页面内容抽取可以参考相关文献.如《HTML抽取工具Jsoup》。

关于Word等文件,建议学习使用一款叫POI的开源工具来实现:

Apache POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。目前POI已经有了Ruby版本。

结构:

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。

HWPF - 提供读写Microsoft Word DOC格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读Microsoft Visio格式档案的功能。

HPBF - 提供读Microsoft Publisher格式档案的功能。

HSMF - 提供读Microsoft Outlook格式档案的功能。

POI项目网站:http://poi.apache.org/.

最常见的一种PDF文本抽取工具就是PDFBox,PDF文档可以使用PDFBox来处理,http://pdfbox.apache.org/

以下是一些文档内容抽取的例子代码。

Html文档的内容抽取HtmlParser.java:

import java.io.File;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import GEsearcher.encode.FileEncode;
import GEsearcher.index.FileDocument;

/**
* 解析html
* @author Shilong
*
*/
public class HtmlParser {

private String title;
private String content;
private String url;
private String path;
private FileDocument filedocument;

public HtmlParser(String path)
{
//nothing
this.path=path;
filedocument=new FileDocument(path);
AnalysicDocument();//解析
//System.out.println("测试:"+filedocument.getUrl());
}

//获取待分析文件的File对象
public File getFile()
{
return filedocument.getFile();
}

//获取待分析文件编码
public String getEncoding()
{
String val="GBK";
FileEncode fe=new FileEncode(path);
String encode= fe.getEncode();
if(encode.equals("GB-2312")||encode.equals("gb-2312"))
{
val="GB2312";
}else if(encode.equals("UNKNOWN"))
{
val="UTF-8";
}else
{
val=encode;
}
return val;
}

//分析文件
public void AnalysicDocument()
{
File infile=getFile();
try {
Document doc = Jsoup.parse(infile, getEncoding());
title=doc.title();  //获取标题
content=doc.text();  //获取内容
url=filedocument.getUrl(); //获取url
}
catch (IOException e) {
e.printStackTrace();
}
}

//数据的返回
//获取待分析文件的url
public String getUrl()
{
return this.url;
}
public String getTitle()
{
return this.title;
}
public String getContent()
{
return this.content;
}
}


使用tm-extractors实现的Word抽取WordReader.java:

import java.io.File;
import java.io.FileInputStream;

import org.textmining.text.extraction.WordExtractor;

public class WordReader {

private String FilePath;

public WordReader(String FilePath){
this.FilePath =  FilePath;
}

public String getText() {
String text = "";
FileInputStream in;
try {
in = new FileInputStream(new File(FilePath));
WordExtractor extractor= new WordExtractor();
text = extractor.extractText(in);
} catch (Exception e) {
e.printStackTrace();
}
return text;
}

public String getTitle(){
File f = new File(FilePath);
String name = f.getName();

return name;
}

public String getUrl(){
return FilePath;
}


Txt文件内容的读取TxtReader.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class TxtReader {

private String FilePath;

public TxtReader(String FilePath){
this.FilePath = FilePath;
}

public String getText(){
String str="";
try{
BufferedReader br=new BufferedReader(new FileReader(FilePath));
String r=br.readLine();
while(r!=null){
str+=r;
r=br.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
return str;
}

public String getTitle(){
File f = new File(FilePath);
String name = f.getName();
return name;
}

public String getUrl(){
return FilePath;
}
}


总之,在建立搜索索引之前,先对Web页面资源进行文本的抽取处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐