用Python API实现对具有子字单位的罕见词的神经机器翻译的C++实现。
fastBPE的Python项目详细描述
快速业务伙伴关系
C++实现的Neural Machine Translation of Rare Words with Subword Units,带有python API。
安装
编译时使用:
g++ -std=c++11 -pthread -O3 fastBPE/main.cc -IfastBPE -o fast
用法:
列出命令
./fast
usage: fastbpe <command> <args>
The commands supported by fastBPE are:
getvocab input1 [input2] extract the vocabulary from one or two text files
learnbpe nCodes input1 [input2] learn BPE codes from one or two text files
applybpe output input codes [vocab] apply BPE codes to a text file
applybpe_stream codes [vocab] apply BPE codes to stdin and outputs to stdout
fastbpe还支持stdin输入。例如,这两个命令是等效的:
./fast getvocab text > vocab
cat text | ./fast getvocab - > vocab
但是第一个将内存映射输入文件以有效地读取它,在非常大的文件上比stdin快两倍以上。类似地,这两个命令是等价的:
./fast applybpe output input codes vocab
cat input | ./fast applybpe_stream codes vocab > output
尽管第一种方法在大型数据集上会更快,因为它使用多线程来预计算输入文件中所有单词的bpe拆分。
学习代码
./fast learnbpe 40000 train.de train.en > codes
对列车应用代码
./fast applybpe train.de.40000 train.de codes
./fast applybpe train.en.40000 train.en codes
获取列车词汇
./fast getvocab train.de.40000 > vocab.de.40000
./fast getvocab train.en.40000 > vocab.en.40000
对有效和测试应用代码
./fast applybpe valid.de.40000 valid.de codes vocab.de.40000
./fast applybpe valid.en.40000 valid.en codes vocab.en.40000
./fast applybpe test.de.40000 test.de codes vocab.de.40000
./fast applybpe test.en.40000 test.en codes vocab.en.40000
python api
要安装python api,只需运行:
python setup.py install
注意:对于mac osx用户,在执行上述安装命令之前/期间,视情况将export MACOSX_DEPLOYMENT_TARGET=10.x
(x=9或10,取决于您的版本)或-stdlib=libc++
添加到extra_compile_args
的setup.py
中。
使用:
importfastBPEbpe=fastBPE.fastBPE(codes_path,vocab_path)bpe.apply(["Roasted barramundi fish","Centrally managed over a client-server architecture"])>>['Ro@@ asted barr@@ am@@ un@@ di fish','Centr@@ ally managed over a cli@@ ent-@@ server architecture']