使用GIZA++进行词对齐

运行于Windows中的GIZA++( GIZA++ working inWindows Platform) 摘 要:Linux环境中,大部分基于短语的统计机器翻译系统可以很方便的利用GIZA++训练得出的词语对齐结果。然而,对于许多习惯在Windows系统中从事科学研究的科研工作者却享受不到这种便利。虽然我们可以使用Cygwin或者虚拟机在Windows系统下模拟Linux系统,但是有时这样来回的切换很不方便。本文主要是想和大家分享一下我们如何在Windows系统中使用GIZA++进行词语对齐的一些经验 阅读详情

GIZA++的github地址:https://github.com/moses-smt/giza-pp

准备双语语料

zh.txt:源语言

海洋 是 一个 非常 复杂 的 事物 。
人类 的 健康 也 是 一 件 非常 复杂 的 事情 。
将 两者 统一 起来 看 起来 是 一 件 艰巨 的 任务 。 但 我 想 要 试图 去 说明 的 是 即使 是 如此 复杂 的 情况 , 也 存在 一些 我 认为 简单 的 话题 , 一些 如果 我们 能 理解 , 就 很 容易 向前 发展 的 话题 。
这些 简单 的 话题 确实 不 是 有关 那 复杂 的 科学 有 了 怎样 的 发展 , 而是 一些 我们 都 恰好 知道 的 事情 。
接下来 我 就 来说 一个 。 如果 老 妈 不 高兴 了 , 大家 都 别 想 开心 。

en.txt:目标语言

It can be a very complicated thing , the ocean . 
And it can be a very complicated thing , what human health is .
And bringing those two together might seem a very daunting task , but what I 'm going to try to say is that even in that complexity , there 's some simple themes that I think , if we understand , we can really move forward .
And those simple themes aren 't really themes about the complex science of what 's going on , but things that we all pretty well know .
And I 'm going to start with this one : If momma ain 't happy , ain 't nobody happy .

注意UTF-8编码。

下载编译GIZA++

$ git clone https://github.com/moses-smt/giza-pp.git
$ cd giza-pp
$ make

编译完会在GIZA++-v2/和mkcls-v2/目录下生成以下可执行文件:
plain2snt.out、snt2cooc.out、GIZA++、
mkcls

将这四个程序移动到工作目录workspace下:

运行命令进行词对齐

文本单词编号

./plain2snt.out zh.txt en.txt

得到en.vcb、zh.vcb、en_zh.snt、zh_en.snt四个文件

en.vcb / zh.vcb:字典文件,id : token : count

2 海洋 1
3 是 6
4 一个 2
5 非常 2
6 复杂 4
7 的 12
8 事物 1
9 。 7
10 人类 1
...

en_zh.snt / zh_en.snt:编号表示句对,第一行表示句对出现次数

1
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10 11 12 13
1
10 7 11 12 3 13 14 5 6 7 15 9
14 15 4 5 6 7 8 9 10 16 17 18 19 13
...

生成共现文件

./snt2cooc.out zh.vcb en.vcb zh_en.snt > zh_en.cooc
./snt2cooc.out en.vcb zh.vcb en_zh.snt > en_zh.cooc

 zh_en.cooc / en_zh.cooc

0 33
0 34
0 35
0 36
0 37
0 38
0 39
0 40
...

生成词类

./mkcls -pzh.txt -Vzh.vcb.classes opt
./mkcls -pen.txt -Ven.vcb.classes opt

***** 1 runs. (algorithm:TA)*****
;KategProblem:cats: 100   words: 68

start-costs: MEAN: 262.907 (262.907-262.907)  SIGMA:0
  end-costs: MEAN: 190.591 (190.591-190.591)  SIGMA:0
   start-pp: MEAN: 3.52623 (3.52623-3.52623)  SIGMA:0
     end-pp: MEAN: 1.95873 (1.95873-1.95873)  SIGMA:0
 iterations: MEAN: 50117 (50117-50117)  SIGMA:0
       time: MEAN: 1.468 (1.468-1.468)  SIGMA:0

 

参数:
-c 词类数目
-n 优化次数,默认是1,越大越好
-p 输入文件
-V 输出文件
opt 优化输出

en.vcb.classes / zh.vcb.classes:单词所属类别编号

,       26
.       28
:       64
And     29
I       13
If      52
It      49
a       34
about   22
...

en.vcb.classes.cats / zh.vcb.classes.cats:类别所拥有的一组单词

0:$,
1:
2:science,
3:seem,
4:things,
5:some,
6:start,
7:task,
...

 

GIZA++

先在当前目录新建两个输出文件夹z2e、e2z,否则下面的程序会出错,没有输出。

$ ./GIZA++ -S zh.vcb -T en.vcb -C zh_en.snt -CoocurrenceFile zh_en.cooc -o z2e -OutputPath z2e
$ ./GIZA++ -S en.vcb -T zh.vcb -C en_zh.snt -CoocurrenceFile en_zh.cooc -o e2z -OutputPath e2z

参数:
-o 文件前缀
-OutputPath 输出所有文件到文件夹

输出文件详解(以z2e为例):

z2e.perp 困惑度

#trnsz  tstsz   iter    model   trn-pp          test-pp         trn-vit-pp              tst-vit-pp
5       0       0       Model1  80.5872         N/A             2250.77         N/A
5       0       1       Model1  36.0705         N/A             648.066         N/A
5       0       2       Model1  34.0664         N/A             523.575         N/A
5       0       3       Model1  32.628          N/A             423.928         N/A
5       0       4       Model1  31.5709         N/A             359.343         N/A
5       0       5       HMM     30.7896         N/A             314.58          N/A
5       0       6       HMM     31.1412         N/A             172.128         N/A
5       0       7       HMM     26.1343         N/A             111.444         N/A
5       0       8       HMM     22.177          N/A             79.3055         N/A
5       0       9       HMM     19.0506         N/A             58.4415         N/A
5       0       10      THTo3   32.6538         N/A             37.8575         N/A
5       0       11      Model3  11.1194         N/A             11.944          N/A
5       0       12      Model3  8.93033         N/A             9.50349         N/A
5       0       13      Model3  7.68766         N/A             8.19622         N/A
5       0       14      Model3  6.64154         N/A             7.04977         N/A
5       0       15      T3To4   6.17993         N/A             6.55567         N/A
5       0       16      Model4  6.16858         N/A             6.4715          N/A
5       0       17      Model4  6.0819          N/A             6.39317         N/A
5       0       18      Model4  6.04302         N/A             6.34387         N/A
5       0       19      Model4  5.95066         N/A             6.2234          N/A

z2e.a3.final:i j l m p(i/j, l, m):i代表源语言Token位置;j代表目标语言Token位置;l代表源语言句子长度;m代表目标语言句子长度;p(i/j, l, m)代表the probability that a source word in position i is moved to position j in a pair of sentences of length l and m。

1 1 8 100 1
5 2 8 100 1
4 3 8 100 1
2 4 8 100 1
5 5 8 100 1
4 6 8 100 1
4 7 8 100 1
0 8 8 100 1
...

z2e.d3.final:类似于z2e.a3.final文件,只是交换了i 和 j 的位置。

2 0 100 8 0.0491948
6 0 100 8 0.950805
3 1 100 8 1
5 2 100 8 1
4 3 100 8 1
2 4 100 8 0.175424
5 4 100 8 0.824576
2 5 100 8 1
4 6 100 8 1
4 7 100 8 1
...

z2e.n3.final:source_id p0 p1 p2 … pn;源语言Token的Fertility分别为0,1,…,n时的概率表,比如p0是Fertility为0时的概率。

