言語処理とPython
1章の内容
1,自然言語処理の概要
2,NLTKを使ってみる
3,pythonを使ってみる
4,細かく文章から単語を取得する
1,自然言語処理の概要
自動自然言語理解
自然言語処理を俯瞰すると以下の通り
・語義曖昧性解消
・代名詞解析
・言語出力の生成
・機械翻訳
・音声対話システム
・含意関係
NLPの限界
言語理解の研究開発が進んでいるにもかかわらず、現実世界の自然言語システムは、常識的な応答を行ったり、世界認識をしっかり描写することなどは今のところできていない。
この本の目的
ひとまず推論や知識といった能力は人工知能にまかせ、自然言語処理としては「言語を理解する」という所に主眼をおく
2,NLTKを使ってみる
NLTKの準備
#nltkをインストールする
>>> import nltk
>>> nltk.download()
#nltkのブックモジュールからすべての項目をダウンロードする
>>> from nltk.book import *
テキストを検索する
concordance()
指定された単語が現れた全ての場所を、その周辺の文章と共に表示する
>>> text1.concordance("monstrous")
Displaying 11 of 11 matches:
ong the former , one was of a most monstrous size . ... This came towards us ,
ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r
ll over with a heathenish array of monstrous clubs and spears . Some were thick
d as you gazed , and wondered what monstrous cannibal and savage could ever hav
that has survived the flood ; most monstrous and most mountainous ! That Himmal
they might scout at Moby Dick as a monstrous fable , or still worse and more de
similar()
目的の単語を()内に記述することで、同じ文脈で使われている別の単語を見つける事ができる
>>> text1.similar("monstrous")
trustworthy horrible imperial perilous maddens determined doleful
passing gamesome careful modifies contemptible singular tyrannical
pitiable true candid puzzled mouldy wise
common_contexts()
2つ以上の単語で共通に使われている文脈を調査できる
>>> text2.commmon_contexts(["monstrous", "very"])
be_glad am_glad a_pretty is_pretty a_lucky
3,pythonを使ってみる
#語彙を数える
>>> len(text3)
44764
#語彙データを句読点→Aから始まる単語でソートする
>>> sorted(set(text3))
#種類を数える
>>>len(set(text3))
2789
#テキスト中に語句が何個あるか数える
text3.count("smote")
5
pythonの基本文法はパス
言語の計算処理
ある書籍から、もっとも多く出現する単語を50個探すことを考える
頻度分布は言語処理で頻繁に必要になるので、NLTKはそれを標準でサポートしている
#頻度分布を調べる
>>> fdist1 = FreqDist(text1)
>>> fdist1
FreqDist({',': 18713, 'the': 13721, '.': 6862, 'of': 6536, 'and': 6024, 'a': 4569, 'to': 4542, ';': 4072, 'in': 3916, 'that': 2982, ...})
#キーを並べる
>>> vocabulary1 = list(fdist1.keys())
>>> vocabulary[:50]
['willingness', 'blindest', 'unwearied', 'tiled', 'miraculous', 'attacked', 'czar', 'repose', 'convalescing', 'selecting', 'Meets', 'sublimity', 'madly', 'horrid', 'Aldrovandus', 'WAY', 'quarrel', 'systematic', 'concentrating', 'laughter', 'TO', 'NINE', 'tell', 'consciously', 'harmed', 'SPOUTINGS', 'daresn', 'glued', 'unfurnished', 'GOLDSMITH', 'tips', 'Lowering', 'cramped', 'distance', 'madest', 'knives', 'ribbons', 'premonitory', 'detestation', 'protesting', 'sallied', 'adhesiveness', 'II', 'revelry', 'Dragged', 'measure', 'offspring', 'insufficient', 'abaft', 'jambs']
#whaleはいくつあるか
>>> fdist1["whale"]
906
4,細かく文章から単語を取得する
①テキスト中の長い単語を調べる
これはそういった単語が文章の特徴を表しており、有益であると考えられるから
テキストの語彙の中から15文字よりよりも長い単語を探し出してみよう
>>> V = set(text1)
#長さが15文字以上の言葉だけ並べる
>>> long_words = [w for w in V if len(w) > 15]
>>> sorted(long_words)
['CIRCUMNAVIGATION', 'Physiognomically', 'apprehensiveness', 'cannibalistically', 'characteristically', 'circumnavigating', 'circumnavigation', 'circumnavigations', 'comprehensiveness', 'hermaphroditical', 'indiscriminately', 'indispensableness', 'irresistibleness', 'physiognomically', 'preternaturalness', 'responsibilities', 'simultaneousness', 'subterraneousness', 'supernaturalness', 'superstitiousness', 'uncomfortableness', 'uncompromisedness', 'undiscriminating', 'uninterpenetratingly']
②テキストに何度も出現する単語を探す
7文字よりも長く、7回よりも多く出現するすべての単語を抜き出す
>>> fdist5 = FreqDist(text5)
>>> sorted([w for w in set(text5) if len(w) > 7 and fdist5[w] > 7])
['#14-19teens', '#talkcity_adults', '((((((((((', '........', 'Question', 'actually', 'anything', 'computer', 'cute.-ass', 'everyone', 'football', 'innocent', 'listening', 'remember', 'seriously', 'something', 'together', 'tomorrow', 'watching']
コロケーションとバイグラム
コロケーションとは、非常に頻繁に共起する一連の単語列
例)red wine
コロケーションの特徴は、類似した意味を持つ単語での置き換えがしづらい事
例)maroon wine
テキストから単語のペアを抜き出してみる
>>> from nltk import bigrams
>>> list(bigrams({'more', 'is', 'said', 'than', 'done']))
than-doneのような単語のペアがバイグラムである
コロケーションとは、本質的には頻繁に出現するバイグラムのことであり、まれにしか使われない単語のバイグラムよりも、注意を払う価値があることが期待できる
次にバイグラムの出現頻度が個々の単語の出現頻度から期待される出現頻度より大きいものを抽出
>>> text4.collocations()
United States; fellow citizens; four years; years ago; Federal
Government; General Government; American people; Vice President; Old
World; Almighty God; Fellow citizens; Chief Magistrate; Chief Justice;
God bless; every citizen; Indian tribes; public debt; one another;
foreign nations; political parties
>>> text8.collocations()
would like; medium build; social drinker; quiet nights; non smoker;
long term; age open; Would like; easy going; financially secure; fun
times; similar interests; Age open; weekends away; poss rship; well
presented; never married; single mum; permanent relationship; slim
build