Python 分词
行分词
在以下示例中,我们使用sent_tokenize函数将给定的文本分成不同的行。
import nltk
sentence_data = "The First sentence is about Python. The Second: about Django. You can learn Python,Django and Data Ananlysis here. "
nltk_tokens = nltk.sent_tokenize(sentence_data)
print (nltk_tokens)
当我们运行上面的程序时,会得到以下的输出 –
['The First sentence is about Python.', 'The Second: about Django.', 'You can learn Python,Django and Data Ananlysis here.']
非英语的分词
在下面的示例中,我们对德语文本进行了分词。
import nltk
german_tokenizer = nltk.data.load('tokenizers/punkt/german.pickle')
german_tokens=german_tokenizer.tokenize('Wie geht es Ihnen? Gut, danke.')
print(german_tokens)
当我们运行上述程序时,我们得到以下输出 −
['Wie geht es Ihnen?', 'Gut, danke.']
词语标记化
我们使用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']