Stanford Segment 使用笔记
2016-03-04 01:20
162 查看
斯坦福分词器对搜狗简化版语料库分词处理步骤:
1、读取语料库
2、对文件逐个分词
3、每个文件分词完的同时存盘到指定路径
1、读取语料库
2、对文件逐个分词
3、每个文件分词完的同时存盘到指定路径
package com.Seg; import java.io.*; import java.util.List; import java.util.Properties; import edu.stanford.nlp.ie.crf.CRFClassifier; import edu.stanford.nlp.io.IOUtils; import edu.stanford.nlp.ling.CoreLabel; /** * This Class is for Segment the SG words * @author Bill */ public class SGSegmenter { /** * 获取data文件夹内的内容 */ private static final String basedir = System.getProperty("SGSegmenter", "data"); /** * 批文件分词函数 * @param CRFClassifier<CoreLabel> segmenter 用于调试分词器 * @param String rfilepath 包含子文件夹的路径,需要读取文件的路径 * @param String wfilepath 写入文件的文件夹根路径 */ public boolean SegmenterFiles(CRFClassifier<CoreLabel> segmenter, String rfilepath, String wfilepath) { // 预定义操作文本需要用到的变量 // 用来存储txt中的内容 String content = null; // 用来存储一个完成的分词文本 List<String> segmented = null; // 用来更新写入的文件名 int num = 0; // 获取所有文件文件(搜狗语料库文件夹) File file = new File(rfilepath); String[] fileListA = file.list(); // 第一次循环获得包含txt的所有文件夹 for(String subFilePath : fileListA) { File file1 = new File(rfilepath + "/" + subFilePath); String[] fileListB = file1.list(); // 第二次循环获得子文件夹下的所有txt文件 for(String txtpath : fileListB) { num ++; File file2 = new File(rfilepath + "/" + subFilePath + "/" + txtpath); // 读取内容 content = IOUtils.stringFromFile(file2.getPath(), "GBK"); segmented = segmenter.segmentString(content); // 将分词好的内容写入新的文件夹 String newpath = wfilepath + "/" + num + ".txt"; try { PrintWriter pw = new PrintWriter(new FileWriter(newpath)); // 写入 for(int i = 0; i < segmented.size(); i++) { pw.print(segmented.get(i) + " "); } // 关闭写流 pw.flush(); pw.close(); } catch(IOException e) { e.printStackTrace(); } } } return true; } /** * 分词main函数 */ public static void main(String args[]) throws Exception{ // 设置utf-8输出格式 System.setOut(new PrintStream(System.out, true, "utf-8")); // 不太理解 Properties props = new Properties(); props.setProperty("sighanCorporaDict", basedir); // 一下内容是接入分词器必须的内容 props.setProperty("serDictionary", basedir + "/dict-chris6.ser.gz"); if(args.length > 0) { props.setProperty("testFile", args[0]); } props.setProperty("inputEncoding", "UTF-8"); props.setProperty("sighanPostProcessing", "true"); // 设置分词器 CRFClassifier<CoreLabel> segmenter = new CRFClassifier<>(props); segmenter.loadClassifierNoExceptions(basedir + "/ctb.gz", props); // segmenter.loadClassifierNoExceptions(basedir + "/pku.gz", props); // 不太理解 for(String filename : args) { segmenter.classifyAndWriteAnswers(filename); } // 设置输入 String sample = "我的名字叫杜宇晨,我需要对搜狗语料库进行分词,使用的是斯坦福分词器。adsjkfhajkfadskf"; List<String> segmented = segmenter.segmentString(sample); System.out.println(segmented); SGSegmenter sgs = new SGSegmenter(); sgs.SegmenterFiles(segmenter, "D:/Installers/程序开发/文本分类/SogouC.reduced.20061127精简版/SogouC.reduced/Reduced", "D:/Installers/程序开发/文本分类/已分词的搜狗语料库"); /** * @test * 以下内容读取一个语料文档,看看结果 */ /* String filename = "D:/Installers/程序开发/文本分类/SogouC.reduced.20061127精简版/SogouC.reduced/Reduced/C000008/10.txt"; String filename2 = "D:/Installers/程序开发/文本分类/已分词的搜狗语料库/1.txt"; String content = null; content = IOUtils.stringFromFile(filename, "GBK"); System.out.println(content); segmented = segmenter.segmentString(content); System.out.println(segmented); // 写文件 PrintWriter pw = null; pw = new PrintWriter(new FileWriter(filename2)); for(int i = 0; i < segmented.size(); i++) { pw.print(segmented.get(i) + " "); } // 关闭写流 pw.flush(); pw.close(); */ /** * @test * 获取一个文件夹内所有文件名称 */ /* File file = new File("D:/Installers/程序开发/文本分类/SogouC.reduced.20061127精简版/SogouC.reduced/Reduced/C000008"); String[] fileList = file.list(); for(String str : fileList) { System.out.println(str); } */ } }
相关文章推荐
- 整数划分
- HDU-5349
- AndroidStudio中如何打开hierarchyviewer
- Find the Duplicate Number
- 支付宝账户提现的银行账户已经被占用了怎么办
- 学习新东西的唯一方法
- 旋转数组的最小数字
- 康子KAKA 程序猿毕业后荒唐的两年点滴
- CSS实现Loading加载动画
- Mac快捷键
- Elasticsearch v2.2 快速入门(含curl,Sense,python 3种客户端方式)
- cocos2dx Action 特效
- 支付宝认证银行卡丢失了,怎么办
- android 图片转 Base64
- i++与++i的误解
- SQLAlchemy 学习笔记
- 软件工程与个人项目的结合
- CSS学习(六)-css圆角边框高级效果、边框阴影效果(上)
- 推荐几款实用的Android Studio 插件
- read me of del.sh