📜  Python -Word标记化

📅  最后修改于: 2020-12-13 14:16:34             🧑  作者: Mango


单词标记化是将大量文本样本拆分为单词的过程。这是自然语言处理任务的要求,在自然语言处理任务中,每个单词都需要捕获并进行进一步的分析,例如根据特定的情感对它们进行分类和计数等。自然语言工具套件(NLTK)是用于实现此目的的库。在继续Python程序进行单词标记化之前,请安装NLTK。

conda install -c anaconda nltk

接下来,我们使用word_tokenize方法将段落拆分为单个单词。

import nltk

word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
nltk_tokens = nltk.word_tokenize(word_data)
print (nltk_tokens)

当我们执行上面的代码时,它产生以下结果。

['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers', 
'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the',
'comforts', 'of', 'their', 'drawing', 'rooms']

标记化句子

我们也可以像标记词一样标记段落中的句子。我们使用send_tokenize方法来实现此目的。下面是一个例子。

import nltk
sentence_data = "Sun rises in the east. Sun sets in the west."
nltk_tokens = nltk.sent_tokenize(sentence_data)
print (nltk_tokens)

当我们执行上面的代码时,它产生以下结果。

['Sun rises in the east.', 'Sun sets in the west.']