中文分词之评价基本分词
一、 大纲步骤:
评价对象的抽取分为:基本分词,候选词的获取,特殊词的过滤,完整性算法,稳定性算法
二、 基本分词
先利用IC对文章进行基本的分词,分词的依据是IC本身自带的词典。该词典只有普通分词,例如/n ,/v, /q,/ns,/w等。
import java.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
import java.io.FileOutputStream;
importjava.io.OutputStream;
import java.io.OutputStreamWriter;
importjava.util.ArrayList;
importcom.myretrieve.config.ConstantString;
importICTCLAS.I3S.AC.ICTCLAS50;
public classIctclasUtils {
/**
* 根据字符串进行分词,返回分词的结果。
* @param str
* @return
*/
public static StringictclasSpilt(String str){
String result = null;
try
{
ICTCLAS50 testICTCLAS50 = newICTCLAS50();
String argu = ".";
//初始化
if(testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")) == false)
{
System.out.println("InitFail!");
return result;
}
//设置词性标注集(0 计算所二级标注集,1 计算所一级标注集,2 北大二级标注集,3 北大一级标注集)
testICTCLAS50.ICTCLAS_SetPOSmap(2);
//导入用户词典前分词
bytenativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(str.getBytes("GB2312"), 0, 1);//分词处理
result = newString(nativeBytes, 0, nativeBytes.length, "GB2312");
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
/**
* 循环读出路径下所有的文件,然后分词,写入到输出路径中
* @paraminputPath
* @paramoutputPath
* @throwsException
*/
public static voidspiltWord2File(String inputPath,String outputPath) throwsException{
if(StringUtil.isEmpty(inputPath)){
throw newException("the inputPath is NULL");
};
File outputDir = newFile(outputPath);
if(!outputDir.exists()){
outputDir.mkdirs();
}
OutputStream out;
BufferedWriter bw = null;
ArrayList<String> fileLists = newStringUtil().getAllPath(inputPath);
for(String p: fileLists ){
String content = StringUtil.getContent(p);
String result = ictclasSpilt(content);
System.out.println(result);
String name = StringUtil.getNameFromPath(p);
String outputFile =outputPath+ConstantString.spilePath+name+ConstantString.postText;
StringUtil.String2File(result,outputFile);
}
}
}
分词前:
分词后: