LoginSignup
3
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-03-05

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

第9章: ベクトル空間法 (I)

enwiki-20150112-400-r10-105752.txt.bz2は,2015年1月12日時点の英語のWikipedia記事のうち,約400語以上で構成される記事の中から,ランダムに1/10サンプリングした105,752記事のテキストをbzip2形式で圧縮したものである.このテキストをコーパスとして,単語の意味を表すベクトル(分散表現)を学習したい.第9章の前半では,コーパスから作成した単語文脈共起行列に主成分分析を適用し,単語ベクトルを学習する過程を,いくつかの処理に分けて実装する.第9章の後半では,学習で得られた単語ベクトル(300次元)を用い,単語の類似度計算やアナロジー(類推)を行う.

なお,問題83を素直に実装すると,大量(約7GB)の主記憶が必要になる. メモリが不足する場合は,処理を工夫するか,1/100サンプリングのコーパスenwiki-20150112-400-r100-10576.txt.bz2を用いよ.

80. コーパスの整形

文を単語列に変換する最も単純な方法は,空白文字で単語に区切ることである. ただ,この方法では文末のピリオドや括弧などの記号が単語に含まれてしまう. そこで,コーパスの各行のテキストを空白文字でトークンのリストに分割した後,各トークンに以下の処理を施し,単語から記号を除去せよ.

  • トークンの先頭と末尾に出現する次の文字を削除: .,!?;:()[]'"
  • 空文字列となったトークンは削除

以上の処理を適用した後,トークンをスペースで連結してファイルに保存せよ.

出来上がったコード:

main.py
# coding: utf-8
import bz2
fname_input = 'enwiki-20150112-400-r100-10576.txt.bz2'      # 1/100版^^;
fname_output = 'corpus80.txt'

# 1行ずつ処理
with bz2.open(fname_input, 'rt') as data_file, \
        open(fname_output, mode='wt') as out_file:
    for line in data_file:

        # 空白で分解、前後の記号除去
        tokens = []     # 結果のトークン配列
        for chunk in line.split(' '):
            token = chunk.strip().strip('.,!?;:()[]\'"')
            if len(token) > 0:
                tokens.append(token)

        # 出力
        print(*tokens, sep=' ', end='\n', file=out_file)

実行結果:

結果ファイル「orpus80.txt」の先頭部分です。

corpus80.txtの先頭部分
Anarchism

Anarchism is a political philosophy that advocates stateless societies often defined as self-governed voluntary institutions but that several authors have defined as more specific institutions based on non-hierarchical free associations Anarchism holds the state to be undesirable unnecessary or harmful While anti-statism is central anarchism entails opposing authority or hierarchical organisation in the conduct of human relations including but not limited to the state system
As a subtle and anti-dogmatic philosophy anarchism draws on many currents of thought and strategy Anarchism does not offer a fixed body of doctrine from a single particular world view instead fluxing and flowing as a philosophy There are many types and traditions of anarchism not all of which are mutually exclusive Anarchist schools of thought can differ fundamentally supporting anything from extreme individualism to complete collectivism Strains of anarchism have often been divided into the categories of social and individualist anarchism or similar dual classifications Anarchism is usually considered a radical left-wing ideology and much of anarchist economics and anarchist legal philosophy reflect anti-authoritarian interpretations of communism collectivism syndicalism mutualism or participatory economics
The central tendency of anarchism as a social movement has been represented by anarcho-communism and anarcho-syndicalism with individualist anarchism being primarily a literary phenomenon which nevertheless did have an impact on the bigger currents and individualists have also participated in large anarchist organisations Many anarchists oppose all forms of aggression supporting self-defense or non-violence anarcho-pacifism while others have supported the use of some coercive measures including violent revolution and propaganda of the deed as means to achieve anarchist ends
Etymology and terminology
The term anarchism is a compound word composed from the word anarchy and the suffix -ism themselves derived respectively from the Greek i.e anarchy from anarchos meaning one without rulers from the privative prefix ἀν- an- i.e without and archos i.e leader ruler cf archon or arkhē i.e authority sovereignty realm magistracy and the suffix or -ismos -isma from the verbal infinitive suffix -ίζειν -izein The first known use of this word was in 1539."Anarchist was the term adopted by Maximilien de Robespierre to attack those on the left whom he had used for his own ends during the French Revolution but was determined to get rid of though among these anarchists there were few who exhibited the social revolt characteristics of later anarchists There would be many revolutionaries of the early nineteenth century who contributed to the anarchist doctrines of the next generation such as William Godwin and Wilhelm Weitling but they did not use the word anarchist or anarchism in describing themselves or their beliefs Pierre-Joseph Proudhon was the first political philosopher to call himself an anarchist marking the formal birth of anarchism in the mid-nineteenth century Since the 1890s from France the term libertarianism has often been used as a synonym for anarchism and was used almost exclusively in this sense until the 1950s in the United States its use as a synonym is still common outside the United States On the other hand some use libertarianism to refer to individualistic free-market philosophy only referring to free-market anarchism as libertarian anarchism
History
Origins
The earliest anarchist themes can be found in the 6th century BC among the works of Taoist philosopher Laozi and in later centuries by Zhuangzi and Bao Jingyan Zhuangzi's philosophy has been described by various sources as anarchist Zhuangzi wrote A petty thief is put in jail A great brigand becomes a ruler of a Nation Diogenes of Sinope and the Cynics their contemporary Zeno of Citium the founder of Stoicism also introduced similar topics Jesus is sometimes considered the first anarchist in the Christian anarchist tradition Georges Lechartier wrote that The true founder of anarchy was Jesus Christ and  the first anarchist society was that of the apostles In early Islamic history some manifestations of anarchic thought are found during the Islamic civil war over the Caliphate where the Kharijites insisted that the imamate is a right for each individual within the Islamic society Later some Muslim scholars such as Amer al-Basri and Abu Hanifa led movements of boycotting the rulers paving the way to the waqf endowments tradition which served as an alternative to and asylum from the centralized authorities of the emirs But such interpretations reverberates subversive religious conceptions like the aforementioned seemingly anarchistic Taoist teachings and that of other anti-authoritarian religious traditions creating a complex relationship regarding the question as to whether or not anarchism and religion are compatible This is exemplified when the glorification of the state is viewed as a form of sinful idolatry

