ストップワードと句読点の処理

Pythonで学ぶ自然言語処理(NLP)

Fouad Trad

Machine Learning Engineer

ストップワード

  • 頻出するが、機械による文脈理解への寄与は小さい
  • 多くのNLPタスクで有用性が低い
  • 除去により重要語へ焦点化できる

a, an, the, in, of, that, for, by などのストップワードの画像。

Pythonで学ぶ自然言語処理(NLP)

ストップワードの除去

有用な場面

テキストの主題を把握する

モバイルアプリでの製品レビューの画像

Pythonで学ぶ自然言語処理(NLP)

ストップワードの除去

有用な場面

テキストの主題を把握する

モバイルアプリでの製品レビューの画像

不向きな場面

全文の各語が必要なタスク

英語(Good morning)からフランス語(Bonjour)への翻訳の画像。

Pythonで学ぶ自然言語処理(NLP)

ストップワードの取得

NLTKは複数言語のストップワード一覧を提供します

from nltk.corpus import stopwords
nltk.download('stopwords')

stop_words = stopwords.words('english')
print(stop_words[:10])
['a', 'about', 'above', 'after', 'again', 'against', 'ain', 'all', 'am', 'an']
Pythonで学ぶ自然言語処理(NLP)

ストップワードの除去

from nltk.tokenize import word_tokenize

text = "This is an example to demonstrate removing stop words."
tokens = word_tokenize(text)
# The .lower() method helps with case sensitivity filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
print(filtered_tokens)
['example', 'demonstrate', 'removing', 'stop', 'words', '.']
Pythonで学ぶ自然言語処理(NLP)

句読点

  • 人間向けの言語構成のために使われる
  • 多くのNLPタスクでは意味情報が乏しい

句読点や特殊文字を示す画像。

Pythonで学ぶ自然言語処理(NLP)

句読点の除去

有用な場面

文書内の共通語・重要語を抽出するタスク

処理が必要な複数のファイルや文書の画像。

Pythonで学ぶ自然言語処理(NLP)

句読点の除去

有用な場面

文書内の共通語・重要語を抽出するタスク

処理が必要な複数のファイルや文書の画像。

不向きな場面

文の構造を保って明確さを要するタスク

多数の本から要約文書が生成される画像。

Pythonで学ぶ自然言語処理(NLP)

句読点へのアクセスと除去

import string
print(string.punctuation)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
text = "This is an example to demonstrate removing stop words."
tokens = word_tokenize(text)
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]

clean_tokens = [word for word in filtered_tokens if word not in string.punctuation]
print(clean_tokens)
['example', 'demonstrate', 'removing', 'stop', 'words']
Pythonで学ぶ自然言語処理(NLP)

Let's practice!

Pythonで学ぶ自然言語処理(NLP)

Preparing Video For Download...