![]() |
ノート/テキストマイニング/NLTKhttps://pepper.is.sci.toho-u.ac.jp:443/pepper/index.php?%A5%CE%A1%BC%A5%C8%2F%A5%C6%A5%AD%A5%B9%A5%C8%A5%DE%A5%A4%A5%CB%A5%F3%A5%B0%2FNLTK |
![]() |
ノート/テキストマイニング
訪問者数 5012 最終更新 2009-12-25 (金) 12:06:50
参考 Online Resources for Studying English Syntax, Words and Usage
> ノート/テキストマイニング
> ノート/テキストマイニング/テキストマイニングとシソーラス
> ノート/テキストマイニング/PubMed解析
> ノート/テキストマイニング/MeSH
> ノート/テキストマイニング/NLTK+StanfordParser
サイト:http://www.nltk.org/Home Linuxサーバー(FC10)へのインストール:http://www.nltk.org/downloadのソースインストレーションによる。いくつかのパッケージが必要。
解説本 http://www.nltk.org/book(2009/6出版予定、現在はWeb版) を見ながら徐々に試してゆく。
CFG http://www.informatics.susx.ac.uk/research/groups/nlp/carroll/cfg-resources/
こんなことができます。
#!/usr/bin/env python import sys import nltk ### Set inputs # ここでは文字列入力contentに定数から代入して作っている ### # 本番はファイル等から入力する? content = "Epidemiological studies have suggested that the long-term use of aspirin is associated with a decreased incidence of human malignancies, especially colorectal cancer. Since accumulating evidence indicates that peroxynitrite is critically involved in multistage carcinogenesis, this study was undertaken to investigate the ability of aspirin to inhibit peroxynitrite-mediated DNA damage. Peroxynitrite and its generator 3-morpholinosydnonimine (SIN-1) were used to cause DNA strand breaks in phiX-174 plasmid DNA. We demonstrated that the presence of aspirin at concentrations (0.25-2mM) compatible with amounts in plasma during chronic anti-inflammatory therapy resulted in a significant inhibition of DNA cleavage induced by both peroxynitrite and SIN-1. Moreover, the consumption of oxygen caused by 250muM SIN-1 was found to be decreased in the presence of aspirin, indicating that aspirin might affect the auto-oxidation of SIN-1. Furthermore, EPR spectroscopy using 5,5-dimethylpyrroline-N-oxide (DMPO) as a spin trap demonstrated the formation of DMPO-hydroxyl radical adduct (DMPO-OH) from authentic peroxynitrite, and that aspirin at 0.25-2mM potently diminished the radical adduct formation in a concentration-dependent manner. Taken together, these results demonstrate for the first time that aspirin at pharmacologically relevant concentrations can inhibit peroxynitrite-mediated DNA strand breakage and hydroxyl radical formation. These results may have implications for cancer intervention by aspirin." mytokenizer = nltk.data.load('tokenizers/punkt/english.pickle') # 外部からこんなものを用意しておく必要があるらしい sent = mytokenizer.tokenize(content) # これで処理。結果はリストになる。 for u in sent: print ">> " + u # リストの要素をそれぞれプリント
他の(たとえばStanfordの)パーザーの出力トリーを読込んでトリー内部表現にする ⇒ bracketparse(s)
そのトリーを操作する(デモとして書かれている) ⇒ demo()
下記では一部省略。コメントに表示した実行結果は、
import nltk nltk.tree.demo()
の出力。以下、demo()のソースを追いながら、動作を見てゆく。
from nltk import tree # 必要なtreeパッケージをimport # Demonstrate tree parsing. s = '(S (NP (DT the) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie))))' t = Tree(s) print "Convert bracketed string into tree:" print t とにかく木全体を表示 (S (NP (DT the) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie)))) print t.__repr__() 内部 repl を表示 Tree('S', [Tree('NP', [Tree('DT', ['the']), Tree('NN', ['cat'])]), Tree('VP', [Tree('VBD', ['ate']), Tree('NP', [Tree('DT', ['a']), Tree('NN', ['cookie'])])])]) print "Display tree properties:" print t.node # 木のノード(トップ) ⇒ S print t[0] # 木の最初の子ノード ⇒ (NP (DT the) (NN cat)) print t[1] # 2番目の子ノード ⇒ (VP (VBD ate) (NP (DT a) (NN cookie))) print t.height() # 木の高さ ⇒ 5 print t.leaves() # 木の葉 ⇒ ['the', 'cat', 'ate', 'a', 'cookie'] print t[1] # ⇒ (VP (VBD ate) (NP (DT a) (NN cookie))) print t[1,1] # ⇒ (NP (DT a) (NN cookie)) print t[1,1,0] # ⇒ (DT a) # Demonstrate tree modification. the_cat = t[0] # t[0]を変数the_catに代入 the_cat.insert(1, tree.bracket_parse('(JJ big)')) # insertを実行 print "Tree modification:" print t # (S (NP (DT the) (JJ big) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie)))) t[1,1,1] = tree.bracket_parse('(NN cake)') # t[1,1,1]に'(NN cake)'を代入 print t # (S (NP (DT the) (JJ big) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cake)))) # Demonstrate parsing of treebank output format. t = tree.bracket_parse(t.pprint()) # pprintでトリーをきれいに印刷出力した後、bracket_parseで再度読み込み print "Convert tree to bracketed string and back again:" print t # 同じものが読めるだけなので出力省略 # Demonstrate LaTeX output print "LaTeX output:" print t.pprint_latex_qtree() # LaTeX清書プログラムへの入力の生成(出力省略) # Demonstrate tree nodes containing objects other than strings t.node = ('test', 3) # tの(頂上)ノード(=Sだった)に文字でない要素('test', 3)を代入 print t # (('test', 3) (NP (DT the) (NP|<JJ-NN> (JJ big) (NN cat))) (VP (VBD ate) (NP (DT a) (NN cake))))
後は省略する。省略した操作は、トリー変換としてcollapse_unary()、 chomsky_normal_form()、確率トリーProbabilisticTree、プロダクションルール の生成productionsなど。
また、demo()のサンプルには無いが、ノードの「プロパティ」として品詞が書かれているので、それを取り出すには
print t[1,1,0].node # t[1,1,0]は(DT a)なので、左記はDTを出力。 なおprint t[1,1,0,0]はaを出力する。
すべてのサブトリーを取り出すには、
ss = t.subtrees() for u in ss: print u
これより先は、> ノート/テキストマイニング/NLTK+StanfordParser