我正在使用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允许我这样做。更重要的是,我如何解除标点符号?
答案 0 :(得分:131)
实际上这是main page of 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', '.']
答案 1 :(得分:-9)
正如@PavelAnossov回答的那样,规范的答案,请使用nltk中的word_tokenize
函数:
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