文本挖掘—搜狗语料库数据预处理

package muyanmoyang.text_classify.Classify;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.Filer;

import muyanmoyang.text_classify.toMysql.DBUtil;
import muyanmoyang.text_classify.toMysql.DocBean;

import ICTCLAS.I3S.AC.ICTCLAS50;


/**
 * 搜狗语料库的文本预处理:包括停用词处理、获取存储在MySQL数据库中的语料文本、分词、宽窄字符串互转、
 * 						   对每篇文章初步过滤形成词集合、建立词袋子模型等
 * @author moyang
 * @mail [email protected]
 */
public class DataPreProcess {
	
	public static void main(String[] args) throws IOException, SQLException {
		getStopwordsSet("D:/¥烟酒僧/文本挖掘/stopwords.txt") ;
		getSogouTextFromMySQL() ;
	} 
	
	/**
	 * 停用词处理 , 获取停用词表
	 * @param stopFileDir
	 * @return
	 * @throws IOException 
	 */
	public static Set getStopwordsSet(String stopFileDir) throws IOException
	{
		FileReader stopWordsReader = new FileReader(new File(stopFileDir)) ;
		Set stopwordsSet = new HashSet() ;
		BufferedReader BR = new BufferedReader(stopWordsReader) ;
		String stopwordsStr ; 
		while((stopwordsStr = BR.readLine()) != null)
		{
			stopwordsSet.add(stopwordsStr) ;
		}
		Iterator it = stopwordsSet.iterator() ;
//		while(it.hasNext())
//		{
//			Object obj = it.next() ;
//			System.out.print(obj + "、"); 
//		}
		
		return stopwordsSet ;
	}
	
	/**
	 * 从MySQL数据库获取语料文本,以Map形式保存
	 * @throws SQLException  
	 * @throws IOException 
	 */
	public static Map getSogouTextFromMySQL() throws SQLException, IOException
	{ 
		ArrayList sougouTextListFromSql = new ArrayList() ;//用来存取从数据库中select出的每条语料记录
		Map sougouTextMapFromSql = new HashMap() ;//保存从数据库中select出的语料记录
		FileWriter fileWriter = new FileWriter(new File("F:/hadoop_workspace/Text-Classify/sogouTxtMap.txt")) ;
		
		String url = "jdbc:mysql://localhost:3306/sogou?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "123456";
		Connection conn = DBUtil.getConnection(url, username, password);
		
	    Statement stmt = null;  //表示数据库的更新操作  
	    ResultSet result = null; //表示接收数据库的查询结果  
	    stmt = conn.createStatement();
	    result = stmt.executeQuery("select docno,newstitle,newscontent,newsurl,category from sohunews_reduced"); //执行SQL 语句,查询数据库  
	    int count = 0 ; //记录ResultSet记录所在的行数
	    int tempcount = 0; //记录ResultSet总行数count的值
	    while(result.next())
	    {  
	    	
	    	String docno = result.getString("docno");
	    	String newstitle = result.getString("newstitle") ;
	    	String newscontent = result.getString("newscontent") ;
	    	String newsurl = result.getString("newsurl") ;
	    	String category = result.getString("category") ;
	    	
	    	ResultSetMetaData m = result.getMetaData() ;//获取此 ResultSet 对象的列的编号、类型和属性
	    	int columns = m.getColumnCount() ;//返回此 ResultSet 对象中的列数
	    	
	    	//将每条语料文本添加到ArrayList中
	    	sougouTextListFromSql.add(docno) ;
	    	sougouTextListFromSql.add(newstitle) ;
	    	sougouTextListFromSql.add(newscontent) ;
	    	sougouTextListFromSql.add(newsurl) ;
	    	sougouTextListFromSql.add(category) ;
	    	 
	    	count++ ;
	    	tempcount = count ;
	    	System.out.println("第" + count + "行"); 
	    	
	    	String[] textStringArray = {sougouTextListFromSql.get(0),sougouTextListFromSql.get(1),
	    			sougouTextListFromSql.get(2),sougouTextListFromSql.get(3),sougouTextListFromSql.get(4)} ;
	    	
	    	sougouTextMapFromSql.put(count,textStringArray) ; //将语料以Map的形式保存 
	    	sougouTextListFromSql.clear() ; //移除此列表中的所有元素
	    }
	    System.out.println("行数" + tempcount); 
	    
	    for(int i=1 ; i<=tempcount ; i++)
	    {
	    	String[] str = sougouTextMapFromSql.get(i) ;//迭代出Map集合sougouTextMapFromSql
	    	System.out.println("第" + i + "行:" + str[0] + " | " + str[1] + " | " + str[2] + " | " + str[3] + " | " + str[4]);
	    	//写入到指定txt文件进行保存
	    	fileWriter.write("第" + i + "行:" + str[0] + " | " + str[1] + " | " + str[2] + " | " + str[3] + " | " + str[4] + "\n") ; 
	    }
	    fileWriter.flush() ;
	    fileWriter.close() ;
	    result.close();  
	    conn.close(); // 4、关闭数据库  
		return sougouTextMapFromSql ;
	}
	
	/**
	 * ICTCLAS 分词
	 * @throws UnsupportedEncodingException 
	 */
	public static String segmentation() throws UnsupportedEncodingException
	{
	 	ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
		String argu = ".";
        if(testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")) == false){
            System.out.println("Init Fail");
        }else{
            System.out.println("Init Succeed!");
        }
//TODO 		
		
		return null ;
	}
}


只完成了部分代码,未完待续....

文本挖掘理论以及语料库介绍请参考博客:1. http://www.cnblogs.com/finallyliuyu/archive/2010/10/04/1842261.html   2.  http://www.cnblogs.com/finallyliuyu/archive/2010/09/18/1830444.html

语料库是搜狗2008年的news_sohusite_xml.smarty.zip ,数据库用的是MySQL,表结构如下图,语料库导入MySQL数据库这里不赘述,用的是CSDN的一个Java项目,在此给出链接:http://download.csdn.net/detail/raindreams/3348889,根据自己的数据库字段和实际数据进行修改即可。






你可能感兴趣的:(文本挖掘)