DANCE WITH JAVA

          開發(fā)出高質(zhì)量的系統(tǒng)

          常用鏈接

          統(tǒng)計

          積分與排名

          好友之家

          最新評論

          lucene 索引非txt文檔 (pdf word rtf html xml)

          搜索要首先要索引,索引的話最簡單的方式是索引txt文件,上文已經(jīng)介紹了。這里介紹一下一些其它格式的文檔的索引,例如ms word ,pdf ,rtf等。
          索引方法:就是先把各種文檔先轉(zhuǎn)化成純文本再索引,所以關(guān)鍵在轉(zhuǎn)換上。幸好java世界中有太多的開源工程,很多都可以拿來直接使用。下邊分別介紹一下:
          寫在所有之前:下邊所有介紹中的is參數(shù)都是inputStream,就是被索引的文件。
          word文檔:
          把word文檔轉(zhuǎn)換成純文本的開源工程可以使用:POI 或者TextMining
          POI的使用方法:
           WordDocument wd = new WordDocument(is);
                StringWriter docTextWriter 
          = new StringWriter();
                wd.writeAllText(
          new PrintWriter(docTextWriter));
                docTextWriter.close();
                bodyText 
          = docTextWriter.toString();
          TextMining的使用方法更簡單:
          bodyText = new WordExtractor().extractText(is);

          PDF文檔:
          轉(zhuǎn)換PDF文檔可以使用的類庫是PDFbox
          COSDocument cosDoc = null;
             PDFParser parser = new PDFParser(is);
              parser.parse();
          cosDoc 
          = parser.getDocument()
          if (cosDoc.isEncrypted()) {
                  DecryptDocument decryptor 
          = new DecryptDocument(cosDoc);
                  decryptor.decryptDocument(password);
           }
          PDFTextStripper stripper 
          = new PDFTextStripper();
          String docText 
          = stripper.getText(new PDDocument(cosDoc));
          RTF文檔:
          rtf的轉(zhuǎn)換則在javax中就有
          DefaultStyledDocument styledDoc = new DefaultStyledDocument();
          new RTFEditorKit().read(is, styledDoc, 0);
                String bodyText 
          = styledDoc.getText(0, styledDoc.getLength());
          這樣就可以索引各種格式的文本了

          html和xml的處理方法同樣
          不同的是html的可用類庫是:JTidy
          Xml可用的類庫是SAX和digester

          posted on 2007-06-14 13:27 dreamstone 閱讀(8010) 評論(9)  編輯  收藏 所屬分類: 搜索引擎lucence

          評論

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2007-08-08 17:31 123

          不支持中文......  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2008-01-03 18:10 qq307059755

          上述代碼是否有誤?
          運行到
          String docText = stripper.getText(new PDDocument(cosDoc));
          時報:
          正在索引: C:\word\source\基于內(nèi)容的圖像檢索研究進展.pdf 文件

          Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/fontbox/cmap/CMapParser

          at org.pdfbox.pdmodel.font.PDFont.parseCmap(PDFont.java:534)

          at org.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:412)

          at org.pdfbox.util.PDFStreamEngine.showString(PDFStreamEngine.java:325)

          at org.pdfbox.util.operator.ShowTextGlyph.process(ShowTextGlyph.java:80)

          at org.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:452)

          at org.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:215)

          at org.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:174)

          at org.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:336)

          at org.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:259)

          at org.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:216)

          at org.pdfbox.util.PDFTextStripper.getText(PDFTextStripper.java:149)

          at textsearch.TextSearchFrame.indexFile(TextSearchFrame.java:235)
          ..........  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2008-01-03 19:57 qq307059755

          問題已解決:在下載的pdfbox包中包括org.fontbox.cmap.CMapParser 類  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2008-07-01 11:33 wrj

          需要包更多的jar包,根據(jù)提示的類在包的整個目錄下找需要的包,如搜CMapParser ,會搜出一個jar包,將其引入你的應(yīng)用即可!  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2008-07-23 17:18

          是嗎,我下的所有pdfbox里面都沒有那個類,是那個事嗎?@qq307059755
            回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml)[未登錄] 2008-09-09 11:19 hp

          org.fontbox.cmap.CMapParser 這個類;我也沒有找到;怎么回事啊;高手指點一下啊;我的也報這個錯誤啊;  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2010-02-25 11:10 shusnail

          我也報這個錯  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2011-07-29 11:04 拉面tina

          如果是中文的要怎么檢索啊,用的是lucene2.0.0,O(∩_∩)O  回復(fù)  更多評論   

          # re: lucene 索引非txt文檔 (pdf word rtf html xml) 2014-03-13 22:13 第三方第三方

          打發(fā)第三方的說法是打發(fā)的撒  回復(fù)  更多評論   

          主站蜘蛛池模板: 手机| 乌拉特后旗| 栾城县| 宝应县| 阳江市| 藁城市| 奈曼旗| 普安县| 扶风县| 莎车县| 明溪县| 富阳市| 鄂托克前旗| 株洲县| 株洲市| 郯城县| 永嘉县| 阳曲县| 石楼县| 乌什县| 蓬安县| 漳平市| 郸城县| 民和| 祥云县| 北宁市| 思南县| 苍南县| 炉霍县| 北安市| 雅安市| 积石山| 临颍县| 青海省| 天等县| 温宿县| 于田县| 石楼县| 玉屏| 金山区| 柳河县|