python - 如何在 nltk 中预测 IBM 模型的翻译(解码)?
问题描述
我对 NLTK API 还很陌生,想开始使用简单的 IBM Model 1,它进行字级翻译。这是我到目前为止所做的,在NLTK API 文档的帮助下。
from nltk.translate.ibm1 import IBMModel1
from nltk.translate import AlignedSent
def get_text(filename):
senteces = []
with open(filename,'r') as f:
for sentence in f:
sentences.append(sentence.split())
return sentences
src_sentences = get_text('source.txt')
trg_sentences = get_text('target.txt')
bitext = []
for i in range(len(src_sentences)):
bitext.append(AlignedSent(src_sentences[i], trg_sentences[i]))
ibm1 = IBMModel1(bitext, 5)
现在IBMModel1
已经创建了,我不确定如何执行解码,即。从测试集中预测翻译。我似乎也无法在文档中找到它。
简单来说,我想获得一个预测的翻译,给定一个使用这个模型的随机源句子。我如何实现这一目标?
解决方案
请参阅https://www.nltk.org/api/nltk.translate.ibm1.html#nltk.translate.ibm1.IBMModel1上的这些示例
>>> bitext = []
>>> bitext.append(AlignedSent(['klein', 'ist', 'das', 'haus'], ['the', 'house', 'is', 'small']))
>>> bitext.append(AlignedSent(['das', 'haus', 'ist', 'ja', 'groß'], ['the', 'house', 'is', 'big']))
>>> bitext.append(AlignedSent(['das', 'buch', 'ist', 'ja', 'klein'], ['the', 'book', 'is', 'small']))
>>> bitext.append(AlignedSent(['das', 'haus'], ['the', 'house']))
>>> bitext.append(AlignedSent(['das', 'buch'], ['the', 'book']))
>>> bitext.append(AlignedSent(['ein', 'buch'], ['a', 'book']))
>>> ibm1 = IBMModel1(bitext, 5)
>>> print(ibm1.translation_table['buch']['book'])
推荐阅读
- c - 如何扫描直到我得到不同形式的东西
- python - a question about "randoms and my question"
- javascript - JS/jquery 根据被点击元素的 id 获取可变文本
- angular - 动态创建组件呈现为同级
- sql - MODIFY COLUMN 会重置语句中未指定的属性吗?
- python - 与移动用户代理一起使用时,无法使用 python selenium 单击网页元素
- javascript - jQuery从事件中获取标记
- java - 如何读取 XML 文件?
- haskell - 使用 Ord 创建和比较空列表
- amazon-dynamodb - 在 Alexa Skill 上更新 Item DynamoDB 表时出现问题