Bayes过滤方法之一
来源:互联网 发布:剑灵召唤师捏脸数据 编辑:程序博客网 时间:2024/04/29 11:16
毕业设计部分:
/**//*
* ArticleCat.java
* 内容分类
* Created on 2006年12月15日, 下午10:04
*
* 马如林 桂林电子科技大学
*
*/
package com.mrl;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.xjt.nlp.word.ICTCLAS;
import com.mrl.DbConn;
/** *//**
*
* @author longronglin
*/
public class ArticleCat
...{
/** *//** 测试分类文件夹 */
private static String FILE_DIR = "D:/ContentAudit/blogAudit/Test/CommentsInnormal";
/** *//** 不正常内容文件出现次数 */
private static int appearCount = 0;
/** *//** 哈希表 */
private static Hashtable KeywordProbality = new Hashtable();
/** *//** 最大的Double值 */
private static double maxDouble = 1.0;
/** *//** 最小的Double值 */
private static double minDouble = -1.0;
/** *//** Creates a new instance of ArticleCat */
public ArticleCat ()
...{
}
public static void main(String[] args)
...{
appearCount = 0;
// 获取关键词及对应的概率
getKeywordProbaliy();
maxDouble = Double.MAX_VALUE / maxDouble;
minDouble = Double.MIN_VALUE / minDouble;
getFile(FILE_DIR);
System.out.print (appearCount);
}
/**//*
* 根据系统默认设置获取对应的词和概率存储在向量中
*/
private static void getKeywordProbaliy()
...{
FileProcess fileProcess = new FileProcess();
/** *//** 读取数据配置文件所在目录 */
String dir = fileProcess.GetCurrDir ();
/** *//** 系统默认设置的目录文件 */
String fileName = "/blogAudit/Incoming/300CommentsKeywordsPossible.txt";
/** *//** 完整的文件访问路径 */
String fullFileName = dir + fileName;
File myFile = new File(fullFileName);
if(!myFile.exists())
...{
System.err.println("Can't Find " + fullFileName);
}
try
...{
BufferedReader in = new BufferedReader(new FileReader(myFile));
String str;
while ((str = in.readLine()) != null)
...{
// |为分隔符
int divPos = str.lastIndexOf("|");
try
...{
if(maxDouble < Double.parseDouble(str.substring(divPos+1,str.length())))
...{
// 最大值
maxDouble = Double.parseDouble(str.substring(divPos+1,str.length()));
}
if(minDouble > Double.parseDouble(str.substring(divPos+1,str.length())))
...{
// 最小值
minDouble = Double.parseDouble(str.substring(divPos+1,str.length()));
}
// 添加词及对应的概率
KeywordProbality.put(str.substring(0,divPos),str.substring(divPos+1,str.length()));
}
catch(Exception e)
...{
e.getStackTrace();
}
}
in.close();
}
catch (IOException e)
...{
e.getStackTrace();
}
}
/**//*
* 获取文件夹里的文本文件
*/
public static void getFile(String dirPath)
...{
/** *//** 建立当前目录中文件的File对象 */
File filesDir = new File(dirPath);
/** *//** 取得代表目录中所有文件的File对象数组 */
File list[] = filesDir.listFiles();
/** *//** 总文件数 */
System.out.println (list.length);
for(int i=0;i<list.length;i++)
...{
if(list[i].isFile())
...{
System.out.println(list[i].getPath ());
/** *//** i表示现在的words表的起始序号 读取内容*/
readContent(list[i].getPath (),i);
}
}
}
/**//*
* 读取内容
*/
public static void readContent(String filePath, int articleNum)
...{
String strContent = null;
try
...{
BufferedReader in = new BufferedReader(new FileReader(filePath));
String strTemp = null;
while ((strTemp = in.readLine()) != null)
...{
strContent = strContent + strTemp;
}
in.close();
}
catch (IOException e)
...{
e.getStackTrace();
}
// 对内容进行分词
divLexical(strContent,articleNum);
}
/**//*
* 调用ICTCLAS进行分词
*/
public static void divLexical(String strContent,int articleNum)
...{
ICTCLAS ictclas = new ICTCLAS();
if(!ictclas.init (0,2))
...{
ictclas.init (0,2);
}
String strTrans=ictclas.paragraphProcess(strContent);
// 对分词后的字符进行处理
filterString(strTrans,articleNum);
}
/**//*
* 对分词后的字符进行处理,并计算最终的概率值,进行判断
*/
public static void filterString(String srcStr,int articleNum)
...{
/** *//** 使用trim去掉前后多余空格防止发生意外 */
String strTemp=srcStr.trim();
String tempText = "";
double probality = 1.0;
StringTokenizer st = new StringTokenizer(strTemp," ");
int len = st.countTokens();
int i = 0;
while(i<len)
...{
tempText = st.nextToken();
i++;
// 获取该词对应的概率
double keywordProbality = divStr(tempText);
...{
probality = probality * keywordProbality;
// 用2个参数分别表示上溢出和下溢出值
// 溢出处理
// 一旦遇到上溢出 表明其中含有不良信息 即可停止计算
if(probality < minDouble)
...{
// 继续往下计算
probality = 1.0;
}
}
}
System.out.println (probality);
if(probality >1.0)
...{
// 不良记录个数加1
appearCount++;
}
}
/**//*
* 获取词对应的先验概率
*/
public static double divStr(String srcStr)
...{
StringTokenizer st = new StringTokenizer(srcStr,"/");
double probality = 1.0;
int len = st.countTokens();
if(len == 2)
...{
String strPre = st.nextToken ();
// 获取该词的先验概率
try
...{
probality = Double.parseDouble(KeywordProbality.get(strPre).toString());
}
catch(Exception e)
...{
e.getMessage();
}
}
return probality;
}
}
* ArticleCat.java
* 内容分类
* Created on 2006年12月15日, 下午10:04
*
* 马如林 桂林电子科技大学
*
*/
package com.mrl;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.xjt.nlp.word.ICTCLAS;
import com.mrl.DbConn;
/** *//**
*
* @author longronglin
*/
public class ArticleCat
...{
/** *//** 测试分类文件夹 */
private static String FILE_DIR = "D:/ContentAudit/blogAudit/Test/CommentsInnormal";
/** *//** 不正常内容文件出现次数 */
private static int appearCount = 0;
/** *//** 哈希表 */
private static Hashtable KeywordProbality = new Hashtable();
/** *//** 最大的Double值 */
private static double maxDouble = 1.0;
/** *//** 最小的Double值 */
private static double minDouble = -1.0;
/** *//** Creates a new instance of ArticleCat */
public ArticleCat ()
...{
}
public static void main(String[] args)
...{
appearCount = 0;
// 获取关键词及对应的概率
getKeywordProbaliy();
maxDouble = Double.MAX_VALUE / maxDouble;
minDouble = Double.MIN_VALUE / minDouble;
getFile(FILE_DIR);
System.out.print (appearCount);
}
/**//*
* 根据系统默认设置获取对应的词和概率存储在向量中
*/
private static void getKeywordProbaliy()
...{
FileProcess fileProcess = new FileProcess();
/** *//** 读取数据配置文件所在目录 */
String dir = fileProcess.GetCurrDir ();
/** *//** 系统默认设置的目录文件 */
String fileName = "/blogAudit/Incoming/300CommentsKeywordsPossible.txt";
/** *//** 完整的文件访问路径 */
String fullFileName = dir + fileName;
File myFile = new File(fullFileName);
if(!myFile.exists())
...{
System.err.println("Can't Find " + fullFileName);
}
try
...{
BufferedReader in = new BufferedReader(new FileReader(myFile));
String str;
while ((str = in.readLine()) != null)
...{
// |为分隔符
int divPos = str.lastIndexOf("|");
try
...{
if(maxDouble < Double.parseDouble(str.substring(divPos+1,str.length())))
...{
// 最大值
maxDouble = Double.parseDouble(str.substring(divPos+1,str.length()));
}
if(minDouble > Double.parseDouble(str.substring(divPos+1,str.length())))
...{
// 最小值
minDouble = Double.parseDouble(str.substring(divPos+1,str.length()));
}
// 添加词及对应的概率
KeywordProbality.put(str.substring(0,divPos),str.substring(divPos+1,str.length()));
}
catch(Exception e)
...{
e.getStackTrace();
}
}
in.close();
}
catch (IOException e)
...{
e.getStackTrace();
}
}
/**//*
* 获取文件夹里的文本文件
*/
public static void getFile(String dirPath)
...{
/** *//** 建立当前目录中文件的File对象 */
File filesDir = new File(dirPath);
/** *//** 取得代表目录中所有文件的File对象数组 */
File list[] = filesDir.listFiles();
/** *//** 总文件数 */
System.out.println (list.length);
for(int i=0;i<list.length;i++)
...{
if(list[i].isFile())
...{
System.out.println(list[i].getPath ());
/** *//** i表示现在的words表的起始序号 读取内容*/
readContent(list[i].getPath (),i);
}
}
}
/**//*
* 读取内容
*/
public static void readContent(String filePath, int articleNum)
...{
String strContent = null;
try
...{
BufferedReader in = new BufferedReader(new FileReader(filePath));
String strTemp = null;
while ((strTemp = in.readLine()) != null)
...{
strContent = strContent + strTemp;
}
in.close();
}
catch (IOException e)
...{
e.getStackTrace();
}
// 对内容进行分词
divLexical(strContent,articleNum);
}
/**//*
* 调用ICTCLAS进行分词
*/
public static void divLexical(String strContent,int articleNum)
...{
ICTCLAS ictclas = new ICTCLAS();
if(!ictclas.init (0,2))
...{
ictclas.init (0,2);
}
String strTrans=ictclas.paragraphProcess(strContent);
// 对分词后的字符进行处理
filterString(strTrans,articleNum);
}
/**//*
* 对分词后的字符进行处理,并计算最终的概率值,进行判断
*/
public static void filterString(String srcStr,int articleNum)
...{
/** *//** 使用trim去掉前后多余空格防止发生意外 */
String strTemp=srcStr.trim();
String tempText = "";
double probality = 1.0;
StringTokenizer st = new StringTokenizer(strTemp," ");
int len = st.countTokens();
int i = 0;
while(i<len)
...{
tempText = st.nextToken();
i++;
// 获取该词对应的概率
double keywordProbality = divStr(tempText);
...{
probality = probality * keywordProbality;
// 用2个参数分别表示上溢出和下溢出值
// 溢出处理
// 一旦遇到上溢出 表明其中含有不良信息 即可停止计算
if(probality < minDouble)
...{
// 继续往下计算
probality = 1.0;
}
}
}
System.out.println (probality);
if(probality >1.0)
...{
// 不良记录个数加1
appearCount++;
}
}
/**//*
* 获取词对应的先验概率
*/
public static double divStr(String srcStr)
...{
StringTokenizer st = new StringTokenizer(srcStr,"/");
double probality = 1.0;
int len = st.countTokens();
if(len == 2)
...{
String strPre = st.nextToken ();
// 获取该词的先验概率
try
...{
probality = Double.parseDouble(KeywordProbality.get(strPre).toString());
}
catch(Exception e)
...{
e.getMessage();
}
}
return probality;
}
}
- Bayes过滤方法之一
- Naive Bayes 文档过滤
- BAYES
- bayes
- bayes
- Bayes
- 文件过滤驱动之一
- 朴素贝叶斯方法(Naive Bayes)
- 文件过滤驱动之一 - rageliu
- 朴素贝叶斯方法(Naive Bayes)原理和实现
- Windows驱动_文件系统过滤驱动之一
- 关键字过滤方法
- 过滤SQL关键字方法
- 拦截器方法过滤
- MAC 过滤的方法
- 关键字过滤方法
- 过滤Html代码方法
- ext store 过滤方法
- 要促进中国资本市场又好又快发展
- 八大因素影响个人职业生涯
- 昨天喝酒遇到一个东北老乡,教了我不少。
- ADO.NET2.0新特性-异步查询
- 全面剖析FLASH在线大头贴
- Bayes过滤方法之一
- 采样保持电路
- ubuntu下codeblocks起步(八)
- 处理队列空
- 黑四剁
- Asp.net页面传值(3种方法)
- 利用C++模板,代替虚函数,实现类的静态多态性(加入性能测试部分)
- 搜集好用好玩的classid
- java多线程设计模式(4) wait/notify机制