我有以下txt文件,其中包含每个单词的POS(Part of Speech)标记。
无需/ jj / /说/ vb,/,I / ppss / / bedz furious / jj at / in this / dt unparalleled / jj intrusion / nn on / in free / jj enterprise / nn ./。 如何/ wrb dared / vbn他们/ ppss
有没有办法在没有POS标签的情况下读取文件,结果将是:
毋庸置疑,我对这种对自由企业的无与伦比的侵扰感到愤怒。 多么大胆
所以,基本上我想删除/
之后的任何字符。
words = re.findall('\w+',open(input_file).read())
上面的代码会删除/但是缩写如jj,ppss仍会出现。 那么,如何删除/跟随任何角色。
答案 0 :(得分:4)
这还不错吗?
>>> import re
>>> s = 'Needless/jj to/to say/vb ,/, I/ppss was/bedz furious/jj at/in this/dt unparalleled/jj intrusion/nn upon/in free/jj enterprise/nn ./.'
>>> re.sub(r'/[^\s]+','',s)
'Needless to say , I was furious at this unparalleled intrusion upon free enterprise .'
这只会删除以/
开头的任何文本,直到找到空格。
答案 1 :(得分:1)
正如Wooble建议的那样,你可以通过嵌套在列表理解中的两个分割来做到这一点:
s = 'Needless/jj to/to say/vb ,/, I/ppss was/bedz furious/jj at/in this/dt unparalleled/jj intrusion/nn upon/in free/jj enterprise/nn ./.'
print " ".join(word.split("/")[0] for word in s.split())
输出:
Needless to say , I was furious at this unparalleled intrusion upon free enterprise .
s.split()
将句子分成单独的单词。 word.split("/")
将英语单词(或puncutation mark)与其词性分开。 word.split("/")[0]
仅选择英文单词并丢弃POS。 " ".join()
将生成的英语单词列表合并为一个字符串。
答案 2 :(得分:0)
此代码考虑了Wooble的评论以及您需要处理字符串列表,afaiu:
li = [ ('//Needless/jj to/to say/vb ,/, '
'I/ppss was/bedz fur/ious/jj at/in this/dt '
'unparalleled/jj intrusion/nn upon/in '
'free/jj enterprise/nn ./. '
'How/wrb dared/vbn they/ppss'),
'/Before/jj to/to say/vb ,/, /I/ppss am/bedz h/a/p/p/y/jj']
import re
def clean(s,r=re.compile('(?<![\s/])/[^\s/]+(?![\S/])')):
return r.sub('',s)
x = map(clean, li)
print '\n\n'.join(x)
结果
//Needless to say , I was fur/ious at this unparalleled intrusion upon free enterprise . How dared they
/Before to say , /I am h/a/p/p/y