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中运行
正则表达式正在尝试(按顺序)获取:
? [ ( :
等。。。你知道吗
您得到的错误与您量化了字符串锚定的
$
结尾有关。unescaped$
是在字符串末尾匹配的零宽度断言。要匹配文字$
,需要对其进行转义。你知道吗表达式中的
.
字符也需要转义以匹配文字点。你知道吗但是,
-
在[][.,;"'?():-_`]
的character类中形成范围也有问题。要确保-
与-
匹配,请将它放在最后一个]
之前的末尾。你知道吗此外,似乎您希望匹配不包含下划线的单词(因为您将
_
放在最后一个字符类中)。因此,我建议减去_
形成\w
模式,并用[^\W_]+(?:-[^\W_]+)*
替换\w+(?:-\w+)*
。你知道吗以下是我的建议实施的模式:
参见regex demo
相关问题 更多 >
编程相关推荐