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

搜索引擎无非是提供对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:

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import org.jsoup.Jsoup;
 5 import org.jsoup.nodes.Document;
 6 
 7 import GEsearcher.encode.FileEncode;
 8 import GEsearcher.index.FileDocument;
 9 
10 /**
11  * 解析html
12  * @author Shilong
13  *
14  */
15 public class HtmlParser {
16 
17     private String title;
18     private String content;
19     private String url;
20     private String path;
21     private FileDocument filedocument;
22     
23     public HtmlParser(String path)
24     {
25         //nothing
26         this.path=path;
27         filedocument=new FileDocument(path);
28         AnalysicDocument();//解析
29         //System.out.println("测试:"+filedocument.getUrl());
30     }
31         
32     //获取待分析文件的File对象
33     public File getFile()
34     {
35         return filedocument.getFile();
36     }
37     
38     //获取待分析文件编码
39     public String getEncoding()
40     {
41         String val="GBK";
42         FileEncode fe=new FileEncode(path);
43         String encode= fe.getEncode();
44         if(encode.equals("GB-2312")||encode.equals("gb-2312"))
45         {
46             val="GB2312";
47         }else if(encode.equals("UNKNOWN"))
48         {
49             val="UTF-8";
50         }else
51         {
52             val=encode;
53         }
54         return val;
55     }
56     
57     //分析文件
58     public void AnalysicDocument()
59     {
60         File infile=getFile();
61         try {
62             Document doc = Jsoup.parse(infile, getEncoding());
63             title=doc.title();  //获取标题
64             content=doc.text();  //获取内容
65             url=filedocument.getUrl(); //获取url
66         } 
67         catch (IOException e) {
68             e.printStackTrace();
69         }
70     }
71     
72     //数据的返回
73     //获取待分析文件的url
74     public String getUrl()
75     {
76         return this.url;
77     }
78     public String getTitle()
79     {
80         return this.title;
81     }
82     public String getContent()
83     {
84         return this.content;
85     }
86 }

 

使用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页面资源进行文本的抽取处理。

转载于:https://www.cnblogs.com/sl-shilong/archive/2013/01/06/2848283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值