使用java将搜狗词库.scel文件转化为.txt文件

<span style="font-family: Arial, Helvetica, sans-serif;">需求:批量将.scel文件转化为可视的txt文件(支持1对1,多对1,多对多),并从中提取中文词(去重),支持追加内容。</span>

成果:

   




使用:

package com.hxl.files;

import java.io.IOException;

public class Demo {
	
	public static void main(String[] args) {
		//单个scel文件转化  
		FileProcessing scel=new SougouScelFileProcessing();
		scel.parseFile("/Users/ST_iOS/Desktop/test/ciku/89个节日.scel", "/Users/ST_iOS/Desktop/test/ciku/txt/89个节日.txt", true);

		//多个scel文件转化为一个txt (格式:拼音字母 词)
		try {
			scel.parseFiles("/Users/ST_iOS/Desktop/test/ciku", "/Users/ST_iOS/Desktop/test/ciku/txt/汇总.txt", false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//多个scel文件转化为多个txt文件
		scel.setTargetDir("/Users/ST_iOS/Desktop/test/ciku/多对多");//转化后文件的存储位置
		scel.parseFile("/Users/ST_iOS/Desktop/test/ciku",false);
		
	}
}

以上代码是将.scel文件转化为.txt文件的代码,效果类似于图1和图2的,如果要提取中文词,将scel=new TxtFileProcessing();调用一样的方法,即可实现txt词库文件提取中文词。

注意事项:

SougouScelFileProcessing 解析的源文件必须为.scel文件类型,解析后的文件必须为.txt文件类型,参数以绝对路径进行传递

TxtFileProcessing   解析的源文件必须为.txt,格式如图2(拼音 中文),解析后的文件必须为.txt文件类型,参数以绝对路径进行传递


源码:一共4个class,需要的朋友可自行拷入项目中使用,代码解释的也挺清晰的,就不多说了。

1、FileProcessing


package com.hxl.files;

import java.io.File;
import java.io.IOException;

public abstract class FileProcessing {
	// 解析后存放的文件夹
	protected String targetDir;

	/**
	 * 解析单个文件
	 * 
	 * @param filePath
	 *            要解析的源文件路径
	 * @param targetFilePath
	 *            解析后的文件路径
	 * @param isAppend
	 *            是否为内容追加,不追加则会覆盖内容
	 */
	public abstract void parseFile(String filePath, String targetFilePath, boolean isAppend);

	/**
	 * 合并解析多个文件
	 * 
	 * @param fileDirPath
	 *            要解析的源文件夹路径
	 * @param targetFilePath
	 *            解析后的文件路径
	 * @param isAppend
	 *            是否为内容追加,不追加则会覆盖内容
	 * @throws IOException 
	 */
	public abstract void parseFiles(String fileDirPath, String targetFilePath, boolean isAppend) throws IOException;

	/**
	 * 解析单个或者多个文件,如果是多个文件则生成对应的txt文件,{@link #setTargetDir(String)},
	 * 如果targetDir不设置,则在当前文件夹下生成相应的txt文件
	 * 
	 * @param filePath
	 *            源文件路径
	 * @param isAppend
	 *            false:覆盖内容 true:附加内容
	 */
	public abstract void parseFile(String filePath, boolean isAppend);
	
	/**
	 * 创建文件夹
	 * 
	 * @param filePath
	 *            目标文件
	 * @return
	 */
	protected void createParentDir(String targetFilePath) {
		if (!targetFilePath.endsWith(".txt")) {
			throw new IllegalStateException("文件格式错误,后缀必须为.txt,此格式为   " + targetFilePath);
		}
		String path = targetFilePath.substring(0, targetFilePath.lastIndexOf("/") + 1);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
	}
	
	/**
	 * 解析单个文件
	 * @param filePath   文件路径
	 */
	public void parseFile(String filePath){
		parseFile(filePath,false);
	}

	public String getTargetDir() {
		return targetDir;
	}

	/**
	 * 解析后的txt文件存放路径
	 * 
	 * @param targetDir
	 *           
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值