インターネット上のテキストデータは、この数十年で大幅に増加した。
この膨大な情報の処理を自動化する必要があるのは間違いありませんが、TextBlobパッケージはNLP – Natural Language Processingを行うための非常にシンプルな方法の1つです。
品詞タグ付け、名詞句抽出、トークン化、感情分析、分類、翻訳など、一般的な自然言語処理(NLP)タスクに飛び込むためのシンプルなAPIを提供しています。
このライブラリーを利用するための特別な技術的前提条件は必要ない。
例えば、TextBlobはPython 2とPython 3に対応している。
また、手元にテキスト情報がない場合は、NLTKデータベースから必要なコーパスを提供します。
TextBlobのインストール
まずはTextBlobとNLTKのコーパスをインストールします。
$ pip install -U textblob
$ python -m textblob.download_corpora
注意:このライブラリはアルゴリズムやコーパスの数が多いので、インストールに時間がかかる場合があります。
トークン化とは何ですか?
NLP の分野に深く入る前に、以下の重要な用語も理解しておく必要があります。
- コーパス(複数形はコーパス) – 言語データ(テキストなど)の特定のコレクションのことです。コーパスは通常、テキスト分類やセンチメント分析など、さまざまなモデルを学習するために使用されます。
- トークン – 主テキストから切り離された最終的な文字列で、言い換えれば、トークン化の出力です。
トークン化とは何ですか?
トークン化とは、コーパスに含まれる文や単語を小さな単位、つまりトークンに分離する簡単な処理です。
この例として、次のような文章が考えられます。
- 入力(コーパス)。入力(コーパス): 人が行う悪は彼らの後に生きている
- 出力(トークン)。入力(コーパス):The evil | that | men | do | lives | after | them | 出力(トークン):The evil | that | men | do | lives | after | them
ここでは、入力文は単語間のスペースに基づいてトークン化される。
また、1 つの単語から文字をトークン化したり (例: apple の a-p-p-l-e) 、1 つのテキストから個別のセンテンスをトークン化したりすることができます。
トークン化は、言語処理における基本的かつ重要な段階の 1 つです。
構造化されていないテキスト素材をデータに変換する。
これは、機械翻訳、検索エンジン最適化、または異なるビジネスの問い合わせの様々なモデルを開発する際に、さらに応用することができます。
コードによるトークン化の実装
まず最初に、TextBlob
オブジェクトを作成して、トークン化するコーパスのサンプルを定義する必要がある。
例えば、R. Kipling が書いた If という詩の一部をトークン化してみる。
from textblob import TextBlob
# Creating the corpus
corpus = '''If you can force your heart and nerve and sinew to serve your turn long after they are gone. And so hold on when there is nothing in you except the Will which says to them: 'Hold on!'
'''
オブジェクトを作成したら、それを TextBlob
コンストラクタの引数として渡します。
blob_object = TextBlob(corpus)
一度構築されると、この blob_object
に対して様々な操作を行うことができる。
このオブジェクトはすでに私たちのコーパスを含んでおり、ある程度分類されている。
単語のトークン化
最後に、トークン化された単語を取得するために、作成した blob_object
の words
属性を取得する。
これは str
オブジェクトと非常によく似た動作をする Word
オブジェクトを含むリストを提供する。
from textblob import TextBlob
corpus = '''If you can force your heart and nerve and sinew to serve your turn long after they are gone. And so hold on when there is nothing in you except the Will which says to them: 'Hold on!'
'''
blob_object = TextBlob(corpus)
# Word tokenization of the sample corpus
corpus_words = blob_object.words
# To see all tokens
print(corpus_words)
# To count the number of tokens
print(len(corpus_words))
出力コマンドは次のようなものになるはずです。
['If', 'you', 'can', 'force', 'your', 'heart', 'and', 'nerve', 'and', 'sinew', 'to', 'serve', 'your', 'turn', 'long', 'after', 'they', 'are', 'gone', 'and', 'so', 'hold', 'on', 'when', 'there', 'is', 'nothing', 'in', 'you', 'except', 'the', 'Will', 'which', 'says', 'to', 'them', 'Hold', 'on']
38
この方法では、区切り文字として SPACE
を使って単語をトークン化していることに注意する必要があります。
この区切り文字を、例えば TAB
に変更することができます。
from textblob import TextBlob
from nltk.tokenize import TabTokenizer
corpus = '''If you can force your heart and nerve and sinew to serve your turn long after they are gone. And so hold on when there is nothing in you except the Will which says to them: 'Hold on!'
'''
tokenizer = TabTokenizer()
blob_object = TextBlob(corpus, tokenizer = tokenizer)
# Word tokenization of the sample corpus
corpus_words = blob_object.tokens
# To see all tokens
print(corpus_words)
ここでは、最初の文の後にTAB
を追加していることに注意してください。
どのように、単語のコーパスは次のようになります。
['If you can force your heart and nerve and sinew to serve your turn long after they are gone.','And so hold on when there is nothing in you except the Will which says to them: 'Hold on!']
nltk.tokenize
には、他のトークン化オプションも含まれています。
デフォルトでは SpaceTokenizer
を使用しますが、これは明示的に定義する必要はありません。
この 2 つ以外にも、 LineTokenizer
、BlankLineTokenizer
、WordPunctTokenizer
などの便利なトークナイザーが含まれている。
完全なリストはドキュメントに記載されている。
文のトークン化
文レベルでトークン化するために、同じ blob_object
を使用します。
今回は、 words
属性の代わりに sentences
属性を使用します。
これは Sentence
オブジェクトのリストを返します。
from textblob import TextBlob
corpus = '''If you can force your heart and nerve and sinew to serve your turn long after they are gone. And so hold on when there is nothing in you except the Will which says to them: 'Hold on!'
'''
blob_object = TextBlob(corpus)
# Sentence tokenization of the sample corpus
corpus_sentence = blob_object.sentences
# To identify all tokens
print(corpus_sentence)
# To count the number of tokens
print(len(corpus_sentence))
出力されます。
[Sentence("If you can force your heart and nerve and sinew to serve your turn long after they are gone"), Sentence("And so hold on when there is nothing in you except the Will which says to them: 'Hold on!")]
2
結論
トークン化は自然言語処理における非常に重要なデータ前処理のステップであり、テキストをトークンと呼ばれる小さな塊に分解することを含む。
このトークンは元のテキストにある個々の単語、文、文字になる。
TextBlobはシンプルなAPIで、すぐに自然言語処理に取り掛かれるので、自然言語処理に取り組むには最適なライブラリです。
今回はTextBlobが扱う自然言語処理タスクの1つを紹介しましたが、次回はもっと複雑な問題、例えば単語の屈折、複数形、単数形の扱い方などを見ていきます。