簡體   English   中英

如何在NLTK中對字符串句子進行標記?

[英]How do I tokenize a string sentence in NLTK?

我正在使用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允許我這樣做。 更重要的是,我如何解除標點符號?

這實際上是在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', '.']

正如@PavelAnossov回答的那樣,規范的答案,使用word_tokenize中的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM