Python 在中扩展英语缩略语,基于最常见缩略语词典
我试图用Python替换缩略词,但遇到了错误Python 在中扩展英语缩略语,基于最常见缩略语词典,python,dictionary,twitter,preprocessor,Python,Dictionary,Twitter,Preprocessor,我试图用Python替换缩略词,但遇到了错误 import re tweet = "I luv my <3 iphone & you're awsm apple. DisplayIsAwesome, sooo happppppy Here's a clue: >>> print('(%s)' '|'.join(contractions_dict.keys())) you're(%s)|aren't(%s)|ain't(%s)|can't 重新导入 tweet=
import re
tweet = "I luv my <3 iphone & you're awsm apple. DisplayIsAwesome, sooo happppppy Here's a clue:
>>> print('(%s)' '|'.join(contractions_dict.keys()))
you're(%s)|aren't(%s)|ain't(%s)|can't
重新导入
tweet=“我爱我的这里有一个线索:
>>> print('|'.join('(%s)' % k for k in contractions_dict.keys()))
(you're)|(aren't)|(ain't)|(can't)
由于%s
在正则表达式中没有特殊意义,因此它只会匹配自身。但是输入中没有百分号,因此匹配失败
我怀疑你在找类似的东西
>>> print('(%s)' % '|'.join(contractions_dict.keys()))
(you're|aren't|ain't|can't)
或许
>>> contractions_re = re.compile('|'.join(contractions_dict.keys()))
>>> expand_contractions(tweet)
'I luv my <3 iphone & you are awsm apple. DisplayIsAwesome, sooo happppppy \xf0\x9f\x99\x82 http://www.apple.com'
但是,由于您使用的是match.group(0)
(即,整个匹配字符串),因此捕获是不相关的,不需要在替换中插入单词。因此,更简单的解决方案是:
>>压缩项\u re=re.compile(“|”).join(压缩项\u dict.keys())
>>>展开收缩(推特)
“我爱我的孩子