【发布时间】:2016-04-03 17:47:39
【问题描述】:
我正在尝试从一段文本中删除常用词(连词、副词、代词等)。我正在使用正则表达式,但由于某种原因,我的过滤器中的一些常用词没有被过滤掉。
一些没有被过滤的单词示例:“havent”、“why”、“should”
有什么想法吗?
splitResult = s.split()
p = re.compile(
"""^(&|also|a|about|again|all|after|are(nt)?|arent|as|an(y)?|at|
bcuz|before|be(low)?|between|bring|but|by|and|can(not)?|close(d)?|could(nt)?|
cuz|do(nt)?|down|decide(d)?|decision|on(to)?|or|of|our|over|out|have(nt)?|he(re)?|
her|his|other(s)?|even|got(ten)?|for|from|get(s)?|got(ten)?|has(nt)?|havent|he(s)?|
him|his|if|in|to|in(to)?|is(nt)?||make|me|once|play(ed)?|role|say(s)?|seen|she(s)?|
should(nt)?|stop(ped)?|time|my|no(t)?|must(nt)?|now|you(re)?|your|want|want(ed)?|
watch(ed)?|way|we(re)?|will|with||i|a|is(nt)?|just|would(nt)?|before|that|the(re)?|
their|them|they|this|turn|when|at|how|it(s)?|which|who|after|then|if|how|because|know(s)?|
yet|[A-Za-z]{1,2}|http(s)?://.*|www\..*)$""",re.I)
for word in splitResult:
m = p.findall(word)
if not m:
word = "".join(c for c in word if c not in ("?", ".", "!", '"', ",","'","(",")"))
wordsList.insert(ctr,word)
【问题讨论】:
-
我认为这是一个更适合自然语言处理的工作,例如:stackoverflow.com/questions/9953619/…。
-
我把它放在Regex101,你可以在解释部分看到错误(虽然它没有突出显示)。基本上你有
is(nt)?||make,应该是is(nt)?||make和with||i,应该是with|i。两者都有 2||而不是 1。这不能解决问题,但我建议你更新你的 RegEx -
我找不到任何问题,我只是将捕获组转换为非捕获以获得更清晰的输出:请参阅demo。请注意,您可以通过对具有共同结尾的关键字进行分组来进一步缩小此模式以使其更有效。
标签: regex python-3.x