中文分词之候选集的选取
来源:互联网 发布:mac分区不动了 编辑:程序博客网 时间:2024/05/18 02:43
中文分词之候选集的选取
一、 基本思路:
·0. 之前讲到利用字典的分词,是因为字典的词组是有限的,单个的。但是候选词是可以由多个词组成。例如“Android/x 系统 /n 平台/n”,“蓝/n 牙/n”等。如果把这些词是连着出现在被爬虫爬下来的文章中,那么就很有必要把“Android系统 平台”,“蓝牙”作为整体的词语来理解了。即“把x n n”,”n n”组合在一起作为候选词,然后在后面会通过“候选集过滤”,“左右完整性分析”和“稳定性”来评估这个候选词是否有资格成为对象词。
1. 首先定义rule.txt,里面定义好抽取的规则
n
n n
v
n v
v n
ng n p
n n n
rzv q n
x n n
xn
规则来自 《中文产品评价对象的识别研究》
2. 构造词组的正则表达式
思路:读取txt文本,每行地读出内容,然后构造正则表达式,存放在List中去。其中中文字母数字的正则表达式是:String regex = "[\u4E00-\u9FA5a-zA-Z0-9]*";
程序实现:
/** * 获取抽取规则并放在list * @param inputPath * @return * @throws Exception */public static List<String> getRulesFromFile(String inputPath) throws Exception{List<String> results = new ArrayList<String>();File inputFile = new File(inputPath);if(!inputFile.exists()){throw new Exception("inputPath is no exists");}FileReader fr = new FileReader(inputFile);BufferedReader br = new BufferedReader(fr);try {String regex = "[\u4E00-\u9FA5a-zA-Z0-9]*";String temp;while((temp = br.readLine()) != null){String[] worlds = temp.split(" ");String build_regx ="";for(int i =0;i<worlds.length;i++){build_regx += regex +ConstantString.slash+worlds[i]+" ";}System.out.println(build_regx);results.add(build_regx);}} catch (Exception e) {e.printStackTrace();}finally{try {if(br != null){br.close();}if(fr != null){fr.close();}} catch (Exception e2) {e2.printStackTrace();}}return results;}
结果:
3. 利用构造好的正则表达式,递归匹配已经分词的文本内容,并输出到文档。
public static void ExtratorWorld2File(String rulesPath,String inputPath,String outputPath) throws Exception{if(StringUtil.isEmpty(inputPath) || StringUtil.isEmpty(rulesPath)){throw new Exception("inputPath/rulesPath is null");}File file = new File(outputPath);if(!file.exists()){file.mkdirs();}ArrayList<String> rules = (ArrayList<String>) ExtratorWordUtil.getRulesFromFile(rulesPath);ArrayList<String> paths = new StringUtil().getAllPath(inputPath);for(String path : paths){String result ="";String content = StringUtil.getContent(path);String name = StringUtil.getNameFromPath(path);String outputFile = outputPath + ConstantString.slash + name + ConstantString.postText;for(String rule : rules){result += StringUtil.getContentUseRegex(rule, content, 0,ConstantString.WIN_NextLine) +ConstantString.WIN_NextLine;}StringUtil.String2File(result, outputFile);}}
结果:
0 0
- 中文分词之候选集的选取
- 中文分词之过滤候选词以及去重
- 中文分词选取-依概率选取
- 中文分词选取-依概率选取
- 中文分词之基本分词
- 我的复习笔记之中文分词
- SEO的关键之中文分词
- Solr之中文分词
- 中文分词选取-不成词个数判断法
- 中文分词选取-不成词个数判断法
- 什么是中文分词-中文分词的应用
- 自然语言处理--中文分词之机械分词
- 统计分词/无字典分词学习(4):候选片段的过滤方法,互信息过滤
- 统计分词/无字典分词学习(5):候选片段的过滤方法,边界稳定性
- TSE的中文分词
- 中文分词的分类
- ElasticSearch的中文分词
- 中文分词测试集
- IOS多线程系统学习之多线程的优势
- Android内核学习之Binder
- C语言笔记
- 几种常见排序算法的时间复杂度空间复杂度稳定性汇总表
- 最近邻搜索之乘积量化(Product Quantizer)理解(一)
- 中文分词之候选集的选取
- 数学与编程——统计与编程
- JAVA-迭代子模式简单实现
- 迭代器 iterator->second含义
- 几款强力压缩打包软件
- 理解MyGUI3.2.0及其Samples的运行实质
- android studio 更换包名
- 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
- 随笔