2008-05-15
Lucene實戰開發手記(五)--- 為html/txt格式的文檔創建索引
txt與html解析存在亂碼的問題,這個問題困擾了我好幾天,最後找到一些資料,通過多次嘗試,基本解決了。
public class TxtDocHander extends DocHander {
public Document getDocument(byte[] inputByte) throws IOException {
// 進行文檔的編碼格式識別
CodepageDetectorProxy codepageDetectorProxy = CodepageDetectorProxy.getInstance();
codepageDetectorProxy.add(UnicodeDetector.getInstance());
codepageDetectorProxy.add(JChardetFacade.getInstance());
InputStream inputStream = new ByteArrayInputStream(inputByte);
Charset charset = codepageDetectorProxy.detectCodepage(inputStream,inputByte.length);
String charsetName = charset.name();
if (charsetName.equals("windows-1252")){
charsetName = "big5";//JChardetFacade對big5的編碼的txt識別不是太好
}
String contents = new String(inputByte,charsetName);//編碼轉換了哦
Document document = new Document();
addContent(document, contents);
return document;
}
html解析,除了編碼問題外,還有文本提取的問題。lucene的demo類庫自帶的HTMLParser 用起來很簡單,能很幹凈的去掉html標簽,但在處理有繁體中文字的文檔時,有時會出現解析中止的情況,不知是何原因。而對HtmlParser所提供的Parser,我不太熟悉,使用時有部分html標簽去不掉,故將兩者結合起來使用,已將問題解決。
public class HtmlDocHander extends DocHander {
@Override
public Document getDocument(byte[] inputByte) throws Exception {
// TODO Auto-generated method stub
InputStream inputStream = new ByteArrayInputStream(inputByte);
//編碼轉換
CodepageDetectorProxy codepageDetectorProxy = CodepageDetectorProxy.getInstance();
codepageDetectorProxy.add(new HTMLCodepageDetector());
Charset charset = codepageDetectorProxy.detectCodepage(inputStream,inputByte.length);
String contents = new String(inputByte, charset.name());
Document document = new Document();
addContent(document, parseHtmlToString(contents));//解析出文本內容
return document;
}
public static String parseHtmlToString(String pageContent) throws Exception {
//去掉head部分
int headStart = pageContent.indexOf("<head>");
int endStart = pageContent.indexOf("</head>", headStart);
pageContent = pageContent.substring(0, headStart) + pageContent.substring(endStart+7);
//利用HtmlParser包解析
Parser parser = new Parser(pageContent);
NodeList nodeList =null;
NodeFilter textFilter = new NodeClassFilter(TextNode.class);
nodeList = parser.extractAllNodesThatMatch(textFilter);
StringBuffer bodyBuffer = new StringBuffer("");
for (int i=0; i<nodeList.size(); i++){
Node node = nodeList.elementAt(i);
bodyBuffer.append(node.toPlainTextString());
}
String body = bodyBuffer.toString();
//用lucene demo自帶的HtmlParser進一步刪除多余的標簽
HTMLParser parser2 = new HTMLParser(new StringReader(body));
LineNumberReader reader = new LineNumberReader(parser2.getReader());
StringBuffer buffer = new StringBuffer("");
for (String line = reader.readLine(); line != null; line = reader.readLine()){
buffer.append(line);
}
String contents = buffer.toString();
return contents;
}
评论
不会啊,你可以试一试,HTMLParser是处理得比较干净的,单独是Parser会有很多js代码而且还有html标签,两者结合之后的处理效果比较好. 我试了几个文档都处理得干干净净的.
fys124974704
2008-05-19
回复
这样子 会不会留下很多js的代码呢?我也在苦恼怎么将一个页面完全解析好
就是保留title 和 body 的txt 去掉所有的js和标签
就是保留title 和 body 的txt 去掉所有的js和标签
发表评论
链接
最新评论
-
你用过哪些工作流产品?
有时候系统需要引导,我使用我们自己公司内部的审批系统,也是工作流的,我是真不想用 ...
-- by dayang2001911 -
你用过哪些工作流产品?
惭愧,这方面经验不多,最早做OA时用过IBM的NOTES,但只能说懂点点。深入的 ...
-- by gzspark -
我们的项目毛坯上线了
我给了一些建议给项目经理。技术的积累需要时间,更需要人才,慢慢来。而观念的固化更 ...
-- by cleaneyes -
我们的项目毛坯上线了
你是项目中的骨干了,一些以前公司的好的经验,可以给现在的公司推广一下
-- by climber2002 -
夢-蝴蝶(六)
居然有人看,本来就不希望有多少人看的,其实繁体字多看几篇也就习惯了,不信你试试, ...
-- by cleaneyes







评论排行榜