LoginSignup
4
2

More than 5 years have passed since last update.

素人の言語処理100本ノック:91

Last updated at Posted at 2017-04-11

言語処理100本ノック 2015の挑戦記録です。環境はUbuntu 16.04 LTS + Python 3.5.2 :: Anaconda 4.1.1 (64-bit)です。過去のノックの一覧はこちらからどうぞ。

第10章: ベクトル空間法 (II)

第10章では,前章に引き続き単語ベクトルの学習に取り組む.

91. アナロジーデータの準備

単語アナロジーの評価データをダウンロードせよ.このデータ中で": "で始まる行はセクション名を表す.例えば,": capital-common-countries"という行は,"capital-common-countries"というセクションの開始を表している.ダウンロードした評価データの中で,"family"というセクションに含まれる評価事例を抜き出してファイルに保存せよ.

出来上がったコード:

main.py
# coding: utf-8
fname_input = 'questions-words.txt'
fname_output = 'family.txt'

with open(fname_input, 'rt') as data_file, \
        open(fname_output, 'wt') as out_file:

    target = False      # 対象のデータ?
    for line in data_file:

        if target is True:

            # 対象データの場合は別のセクションになるまで出力
            if line.startswith(': '):
                break
            print(line.strip(), file=out_file)

        elif line.startswith(': family'):

            # 対象データ発見
            target = True

実行結果:

結果は「family.txt」に保存します。以下、先頭部分です。

family.txtの先頭部分
boy girl brother sister
boy girl brothers sisters
boy girl dad mom
boy girl father mother
boy girl grandfather grandmother
boy girl grandpa grandma
boy girl grandson granddaughter
boy girl groom bride
boy girl he she
boy girl his her
boy girl husband wife
boy girl king queen
boy girl man woman
boy girl nephew niece
boy girl policeman policewoman
boy girl prince princess
boy girl son daughter
boy girl sons daughters
boy girl stepbrother stepsister
boy girl stepfather stepmother
boy girl stepson stepdaughter
boy girl uncle aunt
brother sister brothers sisters
brother sister dad mom
brother sister father mother
brother sister grandfather grandmother
brother sister grandpa grandma
brother sister grandson granddaughter
brother sister groom bride
brother sister he she
(以下略)

単語アナロジーの評価データ

問題文で指定されている「単語アナロジーの評価データ」はword2vecに含まれている「questions-words.txt」というファイルのようですが、問題文ではリンクが切れてしまっています。

word2vecが公開された当時はGoogle Codeというオープンソースプロジェクトのホスティングサービスが使われていたのですが、このサービスは2016年1月に終了してしまい、現在はGitHubのsvn2github/word2vecに移っているようです。「questions-words.txt」もこの中から入手できます。

今回の問題は、このファイルの中から「family」のセクションを抽出すればOKです。

 
92本目のノックは以上です。誤りなどありましたら、ご指摘いただけますと幸いです。


実行結果には、100本ノックで用いるコーパス・データで配布されているデータの一部が含まれます。この第10章で用いているコーパス・データのライセンスはクリエイティブ・コモンズ 表示-継承 3.0 非移植日本語訳)です。また、単語アナロジーの評価データはApache License 2.0で配布されているword2vecの一部です。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2