版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Handoking/article/details/83346084
原代码下载地址
优化的分词系统代码
以下是核心代码
完整代码可以直接运行分词,点我跳转
public class NLPIRTest {
private List<String> files = new ArrayList<>(); //创建数组files
//getAllFiles函数来获得文件的绝对路径
private void getAllFiles(File filePath) {
File[] fsFiles = filePath.listFiles();//获得filepath的绝对路径
assert fsFiles != null;
for (File f : fsFiles) {
if (f.isFile() && !f.getName().equals(".DS_Store")) files.add(f.getPath());//获得文件的路径
if (f.isDirectory()) this.getAllFiles(f);//递归调用,将文件的绝对路径存入数组files中
}
}
//segLines方法用来规范读写文件
private List<String> segLines(File file) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
List<String> temp = new ArrayList<>();
String str;
while ((str = bf.readLine()) != null) {
//String str2 = str + "\r\n";
temp.add(str);
}
bf.close();
return temp;
}
//excludeStopWord来去除停用词
private void excludeStopWord(File file) throws Exception {
//省去了变量和数组的定义
for (String merBer:splitWordArray){
String[] splitString = merBer.split("\t");
conString.setLength(0);
for (int i=0;i<splitString.length;i++){
if (stopWord.contains(splitString[i])){
splitString[i] = null;
}
if (splitString[i]!=null) conString.append(splitString[i]).append(" ");
}
bw.write(conString.toString());
bw.newLine();
}
bw.close();
}
public static void main(String[] args) throws Exception {
NLPIR.init("lib");
NLPIR.importUserDict("F:\\split_word\\dict_comment1.txt");
//调用用户词典
//分词调用见完整代码
}