正規表現を使用して、単語の文字列を単語のリストに分割しようとしています。私はまだ正規表現の初心者です。
私は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']
「絵文字」の回避策があるので、私が最も懸念しているのは引用符です。