nltk/re:尝试用regex标记时,没有要重复的内容

2024-10-17 06:18:04 发布

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

import nltk

text = """The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital
computer or the gears of a cycle transmission as he does at the top of a mountain
or in the petals of a flower. To think otherwise is to demean the Buddha...which is
to demean oneself."""


sentence_re = r'''(?:(?:[A-Z])(?:.[A-Z])+.?)|(?:\w+(?:-\w+)*)|(?:$?\d+(?:.\d+)?%?)|(?:...|)(?:[][.,;"'?():-_`])'''

toks = nltk.regexp_tokenize(text, sentence_re)

但我得到:

  File "C:\Users\AppData\Local\Continuum\Anaconda2\envs\Python35\lib\sre_parse.py", line 638, in _parse
    source.tell() - here + len(this))

error: nothing to repeat

我知道以前有一个错误,但我正在使用最新的NLTK和Python3.5,我认为我不应该经历的错误。有人知道发生了什么事吗?你知道吗

从python3.5virtualenv在Spyder3中运行

正则表达式正在尝试(按顺序)获取:

  • 缩略语
  • (可选)连字符单词
  • 货币和百分比
  • 省略号和特殊标记,如? [ ( :等。。。你知道吗

Tags: orofthetotextinreparse
1条回答
网友
1楼 · 发布于 2024-10-17 06:18:04

您得到的错误与您量化了字符串锚定的$结尾有关。unescaped$是在字符串末尾匹配的零宽度断言。要匹配文字$,需要对其进行转义。你知道吗

表达式中的.字符也需要转义以匹配文字点。你知道吗

但是,-[][.,;"'?():-_`]的character类中形成范围也有问题。要确保--匹配,请将它放在最后一个]之前的末尾。你知道吗

此外,似乎您希望匹配不包含下划线的单词(因为您将_放在最后一个字符类中)。因此,我建议减去_形成\w模式,并用[^\W_]+(?:-[^\W_]+)*替换\w+(?:-\w+)*。你知道吗

以下是我的建议实施的模式:

sentence_re = r'''\$?\d+(?:\.\d+)?%?|[A-Z](?:\.[A-Z])+\.?|[^\W_]+(?:-[^\W_]+)*|(?:\.{3}|)[][.,;"'?():_`-]'''

参见regex demo

相关问题 更多 >