2 1.22234e-05 0.781188 0.218799 0 0 0 0 0 0 0
3 0.723068 0.223864 0 0.053068 0 0 0 0 0 0
4 0.349668 0.439519 0.0423205 0.168493 0 0 0 0 0 0
5 0.457435 0.447043 0.0955223 0 0 0 0 0 0 0
6 0.214326 0.737912 0 0.0477612 0 0 0 0 0 0
7 1 0 0 0 0 0 0 0 0 0
8 1.48673e-05 0.784501 0.215484 0 0 0 0 0 0 0
...

z2e.t3.final:s_id t_id p(t_id/s_id); IBM Model 3训练后的翻译表;p(t_id/s_id)表示源语言Token翻译为目标语言Token的概率

0 3 0.196945
0 7 0.74039
0 33 0.0626657
2 4 1
3 6 1
4 5 1
5 3 0.822024
5 6 0.177976
6 3 0.593075
...

z2e.A3.final 单向对齐文件,数字代表Token所在句子位置(1为起点)

# Sentence pair (1) source length 8 target length 11 alignment score : 8.99868e-08
It can be a very complicated thing , the ocean .
NULL ({ 8 }) 海洋 ({ 1 }) 是 ({ 4 }) 一个 ({ 9 }) 非常 ({ 3 6 7 }) 复杂 ({ 2 5 }) 的 ({ }) 事物 ({ 10 }) 。 ({ 11 })

# Sentence pair (2) source length 12 target length 14 alignment score : 9.55938e-12
And it can be a very complicated thing , what human health is .
NULL ({ 9 }) 人类 ({ 2 11 }) 的 ({ }) 健康 ({ 12 }) 也 ({ }) 是 ({ 5 }) 一 ({ }) 件 ({ 13 }) 非常 ({ 4 7 8 }) 复杂 ({ 3 6 }) 的 ({ }) 事情 ({ 1 10 }) 。 ({ 14 })
...

z2e.d4.final:IBM Model 4 翻译表

# Translation tables for Model 4 .
# Table for head of cept.
F: 20 E: 26
SUM: 0.125337
9 0.125337

F: 20 E: 15
SUM: 0.0387214
-2 0.0387214

F: 20 E: 24
SUM: 0.0387214
21 0.0387214
...

z2e.D4.final:IBM Model 4的Distortion表

26 20 9 1
15 20 -2 1
24 20 21 1
2 20 -2 1
40 20 -4 1
22 20 -3 0.0841064
22 20 9 0.915894
32 20 28 1
21 20 24 1
29 2 -3 0.472234
29 2 1 0.527766
5 2 1 0.475592
...

z2e.gizacfg:GIZA++配置文件,超参数

adbackoff 0
c zh_en.snt
compactadtable 1
compactalignmentformat 0
coocurrencefile zh_en.cooc
corpusfile zh_en.snt
countcutoff 1e-06
countcutoffal 1e-05
countincreasecutoff 1e-06
countincreasecutoffal 1e-05
d
deficientdistortionforemptyword 0
depm4 76
depm5 68
dictionary
dopeggingyn 0
emalignmentdependencies 2
emalsmooth 0.2
emprobforempty 0.4
emsmoothhmm 2
hmmdumpfrequency 0
hmmiterations 5
l z2e/118-03-20.215009.gld.log
log 0
logfile z2e/118-03-20.215009.gld.log
m1 5
m2 0
m3 5
m4 5
m5 0
m5p0 -1
m6 0
manlexfactor1 0
manlexfactor2 0
manlexmaxmultiplicity 20
maxfertility 10
maxsentencelength 101
mh 5
mincountincrease 1e-07
ml 101
model1dumpfrequency 0
model1iterations 5
model23smoothfactor 0
model2dumpfrequency 0
model2iterations 0
model345dumpfrequency 0
model3dumpfrequency 0
model3iterations 5
model4iterations 5
model4smoothfactor 0.2
model5iterations 0
model5smoothfactor 0.1
model6iterations 0
nbestalignments 0
nodumps 0
nofiledumpsyn 0
noiterationsmodel1 5
noiterationsmodel2 0
noiterationsmodel3 5
noiterationsmodel4 5
noiterationsmodel5 0
noiterationsmodel6 0
nsmooth 64
nsmoothgeneral 0
numberofiterationsforhmmalignmentmodel 5
o z2e/z2e
onlyaldumps 0
outputfileprefix z2e/z2e
outputpath z2e/
p 0
p0 -1
peggedcutoff 0.03
pegging 0
probcutoff 1e-07
probsmooth 1e-07
readtableprefix
s zh.vcb
sourcevocabularyfile zh.vcb
t en.vcb
t1 0
t2 0
t2to3 0
t3 0
t345 0
targetvocabularyfile en.vcb
tc
testcorpusfile
th 0
transferdumpfrequency 0
v 0
verbose 0
verbosesentence -10

