`

LongSentenceFilter Joshua SMT [2]

阅读更多

Note that the first version of LongSentenceFilter is not complete, because even after filtering there still may be French sentences of more than 100 words. Now this version tackles this problem. Note also that this version is not optimal from implementational view and a better version will be in next post.

package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

public class LongSentenceFilter {

	public void filter(File enFile, File frFile, File oenFile, File ofrFile) {

		/*go through both English and French files, 
		 * remove sentences of more than 100 words in one file 
		 * and remove corresponding sentences (with same sentence number) 
		 * in the other file 
		 * */
		
		BufferedReader enBufferedReader;
		BufferedReader frBufferedReader;
		String line = null;
		int lineCount = 0;

		// record line numbers of those sentences that consist more than 100
		// words either in English file or in French file
		ArrayList<Integer> longSentenceIndices = new ArrayList<Integer>();

		// output stringbuffer
		StringBuffer enContent = new StringBuffer();
		StringBuffer frContent = new StringBuffer();

		try {
			// go through English file, find those sentences of more than
			// 100 words and keep record of those line numbers in
			// _longSentenceIndices
			enBufferedReader = new BufferedReader(new FileReader(enFile));
			while ((line = enBufferedReader.readLine()) != null) {
				String[] words = line.split(" ");
				lineCount++;
				if (words.length > 100)
					longSentenceIndices.add(lineCount);
			}
			System.out.println("Number of sentences in original document: "
					+ lineCount);

			// go through French file, keep those sentences, of words less 
			// or equal to 100 and whose line numbers are not in
			// _longSentenceIndices
			// at the same time, keep line numbers of sentences of more than
			// 100 words in _longSentenceIndices
			lineCount = 0;
			frBufferedReader = new BufferedReader(new FileReader(frFile));
			while ((line = frBufferedReader.readLine()) != null) {
				String[] words = line.split(" ");
				lineCount++;
				if (words.length <= 100
						&& !longSentenceIndices.contains(lineCount)) {
					frContent.append(line);
					frContent.append('\n');
				} else {
					if (!longSentenceIndices.contains(lineCount))
						longSentenceIndices.add(lineCount);
				}
			}
			
			// go again through English file, keep those sentences, whose line 
			// number are not in _longSentenceIndices
			lineCount = 0;
			int newLineCount = 0;
			enBufferedReader = new BufferedReader(new FileReader(enFile));
			while ((line = enBufferedReader.readLine()) != null) {
				lineCount++;
				if (!longSentenceIndices.contains(lineCount)) {
					newLineCount++;
					enContent.append(line);
					enContent.append('\n');
				}
			}
			System.out.println("Number of sentences after filteration: "
					+ newLineCount);
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// write stringbuffer to output files
		Writer output;
		try {
			output = new BufferedWriter(new FileWriter(oenFile));
			output.write(enContent.toString());
			output.close();

			output = new BufferedWriter(new FileWriter(ofrFile));
			output.write(frContent.toString());
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		LongSentenceFilter filter = new LongSentenceFilter();
		// English input, output
		File enFile = new File("test/input/hansard.5.en.tok.lc");
		File oenFile = new File("test/output/hansard.5.en.tok.lc.filtered");
		// French input, output
		File frFile = new File("test/input/hansard.5.fr.tok.lc");
		File ofrFile = new File("test/output/hansard.5.fr.tok.lc.filtered");

		// Note that _enFile and _frFile should be translation of each other!
		filter.filter(enFile, frFile, oenFile, ofrFile);
	}

}
 

 

分享到:
评论

相关推荐

    [重构与模式].(美国)Joshua.Kerievsky.清晰中文版 part2

    Joshua Kerievsky写的一本和GOF的设计模式、Martin Fowler重构-改善即有代码的设计齐名的著作! 因上传权限问题,本书压成了二部分,此为第2部分

    Google首席工程师Joshua Bloch谈如何设计优秀的API

    2. **简洁性**:API应尽可能简洁,避免过多的复杂性。每个方法和类都应有明确的责任,遵循“单一职责原则”。同时,尽量减少API的公开表面,以减少潜在的错误使用和维护负担。 3. **一致性**:API的设计应当保持...

    [重构与模式].(美国)Joshua.Kerievsky part2

    本书开创性地深入揭示了重构与模式这两种软件开发关键技术之间的联系,说明了通过重构实现模式改善既有的设计,往往优于在新的设计早期使用模式。本书不仅展示了一种应用模式和重构的创新方法,而且有助于读者结合...

    SPncfpdf_鞍点近似_Joshua_

    标题“SPncfpdf_鞍点近似_Joshua_”和描述中的“鞍点近似From:Joshua Carmichael”暗示了这个压缩包文件内容与一个名为“SPncfpdf”的工具或算法有关,该工具或算法可能用于求解鞍点问题。鞍点在数学和计算机科学中...

    Addison Wesley - Refactoring to Patterns (Joshua Kerievsky)

    重構技術的書,雖然是04年的書,值得大家一看

    读书笔记:Effective Java 中文版(第2版)总结 (美)Joshua Bloch 著.zip

    读书笔记:Effective Java 中文版(第2版)总结 (美)Joshua Bloch 著

    [重构与模式].(美国)Joshua.Kerievsky.清晰版.part2

    [重构与模式].(美国)Joshua.Kerievsky.清晰版.part2 完整的pdf,总共 2 部

    Refactoring to patterns - Joshua Kerievsky

    《Refactoring to Patterns》是Joshua Kerievsky所著的一本专业书籍,主要内容涉及软件设计中的重构和模式应用。本书详细介绍了通过一系列低级重构来安全地将设计向特定模式实现移动、靠近或远离的方法。Kerievsky以...

    joshua-springboot.zip

    这个名为"joshua-springboot.zip"的压缩包包含了一个基于SpringBoot的入门示例项目,该项目可能包含了处理基本HTTP请求、与MySQL数据库交互以及集成Redis缓存等功能。 首先,让我们深入理解SpringBoot的核心特性。...

    joshua_import:Joshua Project数据导入到本地JSON

    LIMIT是每次通过Joshua Project API时要获取的行数,1-1000,默认值: 1000 INDIVIDUAL如果已定义),则为返回的每个人员组行输出单独的JSON文件,默认值: false MINIMIZED如果定义,输出.min.json文件,否则...

    1_Effective_Java_2nd_Edition_proglib_java_Joshua_

    《Effective Java》是Java编程领域的一本经典著作,由知名程序员和计算机科学家Joshua Bloch撰写。这本书的第二版深入探讨了如何编写高效、可维护的Java代码,并提供了许多实用的编程指南和最佳实践。以下是根据标题...

    智能开关新技术项目程序文件.rar_Joshua_stm8_无线智能开关_自发电_自发电开关

    2. **无线技术**:无线智能开关使用无线通信技术,如蓝牙、Wi-Fi或Zigbee,使得用户可以远程控制开关,不受物理距离限制。这增加了便利性,尤其适用于智能家居系统,用户可以通过智能手机或其他设备轻松控制家中的...

    Joshua~Designs-开源

    标题“Joshua~Designs-开源”表明这是一个与开源软件项目相关的集合,由一个名为“Joshua”的开发者或团队创建。这个项目包含了多种不同的应用程序,包括访客留言簿、内容管理系统(CMS)、论坛、FTP客户端、SSH...

    Joshua-Chang.github.io

    标题 "Joshua-Chang.github.io" 暗示这是一个个人网页项目,可能托管在GitHub上。GitHub Pages允许用户免费托管静态网站,而".github.io"是GitHub为用户提供个人域名的一种方式。这个项目的仓库名与标题相同,这通常...

    Joshua Bloch访谈录.doc

    Joshua Bloch和Neal Gafter在加入Google之前,都在Sun Microsystems担任资深工程师,对Java核心和类库开发有着显著贡献。他们共同撰写了《Java Puzzlers》一书,而Bloch还著有广受欢迎的《Effective Java》。他们的...

    java-puzzlers-sampler_java_Joshua_

    《Java Puzzlers Sampler》是由Java领域的权威专家Joshua Bloch和Neal Gafter共同编著的一本书,书中收录了一系列令人困惑、出乎意料的Java编程问题,旨在帮助开发者更好地理解和避免这些潜在的陷阱。书中的...

    joshua

    盖茨比极简启动器 :rocket:快速开始 创建一个Gatsby网站。 使用Gatsby CLI创建一个新站点,并指定最小的启动器。 # create a new Gatsby site using the minimal starter npm init gatsby 开始开发。...

    [重构与模式].(美国)Joshua.Kerievsky.清晰版 pdf (1)

    [重构与模式].(美国)Joshua.Kerievsky.清晰版 pdf (1) 全部pdf,共 2 卷

    [重构与模式].(美国)Joshua.Kerievsky.清晰中文版 part1

    Joshua Kerievsky写的一本和GOF的设计模式、Martin Fowler重构-改善即有代码的设计齐名的著作! 因上传权限问题,本书压成了二部分,此为第1部分

    2018+Neo4j+中国聚会+-+Neo4j+因果集群【Joshua+Yu】

    2018+Neo4j+中国聚会+-+Neo4j+因果集群【Joshua + Yu】

Global site tag (gtag.js) - Google Analytics