RSS
热门关键字:  数据挖掘  数据仓库  人工智能  搜索引擎  数据挖掘导论

如何使用Lucene进行全文检索(二)

来源: 作者:互联网作品 时间:2007-04-12 点击:

在使用lucene对相关内容进行索引时,会遇到各种格式的内容,例如html,pdf,word等等,那么我们如何从这么文档中得到我们需要的内容哪?例如html的内容,一般我们不需要对html标签建立索引,因为那不是我们需要搜索的内容.这个时候,我们就需要从html内容中解析出我们所需要的内容.对于pdf,word文档,也是类似的要求.
  
  总之,我们只需要从内容中提取出我们需要的文本来建立索引,这样用户就能搜索到需要的内容,然后访问对应的资源即可. 
    lucene本身带的例子中有一个解析html的代码,不过不是纯java的,所以在网上我又找到了另外一个html解析器,网址如下:http://htmlparser.sourceforge.net.
  
  对pdf解析的相关项目有很多,例如pdfbox.在pdfbox里面提出pdf的文本内容只需要一句话即可:  
  
    document doc = lucenepdfdocument.getdocument( file );   

  
  当然如果需要更高级的设置,就要使用pdfbox中pdftextstripper等类来实现更高级的操作了.
  
  
  对word文档解析的相关有poi,网址是 http://jakarta.apache.org/poi/. 数据挖掘研究院
  
  htmlparser本身提供的功能很强大,我们下面主要来关注我们需要的功能.首先给出几个函数如下:
  
 /**
 * 解析一个html页面,返回一个html页面类.
 *
 * @param resource 文件路径或者网址
 */
    public static searchhtmlpage parsehtmlpage(string resource)
    {
        string title = "";
        string body = "";
        try
        {
            parser myparser = new parser(resource);

            //设置编码:根据实际情况修改
            myparser.setencoding("gbk"); 数据挖掘研究院

            htmlpage visitor = new htmlpage(myparser);

            myparser.visitallnodeswith(visitor);

            title = visitor.gettitle();

            body = combinenodetext(visitor.getbody().tonodearray());
        }
        catch (parserexception e)
        {
            logman.error("parse html page " + resource + " error!");
        }

        searchhtmlpage result = new searchhtmlpage(title, body); 数据挖掘研究院

        return result;
    }

    /**
     * 解析html内容,得到普通文本和链接的内容.
     * 
     * @param content 要解析的内容
     * @return 返回解析后的内容
     */
    public static string parsehtmlcontent(string content)
    {
        parser myparser;
        nodelist nodelist = null;

        myparser = parser.createparser(content, "gbk");

        nodefilter textfilter = new nodeclassfilter(textnode.class); 数据挖掘实验室
        nodefilter linkfilter = new nodeclassfilter(linktag.class);

        //暂时不处理 meta
        //nodefilter metafilter = new nodeclassfilter(metatag.class);

        orfilter lastfilter = new orfilter();
        lastfilter.setpredicates(new nodefilter[] { textfilter, linkfilter });

        try
        {
            nodelist = myparser.parse(lastfilter);
        }
        catch (parserexception e)
        {

数据挖掘研究院


            logman.warn("parse content error", e);
        }

        //中场退出了
        if (null == nodelist)
        {
            return "";
        }

        node[] nodes = nodelist.tonodearray();

        string result = combinenodetext(nodes);
        return result;
    }

 //合并节点的有效内容
    private static string combinenodetext(node[] nodes)

数据挖掘研究院


    {
        stringbuffer result = new stringbuffer();

        for (int i = 0; i < nodes.length; i++)
        {
            node anode = (node) nodes[i];

            string line = "";
            if (anode instanceof textnode)
            {
                textnode textnode = (textnode) anode;
                //line = textnode.toplaintextstring().trim();
                line = textnode.gettext();
            }
            else if (anode instanceof linktag)
            {
                linktag linknode = (linktag) anode;

                line = linknode.getlink();
                //过滤jsp标签
                line = stringfunc.replace(line, "<%.*%>", "");
            }

            if (stringfunc.istrimempty(line)) continue;

            result.append(" ").append(line);
        }

        return result.tostring();
    }
  
  其中searchhtmlpage类是表示一个html页面的模型,包含标题和内容,代码如下:
  
 package com.jscud.www.support.search;
 
 /**
  * 搜索时解析html后返回的页面模型.
  * 
  * @author scud(飞云小侠) http://www.jscud.com
  *  
  */
 public class searchhtmlpage
 {
     /**标题*/ 数据挖掘研究院
     private string title;
 
     /**内容*/
     private string body;
     
     public searchhtmlpage(string title, string body)
     {
         this.title = title;
         this.body = body;
     }
     
     public string getbody()
     {
         return body;
     }
 
     public void setbody(string body)
     {
         this.body = body;

数据挖掘实验室


     }
 
     public string gettitle()
     {
         return title;
     }
 
     public void settitle(string title)
     {
         this.title = title;
     }
 }
 
  当然,使用htmlparser解析html资源还有很多其他的方法,可以设置很多的条件来满足用户的解析要求,用户可以阅读其他的文章或者htmlparser的文档来了解,在此不多介绍.
  
  下一节讲解如何进行搜索.
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?