java读取中文分词工具(二)



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

/*
 * 文件格式:已分词的中文文本,每个词语空格分割,每行一个段落。
 * 这个类适合读取每行数量较少的文本,比如分好段落的文本,一个段落一行存储。
 * 读取一行,步长为1,返回词组。不会跨段落生成词组。
 * 两种模式:
 * 1 读到文件末尾,结束
 * 2 读到文件末尾,从头再来
 */
public class WordReader 
{
	static final int normalMode = 0;
	static final int againMode = 1;
	int currentMode = 0;
	
	BufferedReader br=null;
	ArrayList<String> paraWords = null;
	
	StringTokenizer tokenizer;
	int currentPara = 0;
	int paraPos = 0;
	public  WordReader(String fileName) throws IOException
	{
		File file=new File(fileName);
		br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
		br.mark((int)file.length()+1);
		paraWords = new ArrayList<String>();
	}	
	
	private boolean readPara() throws IOException
	{		
		//if(currentPara>614005+10) return false;
		String line = br.readLine();
		if(line == null)//到文件末尾了
		{
			if(currentMode == normalMode)
			{
				return false;
			}
			else 
			{				
				br.reset();//从头再来
				return readPara();
			}			
		}
		paraWords.clear();	
		tokenizer= new StringTokenizer(line," ");
		while(tokenizer.hasMoreTokens())
		{
			paraWords.add(tokenizer.nextToken());
		}	
		currentPara++;
		paraPos = 0;
		return true;	
	}
	
	public String[] getNextWords(int count) throws IOException
	{
		if(paraPos+count >= paraWords.size())//到了段落末尾,读取新的段落
		{
			if(readPara())
				return getNextWords(count);
			else return null;
		}
		String[] words = new String[count];
		for(int i=0;i<count;i++)
		{
			words[i] = paraWords.get(paraPos+i);
		}
		paraPos++;
		return words;
	}
		
	public static void main(String[] args) throws IOException 
	{
		// TODO Auto-generated method stub
		WordReader wordReader = new WordReader("/home/linger/sources/ParaModel/electronic_seg.txt");
		//wordReader.currentMode = WordReader.againMode;
		while(true)//614005行
		{
			String[] words = wordReader.getNextWords(5);
			if(words == null) break;
			System.out.println(words[0]);
		}
		System.out.println(wordReader.currentPara);

	}

}


发现bufferreader也是可以移动流位置的,利用mark和reset。

但是如果文件太大,调用mark时容易出错,也不知道为啥。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
word分词是一个Java实现的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义。 能准确识别英文、数字,以及日期、时间等数量词,能识别人名、地名、组织机构名等未登录词。 同时提供了Lucene、Solr、ElasticSearch插件。 分词使用方法: 1、快速体验 运行项目根目录下的脚本demo-word.bat可以快速体验分词效果 用法: command [text] [input] [output] 命令command的可选值为:demo、text、file demo text 杨尚川是APDPlat应用级产品开发平台的作者 file d:/text.txt d:/word.txt exit 2、对文本进行分词 移除停用词:List words = WordSegmenter.seg("杨尚川是APDPlat应用级产品开发平台的作者"); 保留停用词:List words = WordSegmenter.segWithStopWords("杨尚川是APDPlat应用级产品开发平台的作者"); System.out.println(words); 输出: 移除停用词:[杨尚川, apdplat, 应用级, 产品, 开发平台, 作者] 保留停用词:[杨尚川, 是, apdplat, 应用级, 产品, 开发平台, 的, 作者] 3、对文件进行分词 String input = "d:/text.txt"; String output = "d:/word.txt"; 移除停用词:WordSegmenter.seg(new File(input), new File(output)); 保留停用词:WordSegmenter.segWithStopWords(new File(input), new File(output)); 4、自定义配置文件 默认配置文件为类路径下的word.conf,打包在word-x.x.jar中 自定义配置文件为类路径下的word.local.conf,需要用户自己提供 如果自定义配置和默认配置相同,自定义配置会覆盖默认配置 配置文件编码为UTF-8 5、自定义用户词库 自定义用户词库为一个或多个文件夹或文件,可以使用绝对路径或相对路径 用户词库由多个词典文件组成,文件编码为UTF-8 词典文件的格式为文本文件,一行代表一个词 可以通过系统属性或配置文件的方式来指定路径,多个路径之间用逗号分隔开 类路径下的词典文件,需要在相对路径前加入前缀classpath: 指定方式有三种: 指定方式一,编程指定(高优先级): WordConfTools.set("dic.path", "classpath:dic.txt,d:/custom_dic"); DictionaryFactory.reload();//更改词典路径之后,重新加载词典 指定方式Java虚拟机启动参数(中优先级): java -Ddic.path=classpath:dic.txt,d:/custom_dic 指定方式三,配置文件指定(低优先级): 使用类路径下的文件word.local.conf来指定配置信息 dic.path=classpath:dic.txt,d:/custom_dic 如未指定,则默认使用类路径下的dic.txt词典文件 6、自定义停用词词库 使用方式和自定义用户词库类似,配置项为: stopwords.path=classpath:stopwords.txt,d:/custom_stopwords_dic 7、自动检测词库变化 可以自动检测自定义用户词库和自定义停用词词库的变化 包含类路径下的文件和文件夹、非类路径下的绝对路径和相对路径 如: classpath:dic.txt,classpath:custom_dic_dir, d:/dic_more.txt,d:/DIC_DIR,D:/DIC2_DIR,my_dic_dir,my_dic_file.txt classpath:stopwords.txt,classpath:custom_stopwords_dic_dir, d:/stopwords_more.txt,d:/STOPWORDS_DIR,d:/STOPWORDS2_DIR,stopwords_dir,remove.txt 8、显式指定分词算法 对文本进行分词时,可显式指定特定的分词算法,如: WordSegmenter.seg("APDPlat应用级产品开发平台", SegmentationA

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值