使用Lucene進行全文檢索(二)---得到有效的內(nèi)容

          scud(飛云小俠) http://www.jscud.com 轉(zhuǎn)載請注明來源/作者

          關(guān)鍵字:lucene,html parser,全文檢索,IndexReader,Document,Field,IndexWriter,Term,HTMLPAGE


            在使用lucene對相關(guān)內(nèi)容進行索引時,會遇到各種格式的內(nèi)容,例如Html,PDF,Word等等,那么我們?nèi)绾螐倪@么文檔中得到我們需要的內(nèi)容哪?例如Html的內(nèi)容,一般我們不需要對Html標簽建立索引,因為那不是我們需要搜索的內(nèi)容.這個時候,我們就需要從Html內(nèi)容中解析出我們所需要的內(nèi)容.對于PDF,Word文檔,也是類似的要求.
           
            總之,我們只需要從內(nèi)容中提取出我們需要的文本來建立索引,這樣用戶就能搜索到需要的內(nèi)容,然后訪問對應(yīng)的資源即可.

            Lucene本身帶的例子中有一個解析Html的代碼,不過不是純JAVA的,所以在網(wǎng)上我又找到了另外一個Html解析器,網(wǎng)址如下:http://htmlparser.sourceforge.net.
           
            對PDF解析的相關(guān)項目有很多,例如PDFBox.在PDFBox里面提出pdf的文本內(nèi)容只需要一句話即可:  
            
             
          Document doc = LucenePDFDocument.getDocument( file );  

            
            當(dāng)然如果需要更高級的設(shè)置,就要使用PDFBox中PDFTextStripper等類來實現(xiàn)更高級的操作了.
           
           
            對Word文檔解析的相關(guān)有POI,網(wǎng)址是 http://jakarta.apache.org/poi/.
           
            HtmlParser本身提供的功能很強大,我們下面主要來關(guān)注我們需要的功能.首先給出幾個函數(shù)如下:
           

           /**
           * 解析一個Html頁面,返回一個Html頁面類.
           *
           * @param resource 文件路徑或者網(wǎng)址
           */
              public static SearchHtmlPage parseHtmlPage(String resource)
              {
                  String title = "";
                  String body = "";
                  try
                  {
                      Parser myParser = new Parser(resource);

                      //設(shè)置編碼:根據(jù)實際情況修改
                      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內(nèi)容,得到普通文本和鏈接的內(nèi)容.
               *
               * @param content 要解析的內(nèi)容
               * @return 返回解析后的內(nèi)容
               */
              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;
              }

           //合并節(jié)點的有效內(nèi)容
              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頁面的模型,包含標題和內(nèi)容,代碼如下:
            
           package com.jscud.www.support.search;
           
           /**
            * 搜索時解析Html后返回的頁面模型.
            *
            * @author scud(飛云小俠) http://www.jscud.com
            * 
            */
           public class SearchHtmlPage
           {
               /**標題*/
               private String title;
           
               /**內(nèi)容*/
               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;
               }
           }
           


           
            當(dāng)然,使用HtmlParser解析Html資源還有很多其他的方法,可以設(shè)置很多的條件來滿足用戶的解析要求,用戶可以閱讀其他的文章或者HtmlParser的文檔來了解,在此不多介紹.
           
            下一節(jié)講解如何進行搜索.

           

          posted on 2005-08-12 17:33 Scud(飛云小俠) 閱讀(828) 評論(0)  編輯  收藏 所屬分類: Java

          <2005年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計

          公告

          文章發(fā)布許可
          創(chuàng)造共用協(xié)議:署名,非商業(yè),保持一致

          我的郵件
          cnscud # gmail


          常用鏈接

          留言簿(15)

          隨筆分類(113)

          隨筆檔案(103)

          相冊

          友情鏈接

          技術(shù)網(wǎng)站

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 梁河县| 工布江达县| 孟津县| 天祝| 营山县| 鄂托克旗| 四川省| 姜堰市| 两当县| 朔州市| 会东县| 怀集县| 遂宁市| 佛坪县| 礼泉县| 松桃| 留坝县| 宁南县| 中牟县| 扬州市| 兴海县| 报价| 和政县| 黎川县| 木里| 东方市| 浑源县| 钟山县| 延边| 常德市| 阿图什市| 阳谷县| 香格里拉县| 牡丹江市| 福贡县| 宜兰县| 宝应县| 进贤县| 大埔县| 新密市| 隆回县|