全体はGitHubにアップしています。

第9章でやろうとしていること

単語をベクトルに変換

この章では単語をベクトルに変換します。ベクトルは方向と値を持った量なので、単語を方向と値に変換していくことになります。

ベクトルに変換するメリット

単語をベクトルにしたい理由は、ベクトルだと演算が容易にできるので、コンピューターでいろいろと単語の処理がやりやすくなるからです。たとえば2つのベクトルが近いかどうか?というのはコサイン類似度で求められるので、単語をベクトルに変換すれば、ベクトル同士のコサイン類似度を求めることで単語の「近い」「遠い」が判定できるようになります(問題87)。ベクトルを足し算、引き算すれば、単語の足し算や引き算もできるようになります(問題89)。

単語の何をベクトルにするのか

ただし、単語をベクトルに変換する際の方向と値の決め方が重要です。例えばアルファベットのAからZを示す26次元のベクトルを用意し、単語中のアルファベットの出現数を各次元の値にしよう、というルールでベクトルに変換したとします。その結果、単語同士の「近い」「遠い」が計算できるようにはなりますが、単語で使われいているアルファベットの種類や数が似ているかどうかがわかるだけです。これではあまり面白くありません。

そこでこの章では、単語の「意味」をベクトルの方向と値に変換しようとしています。「意味」というとちょっと大げさですが、Wikipediaの記事で使われてる単語の種類だけ次元を用意して(数百万次元になります)、変換元の単語の周辺で使われている他の単語の出現数をその値にします(実際には単純な出現数ではなく工夫します)。やることは前述のアルファベットの例とあまり変わりません。
こうやって作ったベクトルは、他の単語とどれくらい一緒に使われているか?という情報を持っています。使われ方が似ているなら意味的にも近い単語になりそうですよね。これがこの章でやる、単語の意味をベクトルに変換する作業です(問題84)。

ベクトルの次元の圧縮

ベクトル演算で「近い」とわかった単語は意味も似ている単語になりそうですし、ベクトルの足し算や引き算で、意味の足し算や引き算もできるようになりそうです。ただし、数百万次元のベクトルは簡単に演算できません。そのため、次元の圧縮のテクニックも学びます(問題85)。

コーパスの整形

今回の問題は指示通りに整形するだけですので、特に難しいことはないかと思います。bz2ファイルはbz2モジュールを使えば直接読み込めるので便利です。

なお、最初はWikipediaの1/10サンプリングを使っていたのですが、問題85でMemory Errorの壁にぶつかってしまい、1/100サンプリングに切り替えました^^;
1/10サンプリングで挑戦される方はファイル名が異なりますのでご注意ください。

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


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

3
3
2

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
3
3