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;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值