您的位置:首页 > 其它

中文文本分词,关键词提取工具jcseg使用方法

2016-11-05 00:00 633 查看
jcseg可以从文章或者语句中提取出关键词.并且能把中文数字自动转换成阿拉伯数字

效果如下:



目录结构:



实现:

注意:此处的jcsegTaskConfig 和 ADictionary主要用来加载词库和其它配置文件,是线程安全的,正式环境中如果有高并发量的调用此方法时应该提取到静态代码块中.否则会出现内存溢出异常.

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

import org.lionsoul.jcseg.ASegment;
import org.lionsoul.jcseg.core.ADictionary;
import org.lionsoul.jcseg.core.DictionaryFactory;
import org.lionsoul.jcseg.core.IWord;
import org.lionsoul.jcseg.core.JcsegException;
import org.lionsoul.jcseg.core.JcsegTaskConfig;
import org.lionsoul.jcseg.core.SegmentFactory;

public class TestFenci {
public static void main(String[] args) throws IOException, JcsegException {

// 创建JcsegTaskConfig分词任务实例
// 即从jcseg.properties配置文件中初始化的配置
//在1.8.3版之后已经在jcseg-core-1.9.2.jar包中放入了默认的配置文件,此处的properties路径可以为空
//如果需要自己配置,也可以在写好配置文件后替换jar包中的配置文件
JcsegTaskConfig config = new JcsegTaskConfig("config/jcseg.properties");
// config.setAppendCJKPinyin(true);
// 创建默认词库(即: com.webssky.jcseg.Dictionary对象)
// 并且依据给定的JcsegTaskConfig配置实例自主完成词库的加载
ADictionary dic = DictionaryFactory.createDefaultDictionary(config,
true);

dic.loadFromLexiconFile("D:/fenci/jcseg-1.9.2/lexicon/lex-main.lex");// 这个路径是jcseg-1.9.4-src-jar-dict.zip
// 这个jar
// 包的
// 存放路径,
// 你自己找lexicon
// 文件夹下的
// lex-main.lex

// JcsegTaskConfig.COMPLEX_MODE表示创建ComplexSeg复杂ISegment分词对象
// JcsegTaskConfig.SIMPLE_MODE表示创建SimpleSeg简易Isegmengt分词对象.
ASegment seg = (ASegment) SegmentFactory.createJcseg(
JcsegTaskConfig.SIMPLE_MODE, new Object[] { config, dic });
// 设置要分词的内容
String str = "出租车一百元";
seg.reset(new StringReader(str));
// 获取分词结果
IWord word = null;
while ((word = seg.next()) != null) {
TestFenci fenci = new TestFenci();
String o = word.getValue();
System.out.print(o + "|");
//			boolean b = fenci.tes(o);
//			if (b) {
//				 System.out.println("餐饮类");
//				 return;
//			}

}
}

public boolean tes(String i) throws FileNotFoundException {
BufferedReader reader = null;
String tempString = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/tt/5.txt")), "utf-8"));
// 一次读一行,读入null时文件结束
while ((tempString = reader.readLine()) != null) {
if(tempString.contains(i)){
return true;
}
}
reader.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return false;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分词