毕业设计数据查找优化代码之一
2007-10-02 22:38
405 查看
/*
* DivWords.java
*
* 创建于 2007年10月2日, 下午9:16
*
* @author 马如林
*
*/
package com.mrl;
import java.io.*;
import java.util.*;
import com.xjt.nlp.word.ICTCLAS;
import com.mrl.FileProcess;
/**
*
* @author rulinma
*/
public class DivWords
{
/** 测试分类文件夹 */
private static String FILE_DIR ="D:/blogTestSet";
/** 分词存放文本 */
private static String DIV_WORDS ="D:/divWords";
/** 主程序入口 */
public static void main(String[] args)
{
FileProcess fileProcess = new FileProcess();
/** 获取所在文件夹*/
getFile(FILE_DIR);
}
/*
* 根据文件目录读取整个文件夹文件,同时currId表示数据库当前有的文章数
*/
private static void getFile(String dirPath)
{
/** 建立当前目录中文件的File对象 */
File filesDir = new File(dirPath);
/** 取得代表目录中所有文件的File对象数组 */
File list[] = filesDir.listFiles();
for(int i=0; i<list.length; i++)
{
if(list[i].isFile())
{
readContent(list[i].getPath ());
}
}
}
/*
* 根据文件路径读取文章内容
*/
private static void readContent(String filePath)
{
int i = 0;
String strContent = null;
try
{
BufferedReader in = new BufferedReader(new FileReader(filePath));
String strTemp = null;
while ((strTemp = in.readLine()) != null)
{
strContent = strContent + strTemp;
i++;
}
in.close();
}
catch (IOException e)
{
e.getStackTrace();
}
// 调用分词处理
divLexical(strContent);
}
/*
* 内容分词处理
*/
private static void divLexical(String strContent)
{
/** 调用ICTCLAS进行分词 */
ICTCLAS ictclas = new ICTCLAS();
if(!ictclas.init (0,2))
{
ictclas.init (0,2);
}
String strTrans=ictclas.paragraphProcess(strContent);
filterString(strTrans);
}
/*
* 过滤字符串 比如中国/北京 天安门/。,过滤为 中国/北京 天安门/。两个串
*/
private static void filterString(String srcStr)
{
/** 使用trim去掉前后多余空格防止发生意外 */
String strTemp = srcStr.trim();
String tempText = "";
StringTokenizer st = new StringTokenizer(strTemp," ");
int len = st.countTokens();
for(int i=0; i<len; i++)
{
tempText = st.nextToken();
divStr(tempText);
}
}
/*
* 对连续的字符串过滤,比如中国/北京,过滤为 中国 北京 两个词
*/
private static void divStr(String srcStr)
{
StringTokenizer st = new StringTokenizer(srcStr,"/");
int len = st.countTokens();
if(len == 2)
{
/** 前一个词 */
String strPre = st.nextToken ();
System.out.println(strPre);
try
{
/** 在文件中添加词 */
BufferedWriter out = new BufferedWriter(new FileWriter(DIV_WORDS,true));
out.write(strPre);
out.newLine();
out.close();
}
catch (IOException e)
{
}
}
}
}
* DivWords.java
*
* 创建于 2007年10月2日, 下午9:16
*
* @author 马如林
*
*/
package com.mrl;
import java.io.*;
import java.util.*;
import com.xjt.nlp.word.ICTCLAS;
import com.mrl.FileProcess;
/**
*
* @author rulinma
*/
public class DivWords
{
/** 测试分类文件夹 */
private static String FILE_DIR ="D:/blogTestSet";
/** 分词存放文本 */
private static String DIV_WORDS ="D:/divWords";
/** 主程序入口 */
public static void main(String[] args)
{
FileProcess fileProcess = new FileProcess();
/** 获取所在文件夹*/
getFile(FILE_DIR);
}
/*
* 根据文件目录读取整个文件夹文件,同时currId表示数据库当前有的文章数
*/
private static void getFile(String dirPath)
{
/** 建立当前目录中文件的File对象 */
File filesDir = new File(dirPath);
/** 取得代表目录中所有文件的File对象数组 */
File list[] = filesDir.listFiles();
for(int i=0; i<list.length; i++)
{
if(list[i].isFile())
{
readContent(list[i].getPath ());
}
}
}
/*
* 根据文件路径读取文章内容
*/
private static void readContent(String filePath)
{
int i = 0;
String strContent = null;
try
{
BufferedReader in = new BufferedReader(new FileReader(filePath));
String strTemp = null;
while ((strTemp = in.readLine()) != null)
{
strContent = strContent + strTemp;
i++;
}
in.close();
}
catch (IOException e)
{
e.getStackTrace();
}
// 调用分词处理
divLexical(strContent);
}
/*
* 内容分词处理
*/
private static void divLexical(String strContent)
{
/** 调用ICTCLAS进行分词 */
ICTCLAS ictclas = new ICTCLAS();
if(!ictclas.init (0,2))
{
ictclas.init (0,2);
}
String strTrans=ictclas.paragraphProcess(strContent);
filterString(strTrans);
}
/*
* 过滤字符串 比如中国/北京 天安门/。,过滤为 中国/北京 天安门/。两个串
*/
private static void filterString(String srcStr)
{
/** 使用trim去掉前后多余空格防止发生意外 */
String strTemp = srcStr.trim();
String tempText = "";
StringTokenizer st = new StringTokenizer(strTemp," ");
int len = st.countTokens();
for(int i=0; i<len; i++)
{
tempText = st.nextToken();
divStr(tempText);
}
}
/*
* 对连续的字符串过滤,比如中国/北京,过滤为 中国 北京 两个词
*/
private static void divStr(String srcStr)
{
StringTokenizer st = new StringTokenizer(srcStr,"/");
int len = st.countTokens();
if(len == 2)
{
/** 前一个词 */
String strPre = st.nextToken ();
System.out.println(strPre);
try
{
/** 在文件中添加词 */
BufferedWriter out = new BufferedWriter(new FileWriter(DIV_WORDS,true));
out.write(strPre);
out.newLine();
out.close();
}
catch (IOException e)
{
}
}
}
}
相关文章推荐
- 代码优化--数据字典(dictionary)的使用
- 人脸检测之MTCNN训练自己的数据(部分代码公开!请关注置顶的MTCNN算法优化!)
- 分治思想 - 二分搜索技术 - MATLAB代码 list是数据集合,x是要查找的数据。
- 代码优化从数据库里查数据
- 如何优化js代码(4)――减少作用域链上的查找次数
- IOS开发学习笔记028-UITableView单组数据显示代码优化
- 运用 ADO.NET 对象优化数据查询代码
- Tensorflow深度学习入门——采用卷积和池化优化训练MNIST数据——代码+注释
- STM32:STM32学习记录1:MDK基本数据类型及代码优化
- mysql unix准换时间格式查找指定日期数据代码
- Android之ListView性能优化——一行代码绑定数据——万能适配器
- 看数据结构写代码(53) 静态查找表(线性查找,二分查找,斐波那契查找,插值查找)
- 代码优化从数据库里查数据
- 运用 ADO.NET 对象优化数据查询代码
- 2016/3/26 weixin 头像 昵称 网页优化显示 缺表中数据 只有代码 无显示效果
- C++代码,数据结构-次优查找树
- 对查找进行优化实例代码:
- 优化Python代码使其加快作用域内的查找
- 运用 ADO.NET 对象优化数据查询代码
- 【数据结构与算法】【查找】哈希表的代码实现