z2e.Decoder.config:用于ISI Rewrite Decoder解码器

# Template for Configuration File for the Rewrite Decoder
# Syntax:
#         <Variable> = <value>
#         '#' is the comment character
#================================================================
#================================================================
# LANGUAGE MODEL FILE
# The full path and file name of the language model file:
LanguageModelFile =
#================================================================
#================================================================
# TRANSLATION MODEL FILES
# The directory where the translation model tables as created
# by Giza are located:
#
# Notes: - All translation model "source" files are assumed to be in
#          TM_RawDataDir, the binaries will be put in TM_BinDataDir
#
#        - Attention: RELATIVE PATH NAMES DO NOT WORK!!!
#
#        - Absolute paths (file name starts with /) will override
#          the default directory.

TM_RawDataDir = z2e/
TM_BinDataDir = z2e/

# file names of the TM tables
# Notes:
# 1. TTable and InversTTable are expected to use word IDs not
#    strings (Giza produces both, whereby the *.actual.* files
#    use strings and are THE WRONG CHOICE.
# 2. FZeroWords, on the other hand, is a simple list of strings
#    with one word per line. This file is typically edited
#    manually. Hoeever, this one listed here is generated by GIZA

TTable = z2e.t3.final
InverseTTable = z2e.ti.final
NTable = z2e.n3.final
D3Table = z2e.d3.final
D4Table = z2e.D4.final
PZero = z2e.p0_3.final
Source.vcb = zh.vcb
Target.vcb = en.vcb
Source.classes = zh.vcb.classes
Target.classes = en.vcb.classes
FZeroWords       = z2e.fe0_3.final

下面两个Python文件地址:GitHub

词对齐对称化

上面的得到的*.A3.final文件是单向对齐的,我们这里需要对称化,对称化方法有很多,我们这里使用最流行的“grow-diag-final-and”方法

python align_sym.py e2z.A3.final z2e.A3.final > aligned.grow-diag-final-and

 

1-1 2-4 3-1 3-9 4-3 4-6 4-7 5-2 5-5 7-10 8-11
1-2 1-11 1-12 3-2 4-13 5-5 6-13 7-13 8-4 8-7 8-8 9-3 9-6 9-10 11-1 12-14
1-2 2-2 2-25 3-2 3-26 4-2 4-11 5-36 6-6 6-29 7-8 8-22 9-22 10-2 12-7 12-21 12-42 12-46 13-1 14-23 15-15 15-19 16-16 16-20 17-25 18-29 19-24 19-31 20-6 23-5 23-30 24-8 25-5 25-10 26-9 26-14 28-4 29-3 30-9 31-5 31-43 32-3 33-35 34-36 34-45 35-33 37-13 37-44 38-17 39-3 40-16 40-18 41-30 41-34 42-41 43-5 44-17 45-15 45-16 45-17 46-24 47-29 47-38 48-27 48-39 48-40 49-32 51-31 52-47
1-5 1-23 1-25 2-4 2-8 4-7 4-19 4-22 5-9 6-6 8-5 9-26 10-14 12-9 13-13 14-6 14-20 15-5 15-17 16-15 17-3 18-2 18-16 19-10 20-2 21-15 21-21 22-6 23-10 24-11 24-12 24-13 24-24 26-1 27-27
1-15 2-2 3-2 3-3 3-4 3-5 4-12 5-7 6-1 6-21 7-20 8-19 8-20 9-8 10-14 10-18 11-7 11-9 11-10 11-11 12-14 13-4 14-13 15-14 16-6 16-13 17-3 18-6 18-17 19-21

