0
我试图通过正则表达式将单词串分割成单词列表。我仍然是一个有正则表达式的初学者。包括标点符号在标点符号后分割字符串
我正在使用nltk.regex_tokenize,这是产生的结果是接近,但不是我想要的。
这是我到目前为止有:
>>> import re, codecs, nltk
>>> sentence = "détesté Rochard ! m'étais à... 'C'est hyper-cool.' :) :P"
>>> pattern = r"""(?x)
#words with internal hyphens
| \w+(-\w+)*
#ellipsis
| \.\.\.
#other punctuation tokens
| [][.,;!?"'():-_`]
"""
>>> nltk.regexp_tokenize(sentence.decode("utf8"), pattern)
[u'd\xe9test\xe9', u'Rochard', u'!', u'm', u"'", u'\xe9tais', u'\xe0', u'qu', u"'", u'on', u'...', u"'", u'C', u"'", u'est', u'hyper-cool', u'.', u"'", u':', u')', u':', u'P']
我想有如下输出:
[u'd\xe9test\xe9', u'Rochard', u'!', u"m'", u'\xe9tais', u'\xe0', u"qu'", u'on', u'...', u"'", u"C'", u'est', u'hyper-cool', u'.', u"'", u':)', u':P']
我对“表情符号”一种解决办法,所以我”最关心的是引号。