如何记号化在NLTK一个字符串句子?(How do I tokenize a string sent

2019-07-20 11:38发布

我使用NLTK,所以我想创建自己的自定义文本,就像在nltk.books默认的。 不过,我刚刚起床的方法类似

my_text = ['This', 'is', 'my', 'text']

我想发现任何方式输入我的“文本”为:

my_text = "This is my text, this is a nice way to input text."

哪一种方法,python的或从NLTK让我做这件事。 而更重要的是,我怎么能解雇标点符号?

Answer 1:

这实际上是对nltk.org的主要页面 :

>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']


Answer 2:

作为@PavelAnossov回答,规范的答案,使用word_tokenize在NLTK功能:

from nltk import word_tokenize
sent = "This is my text, this is a nice way to input text."
word_tokenize(sent)

如果你的一句话就足够了真正简单:

使用string.punctuation集,删除标点然后用空格分隔符分割:

import string
x = "This is my text, this is a nice way to input text."
y = "".join([i for i in x if not in string.punctuation]).split(" ")
print y


文章来源: How do I tokenize a string sentence in NLTK?