📜  Python标记化

📅  最后修改于: 2020-11-06 06:17:18             🧑  作者: Mango


行标记化

在下面的示例中,我们通过使用send_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']