词对齐可视化

将第一句词对齐结果进行可视化

python align_plot.py en.txt zh.txt aligned.grow-diag-final-and 0

 

利用GIZA++和Moses生成双向对齐文件 踩了两周的坑今天终于解决了,本来觉得晚上关于GIZA++生成对齐文件的教程已经很多了没准备写,但是因为我的这个坑我不得不记录一下。利用GIZA++获得单向对齐文件的同时会获得双语词表,但是我从始至终都没看到哪里有什么双语词表。 然后研究了三四天的moses,关于moses的整体步骤分为以下几步: Prepare data Run GIZA++ Align words ... 阅读详情

相关推荐

GIZA++实现了IBM模型1~5 IBM Model 1详解

GIZA++实现了IBM模型1~5的所有代码,在生成源语言与目标语言之间翻译概率的同时,也产生相应的副产品——“词对齐”。这个副产品成为各种统计机器翻译系统的基石,直到今天,大部分系统仍然首先使用GIZA++对大量平行语料进行词对齐。在阿拉伯语、中文等语言与印欧语系语言的对齐中错误率仍然很高。特别是中文,错误率常常达到30%以上。 这里主要对GIZA++开源代码中的IBM model1实现部

u013736042的专栏 9463

GIZA++的使用

安装很简单,直接在根目录make就可以。 建一个tool目录,将GIZA++-v2内的plain2snt.out,snt2cooc.out,GIZA++复制到tool中,再将mkcls-v2下的mkcls也复制进去,同时把chinese_segment.txt和english_segment.txt也放进去。 接下来,执行下面的指令来完成将普通文本转化为 GIZA++ 格式 ./plain2s...

bonjourdeutsch的博客 2247

NLP - GIZA++ 实现词对齐

关于 GIZA++ 安装 使用 1、将平行语料转化为 GIZA++ 格式(plain2snt.out) 2、获取共现文件(snt2cooc.out) 3、创建 mkcls 文件(mkcls) 4、运行GIZA++

AI工程化、开源分享、文档翻译、代码笔记 1544

使用GIZA++进行平行语料的词对齐

使用GIZA++进行平行语料的词对齐   在使用moses搭建机器翻译系统时,词对齐的过程是和模型训练结合在一起的,GIZA++运行的很多细节都被跳过。本文是将GIZA++单独拿出来,关注其从编译、运行到最终获取对齐文件各阶段的细节问题,以求更加深刻地理解GIZA++的工作原理。   1、 编译安装GIZA++ 最新版本的GIZA++可以从下列链接中找到http://code.go

michenggang_dami的专栏 4601

NLP - 词对齐 MGIZA++

关于 MGIZA++ 安装 命令说明 mkcls d4norm hmmnorm plain2snt snt2cooc snt2coocrmp snt2plain symal mgiza

AI工程化、开源分享、文档翻译、代码笔记 781

Statiscal Machine Translation机器翻译_CodingPark编程公园

本文主要讲述了Statiscal Machine Translation的基本步骤

CodingPark 编程公园 3543

利用开源工具搭一套汉英翻译系统(二):词对齐

训练语料是句子级别对齐的,词对齐工具用来从句子对齐中学习到词对齐,对齐这个概念可以理解成为两个词的翻译对应关系。理论知识不再介绍,感兴趣的看《统计机器翻译》 对齐工具有多个,这里选择使用GIZA++, 下载与编译 下载地址:github 下载完,解压后的路径结构: 其实,这里包含了两个工具,一个是GIZA++,一个是mkcls。make一下,编译。我的编译环境是:gcc versio

zhangjcsd的专栏 4941

