Python-data-science-python-word-tokenization

提供:Dev Guides
移動先:案内検索

Python-ワードトークン化

単語のトークン化は、テキストの大きなサンプルを単語に分割するプロセスです。 これは、自然言語処理タスクの要件であり、各単語をキャプチャして、特定のセンチメントなどで分類およびカウントするなどのさらなる分析を行う必要があります。 Natural Language Tool kit(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']

文のトークン化

単語をトークン化したように、段落内の文をトークン化することもできます。 これを実現するには、メソッド sent_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.']