言語処理100本ノック 2015の第6章: 英語テキストの処理の10問です。
50. 文区切り
(. or ; or : or ? or !) → 空白文字 → 英大文字というパターンを文の区切りと見なし,入力された文書を1行1文の形式で出力せよ.
gist4437579cb8e6b64166d0de27b1ce49b4
// 最初の7文のみ抜粋 # go run q50.go Natural language processing From Wikipedia, the free encyclopedia Natural language processing (NLP) is a field of computer science, artificial intelligence, and linguistics concerned with the interactions between computers and human (natural) languages. As such, NLP is related to the area of humani-computer interaction. History The history of NLP generally starts in the 1950s, although work can be found from earlier periods. In 1950, Alan Turing published an article titled "Computing Machinery and Intelligence" which proposed what is now called the Turing test as a criterion of intelligence.
golangでは、lookaheadをサポートしていないので、次の一文のCapitalを回してあげるのがめんどうでした。
51. 単語の切り出し
空白を単語の区切りとみなし,50の出力を入力として受け取り,1行1単語の形式で出力せよ.ただし,文の終端では空行を出力せよ.
// q50の結果をtxtに保存しておく # go run q50.go > ../data/q50_out.txt
gist154c531bd00e06627a06871620015ece
// 最初の3文のみ抜粋 # go run q51.go Natural language processing From Wikipedia, the free encyclopedia Natural language processing (NLP) is a field of computer science, artificial intelligence, and linguistics concerned with the interactions between computers and human (natural) languages. As such, NLP is related to the area of humani-computer interaction.
Split
するだけなので余裕ですね。
52. ステミング
51の出力を入力として受け取り,Porterのステミングアルゴリズムを適用し,単語と語幹をタブ区切り形式で出力せよ. Pythonでは,Porterのステミングアルゴリズムの実装としてstemmingモジュールを利用するとよい.
// q51の結果をtxtに保存しておく # go run q51.go > ../data/q51_out.txt
gist8a0fc9ac1d9432a9da14bd87d91476ae
# go run q52.go // 最初の3文のみ抜粋 Natural natur language languag processing process From from Wikipedia, wikipedia, the the free free encyclopedia encyclopedia Natural natur language languag processing process (NLP) (nlp) is is a a field field of of computer comput science, science, artificial artifici intelligence, intelligence, and and linguistics linguist concerned concern with with the the interactions interact between between computers comput and and human human (natural) (natural) languages. languages. As as such, such, NLP nlp is is related relat to to the the area area of of humani-computer humani-comput interaction. interaction.
golangでのstemming実装としてGo Porter Stemmerを使いました。
53. Tokenization
Stanford Core NLPを用い,入力テキストの解析結果をXML形式で得よ.また,このXMLファイルを読み込み,入力テキストを1行1単語の形式で出力せよ.
// 公式サイトからDownloadし、zipを解凍したフォルダで以下のshellを実行すると、実行したディレクトリにxmlファイルが生成される # cd stanford-corenlp-full-2017-06-09 # ./corenlp.sh -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref -outputFormat xml -file <YOUR_PATH_TO>/nlp.txt # mv ./nlp.txt.xml ../data // xmlから自動的にgo structを作り出す # chidley -G -e "" ../data/nlp.txt.xml
gist9672a32d376e48be9bf55571c799db31
// 最初の1文のみ抜粋 # go run q53.go Natural language processing From Wikipedia , the free encyclopedia Natural language processing -LRB- NLP -RRB- is a field of computer science , artificial intelligence , and linguistics concerned with the interactions between computers and human -LRB- natural -RRB- languages .
Stanford Core NLPを初めて触ったので実行の仕方がわからなくて苦労しました。 golangで実装するのはxmlの読み込みからです。 前処理にも書きましたが、自分でgo structを定義し直すのは面倒なので、chidleyを使って自動生成させています。
54. 品詞タグ付け
Stanford Core NLPの解析結果XMLを読み込み,単語,レンマ,品詞をタブ区切り形式で出力せよ.
gist701c1fdd47b0777c7a6c8cbb1f663bb5
# go run q54.go Natural natural JJ language language NN processing processing NN From from IN Wikipedia Wikipedia NNP , , , the the DT free free JJ encyclopedia encyclopedia NN Natural natural JJ language language NN processing processing NN -LRB- -lrb- -LRB- NLP nlp NN -RRB- -rrb- -RRB- is be VBZ a a DT field field NN of of IN computer computer NN science science NN , , , artificial artificial JJ intelligence intelligence NN , , , and and CC linguistics linguistics NNS concerned concern VBN with with IN the the DT interactions interaction NNS between between IN computers computer NNS and and CC human human JJ -LRB- -lrb- -LRB- natural natural JJ -RRB- -rrb- -RRB- languages language NNS . . .
Q53から出力部分を変えるのみです。
55. 固有表現抽出
入力文中の人名をすべて抜き出せ.
gist6ee9809052e2294a98ef9237316525c2
# go run q55.go Alan Turing Joseph Weizenbaum MARGIE Schank Wilensky Meehan Lehnert Carbonell Lehnert Racter Jabberwacky Moore
Stanford Core NLPで解析を行うと、人名がNER
にPERSON
として現れてくるのでそれを抜き出してあげるだけです。
56. 共参照解析
Stanford Core NLPの共参照解析の結果に基づき,文中の参照表現(mention)を代表参照表現(representative mention)に置換せよ.ただし,置換するときは,「代表参照表現(参照表現)」のように,元の参照表現が分かるように配慮せよ.
gistf36c33493e5d272f743916088a8ad6f5
// 最初の5文のみ抜粋 # go run q56.go Natural language processing From Wikipedia , the free encyclopedia Natural language processing -LRB- NLP -RRB- is [the free encyclopedia Natural language processing -LRB- NLP -RRB-] (a field of computer science) , artificial intelligence , and linguistics concerned with the interactions between computers and human -LRB- natural -RRB- languages . As such , NLP is related to the area of humani-computer interaction . Many challenges in NLP involve natural language understanding , that is , enabling [computers] (computers) to derive meaning from human or natural language input , and others involve natural language generation . History The history of NLP generally starts in the 1950s , although work can be found from earlier periods . In 1950 , Alan Turing published an article titled `` Computing Machinery and Intelligence '' which proposed what is now called the [Alan Turing] (Turing) test as a criterion of intelligence .
参照表現を()
で括り、もとの代表参照表現を[]
で括っています。
全文まとめてStanford Core NLPで解析したからか微妙な解析結果もちらほらあるようです。
しかし一文ずつ解析すると文をまたがった表現が拾えないので全文まとめた解析としました。
57. 係り受け解析
Stanford Core NLPの係り受け解析の結果(collapsed-dependencies)を有向グラフとして可視化せよ.可視化には,係り受け木をDOT言語に変換し,Graphvizを用いるとよい.また,Pythonから有向グラフを直接的に可視化するには,pydotを使うとよい.
gistea1dc7dec630b507a0d3ce336acda10f
# go run q57.go # dot -T png ../data/q57.dot -o ../data/q57.png # open ../data/q57.png
最初の一文をグラフにしました。 Goでの有向グラフの書き方は以前まとめたのでこちらをご覧ください。
58. タプルの抽出
Stanford Core NLPの係り受け解析の結果(collapsed-dependencies)に基づき,「主語 述語 目的語」の組をタブ区切り形式で出力せよ.ただし,主語,述語,目的語の定義は以下を参考にせよ.
- 述語: nsubj関係とdobj関係の子(dependant)を持つ単語
- 主語: 述語からnsubj関係にある子(dependent)
- 目的語: 述語からdobj関係にある子(dependent)
gist01e16fef3dd5e2f567fa7b63896e239d
# go run q58.go challenges involve generation others involve generation understanding enabling computers Turing published article experiment involved translation ELIZA provided interaction patient exceeded base ELIZA provide response which structured information underpinnings discouraged sort that underlies approach Some produced systems which make decisions systems rely which that contains errors implementations involved coding algorithms take set Some produced systems which make decisions models have advantage they express certainty Systems have advantages Automatic make use that make decisions
mapで実装しようとしたらcannot assign to struct field
のエラーが出てハマりました。
59. S式の解析
Stanford Core NLPの句構造解析の結果(S式)を読み込み,文中のすべての名詞句(NP)を表示せよ.入れ子になっている名詞句もすべて表示すること.
gist6f587f61adc2ca08abf5eda2201d2dcb
// 最初の1文のみ抜粋 # go run q59.go (NP (JJ Natural)(NN language)(NN processing)) (NP (NNP Wikipedia)) (NP (NP (DT the)(JJ free)(NN encyclopedia)(JJ Natural)(NN language)(NN processing))(PRN (-LRB- -LRB-)(NP (NN NLP))(-RRB- -RRB-))) (NP (DT the)(JJ free)(NN encyclopedia)(JJ Natural)(NN language)(NN processing)) (NP (NN NLP)) (NP (NP (NP (DT a)(NN field))(PP (IN of)(NP (NN computer)(NN science))))(, ,)(NP (JJ artificial)(NN intelligence))(, ,)(CC and)(NP (NP (NNS linguistics))(VP (VBN concerned)(PP (IN with)(NP (NP (DT the)(NNS interactions))(PP (IN between)(NP (NP (NNS computers))(CC and)(NP (JJ human)(-LRB- -LRB-)(JJ natural)(-RRB- -RRB-)(NNS languages))))))))) (NP (NP (DT a)(NN field))(PP (IN of)(NP (NN computer)(NN science)))) (NP (DT a)(NN field)) (NP (NN computer)(NN science)) (NP (JJ artificial)(NN intelligence)) (NP (NP (NNS linguistics))(VP (VBN concerned)(PP (IN with)(NP (NP (DT the)(NNS interactions))(PP (IN between)(NP (NP (NNS computers))(CC and)(NP (JJ human)(-LRB- -LRB-)(JJ natural)(-RRB- -RRB-)(NNS languages)))))))) (NP (NNS linguistics)) (NP (NP (DT the)(NNS interactions))(PP (IN between)(NP (NP (NNS computers))(CC and)(NP (JJ human)(-LRB- -LRB-)(JJ natural)(-RRB- -RRB-)(NNS languages))))) (NP (DT the)(NNS interactions)) (NP (NP (NNS computers))(CC and)(NP (JJ human)(-LRB- -LRB-)(JJ natural)(-RRB- -RRB-)(NNS languages))) (NP (NNS computers)) (NP (JJ human)(-LRB- -LRB-)(JJ natural)(-RRB- -RRB-)(NNS languages)) --------------
S式の簡易パーサを実装したのでそちらを使いました。 Treeは再帰的に処理させやすくてよいですね。
感想
Stanford Core NLPはもう少し丁寧に触るとかなりおもしろそうですね。 ステミングあたりなどは言葉から知らなかったり、パーサを初めて書いてみたり色々収穫の多い章でした。
いつも通りコードはGithubにも置いてあります。