如何修复这个python正则表达式中的这个bug来删除单词列表中不需要的单词((字符串)

2024-07-07 07:13:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我从StackOverflow得到一个正则表达式,用于删除停止词:

def killStopWords(stringsList):
        pattern = re.compile("\\b(a|y|o|un|una|unas|unos|que|k|d|el|la|los|les|las|pq|xq|en|como|me|con|de)\\W", re.I)
        return [pattern.sub("", aString) for aString in stringsList]

但是我注意到它在某些情况下无法正常工作,例如:

test = killStopWords(['alegría es un amor', 'la papelería afición con ilusión'])

for k in test:
    print k

将打印:

alegríes amor
papeleríafición ilusión

而不是:

alegría, es, amor, papelería, afición, ilusión

应该是这样的

它看起来像是删除字符旁边的那些含有口音


Tags: reforesconlaunpatternamor