spaCyで英文の固有表現認識

今回はspaCyを使って英文の固有表現認識を行ってみます。

GiNZAを使った日本語の固有表現認識はこちら↓です。

ohke.hateblo.jp

固有表現抽出

固有表現認識 (named entity recognition: NER) は、文書から固有表現 (named entity) を抽出・分類することです。

固有表現には、固有名詞や、数字を含む表現などが該当します。固有表現は、時事性を持っていたり、数字によって膨大なパターンが存在していたりするため、辞書化が難しいものです。
そのため「辞書には無いけどこれはXに分類される単語だな」ということだけでもわかると、この後のタスクの精度改善に寄与できます。

分類 (ラベル) はいくつか定義がありますが、例えばMUC (参考) で定義されているのは7種類です。

ラベル
組織名 IEEE, 阪神タイガース
人名 田中, 所ジョージ
地名 蘇我, 東京タワー
日付 2月2日, 2/2/2019
時間 19時, 2分
金額 ¥12,000, 65M
割合 33%, 五分

固有表現抽出タスクは、例えばこんな感じで固有表現を抽出・分類します。

去年の4月、友人・安田と新幹線で京都を旅行した。
↓
去年の 4月[日付] 、友人・ 安田[人名] と新幹線で 京都[地名] を旅行した。

spaCyを使った固有表現認識

spaCyはNLPタスクのために作られたPythonライブラリで、固有表現認識以外にも、品詞タグ付けや構文解析などにも用いられます。

github.com

何はともあれspaCy本体をインストール。

pip install spacy

一緒に学習済みもダウンロードしておきます (利用可能なモデルの一覧は こちら ) 。後ほどモデル名で参照します。
en_core_web_smは、OntoNotesコーパスから学習したモデルです。

python -m spacy download en_core_web_sm

それではPythonの実装です。今回はCNNの記事を使いました。

  • 最初にダウンロードしたモデル "en_core_web_sm" をロードして、それをメソッド呼び出し (__call__) するだけで引数の文書を解析してくれます
  • entはSpan型になっており、label_プロパティに固有表現のラベルが入ってます
import spacy

# モデルをロード
nlp = spacy.load('en_core_web_sm')

# 抜粋: https://edition.cnn.com/2019/02/01/asia/japan-hacking-cybersecurity-iot-intl/index.html
news = """Beginning on February 20, Japanese officials will start probing 200 million IP addresses linked to the country, sniffing out devices with poor or little security.A law was passed last year to enable the mass hack, as part of security preparations ahead of the Tokyo 2020 Olympics.According to the Ministry of Internal Affairs and Communications (MIAC), two-thirds of cyber attacks in Japan in 2016 targeted IOT devices. Officials fear some kind of IOT-related attack could be used to target or disrupt the Olympics."""

# 解析
doc = nlp(news)

# 固有表現を見てみる
for ent in doc.ents:
    # spacy.tokens.span.Span
    print(ent.text, ent.label_, ent.start_char, ent.end_char)

結果はこうなりました。ラベルの意味は こちら ですが、良い感じに分類されています。

  • "the Tokyo 2020 Olympics" もちゃんとイベントとしてラベリングされてて、驚きです
  • "two-thirds"が数字、"last year"が日付として柔軟にラベリングしてます
  • "IOT"が組織として認識されている点が惜しいです
February 20 DATE 13 24
Japanese NORP 26 34
200 million CARDINAL 64 75
last year DATE 179 188
the Tokyo 2020 Olympics EVENT 256 279
the Ministry of Internal Affairs and Communications ORG 293 344
MIAC ORG 346 350
two-thirds CARDINAL 353 363
Japan GPE 384 389
2016 DATE 393 397
IOT ORG 407 410
IOT ORG 448 451
Olympics EVENT 506 514

文章を少し変えて、実在しない組織 "Ministry of Hogehoge and Fugafuga" に置き換えて解析させてみました。
それでもちゃんと組織 ("ORG") として認識されています。

According to the Ministry of Hogehoge and Fugafuga, two-thirds of cyber attacks in Japan in 2016 targeted IOT devices.
↓
the Ministry of Hogehoge and Fugafuga ORG

もうちょっと遊んでみます。文章のTokyoをChibaに変えてみたところ、"the Tokyo 2020 Olympics"として1語で認識されていたものが、Chiba, 2020, Olympicsの3語に分割されてそれぞれでラベル付されていました。学習データに東京オリンピックが入っていたのでしょう。

A law was passed last year to enable the mass hack, as part of security preparations ahead of the Chiba 2020 Olympics.
↓
Chiba GPE 260 265
2020 DATE 266 270
Olympics EVENT 271 279

まとめ

今回はspaCyで固有表現認識を行いました。日本語で同じことをするにはどうすれば良いのか、また次の機会で調べてみたいと思います。