从http://code.google.com/p/berkeleyaligner/
下载主干代码后,我将项目添加到Eclipse的构建路径中。然后,使用下面的代码,我可以提取从sourceFile和targetFile中读取的每个句子对的对齐方式。
对齐后,如何从BerkeleyAligner读取Alignment
类型?
import edu.berkeley.nlp.wa.mt.Alignment;
import edu.berkeley.nlp.wa.mt.SentencePair;
import edu.berkeley.nlp.wordAlignment.combine.WordAlignerCombined;
public static void main(String[] args) {
BufferedReader brSrc = new BufferedReader(new FileReader ("sourceFile"));
BufferedReader brTrg = new BufferedReader(new FileReader ("targetFile"));
while ((currentSrcLine = brSrc.readLine()) !=null) {
String currentTrgLine = brTrg.readline();
// Reads into BerkeleyAligner SentencePair format.
SentencePair src2trg = new SentencePair(sentCounter, params.get("source"),
Arrays.asList(srcLine.split(" ")), Arrays.asList(trgLine.split(" ")));
// Generate Alignment type from SentencePair
WordAlignerCombined aligner;
Alignment alignedPair = aligner.alignSentencePair(src2trg);
// How do i print out the Alignment???
}
}
例如源文件:
this is the first line in the textfile.
that is the second line.
foo bar likes to eat bar foo.
例如targetFile:
Dies ist die erste Textzeile in der Datei.
das ist die zweite Zeile.
foo bar gerne bar foo essen.
最佳答案
打印GIZA。 Alignment为此提供了一种方法:
public void writeGIZA(PrintWriter out, int idx)
GIZA是:
"# sentence pair (%d) source length %d target length %d alignment score : 0\n"
"NULL ({ %s })"
" %s ({ %s })" (englishSentence.get(i), StrUtils.join(alignments))
idx
只是句子对ID。out
就是您要打印它的地方。