MGIZA++-开源

mgiza现在已移至github https://github.com/moses-smt/mgiza

USING GIZA++ TO OBTAIN WORD ALIGNMENT BETWEEN BILINGUAL SENTENCES

USING GIZA++ TO OBTAIN WORD ALIGNMENT BETWEEN BILINGUAL SENTENCES PROBLEM Word alignment is mapping of words between two sentences that have the same meaning in two different languages. Let's say we...

活到老、学到老 347

NLP技术中的词语对齐实现模块 GIZA++在windows上的移植实现

NLP技术中的词语对齐实现模块 GIZA++在windows上的移植实现!用VS2003.net编译,并在编译前给VS2003安装好STLport-4.6.2库,才能编译运行成功! 注:STLport-4.6.2库,我将在本资源的下一站上传给大家,已经编译好的适合VS2003的STLport-4.6.2库。

giza++用作词语对齐,在自然语言处理中有广泛应用

giza++用作词语对齐,在自然语言处理如机器翻译中有广泛应用

GIZA++词对齐运行实验

http://hi.baidu.com/maydaygmail/blog/item/cf666a7b92ae871429388a36.html

676

giza++及模型代码解析

该资源主要用于机器翻译,解决多语言翻译间的词对齐,具体包含: 1.giza-pp-v1.0.7.tar.gz 2.giza模型代码解析

机器翻译重要过程(3)---抽取短语

上一步通过GIZA++进行词语对齐,我们得到了对应的中英文词对,在此基础上我们就可以进行第三个重要过程了,即抽取短语。短语抽取是短语翻译表构造的第一步,而短语翻译表是翻译系统解码器的要使用到的最重要组件之一,所以抽取短语这一步意义非常大。 示例:        上图展示了从含有词对齐信息的双语平行句对(上方图所示)中抽取的短语对(中下方的“示例短语”所示)。从图中可以看出,理想

寒小阳 8542

基于YOLOv8的多类别检测模型融合实战:从数据集重构到模型训练

本文详细介绍了如何将多个单一类别的YOLOv8检测模型融合为一个高效的多类别检测模型。通过重构数据集、利用预训练模型生成新标签,并重新训练,解决了多模型并行带来的速度慢、资源占用高和部署复杂等问题。实战流程涵盖数据准备、标签生成、模型训练与评估,旨在帮助开发者构建性能优异的YOLOv8多类别检测模型。

kmeans3miner的博客 227

kaggle项目:纽约出租车行程时间NYC Taxi Trip Duration

纽约出租车形成时间预测(NYC Taxi Trip Duration)是Kaggle中的一个Playground竞赛。比赛数据集基于2016年纽约市黄色出租车行程记录数据,需要建立一个模型,根据单个行程属性,预测测试集中每次行程的持续时间。Kaggle项目模型XGBoost,评价指标RMSLE。

weixin_42113929的博客 2790

Lesson 4 基于统计的翻译系统

基于统计的翻译系统构建步骤(以 中->英 翻译为例): step1:对文本进行预处理,如:分词,去除停用词…,最终形成“平行语料”用于training; step2:词对齐:利用giza++ 形成 两个“词对齐文件”:1)中->英,2)英->中,并利用“词对齐对称化算法”将两个词对齐文件进行合并,从而形成最后的词对齐文件。词对齐具体格式如下图: 单向词对齐: 双向词对齐: ...

u014765410的博客 230

[渗透测试实战篇-4] RCE漏洞组合拳突破Getshell全解析

本文深入解析RCE漏洞组合拳在渗透测试中的实战应用,通过SSRF、XXE等漏洞串联技术突破Getshell的全过程。文章详细介绍了Java和PHP环境下的漏洞利用差异,以及防御绕过与权限维持的实用技巧,为安全研究人员提供全面的实战指导。

weixin_29211309的博客 350
上一篇: Greedy search与beam search
下一篇: Python中的generator详解
csdnofzyk
博客等级 码龄12年 14粉丝 5原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值