[英]How to change the word order of phrasal verbs in a POS-tagged corpus file
我有一个带有POS标签的并行语料库文本文件,在该文件中我想对单词进行重新排序,以便“可分离的短语动词粒子”出现在短语动词的“动词”旁边(“组成计划”而不是“制定计划”)。 用于统计机器翻译系统中的预处理。 以下是带有POS标签的文本文件中的一些示例行:
我想将所有质点(在示例中:out_RP,off_RP,out_RP,down_RP)移到最接近的前一个动词(即与质点组合在一起的动词组成短语动词)的旁边。 更改单词顺序后,各行的外观如下:
到目前为止,我已经尝试使用python和正则表达式通过使用re.findall对问题进行排序:
import re
file=open('first100k.txt').read()
matchline3='\w*_VB.?\s\w*_DT\s\w*_NN\s\w*_RP'
wordorder1=re.findall(matchline3,file)
print wordorder1
这将找到单词顺序为1的所有短语动词(请参见下文),但这是我所能做到的,因为我不知道如何将质点移动到动词旁边。 任何想法如何正确解决此问题(不一定使用python和regex)? 我希望能够搜索所有短语动词,并按以下单词顺序移动质点:
(使用的标记来自Penn Treebank标记集( http://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html))(x表示可选字符,以便包括所有动词形式,并且*表示通配符))
* _ VBx + * _DT + * _。JJ + * _ NNS + * _ RP
* _VBx + * _ PRP $ + * _ NN + * _ RP
* _ VBx + * _PRP $ + * _。JJ + * _ NNS + * _ RP
* _VBx + * _ NNP + * _ RP
* _VBx + * _ JJ + * _ NNP + * _ RP
* _VBx + * _ NNPS + * _ RP
* _VBx + * _ PRP + * _ RP
预先感谢您的帮助!
我不建议在这里使用正则表达式。 绝对不像在空格上分割后在每一行上迭代,可能重新排列列表并最终加入那样,不那么直观。 你可以尝试这样
reordered_corpus = open('reordered_corpus.txt', 'w')
with open('corpus.txt', 'r') as corpus:
for phrase in corpus:
phrase = phrase.split() # split on whitespace
vb_index = rp_index = -1 # variables for the indices
for i, word_pos in enumerate(phrase):
pos = word_pos.split('_')[1] # POS at index 1 splitting on _
if pos == 'VB' or pos == 'VBZ': # can add more verb POS tags
vb_index = i
elif vb_index >= 0 and pos == 'RP': # or more particle POS tags
rp_index = i
break # found both so can stop
if vb_index >= 0 and rp_index >= 0: # do any rearranging
phrase = phrase[:vb_index+1] + [phrase[rp_index]] + \
phrase[vb_index+1:rp_index] + phrase[rp_index+1:]
reordered_corpus.write(' '.join(word_pos for word_pos in phrase)+'\n')
reordered_corpus.close()
使用此代码,如果corpus.txt
读取,
you_PRP mean_VBP we_PRP should_MD kick_VB them_PRP out_RP ._.
don_VB 't_NNP take_VB it_PRP off_RP until_IN I_PRP say_VBP so_RB ._.
please_VB help_VB the_DT man_NN out_RP ._.
shut_VBZ it_PRP down_RP !_.
运行后, reordered_corpus.txt
将是
you_PRP mean_VBP we_PRP should_MD kick_VB out_RP them_PRP ._.
don_VB 't_NNP take_VB off_RP it_PRP until_IN I_PRP say_VBP so_RB ._.
please_VB help_VB out_RP the_DT man_NN ._.
shut_VBZ down_RP it_PRP !_.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.