ノート
ノート/テキストマイニング
訪問者数 2087 最終更新 2011-04-18 (月) 10:44:07
#!/usr/bin/env python # encoding: utf-8 # -*- coding: utf-8 -*- ## To run with Kanji properly, the Python default encoding should be set to utf-8. import sys import codecs import re import pycha ## これを入れる sys.stdout = codecs.getwriter('utf_8')(sys.stdout) ### A magic for printing UTF-8 characters fname="input.txt" words = [] # wordsは語のリスト # ファイル読出し fd = open(fname, "r") txt = fd.read() # chasenの呼出し out = pycha.pycha(txt) # outはchasen出力に対応し、全体outは語goを要素とするリストとなっており、 # 語goは更に語幹、読み、原形、品詞、その他の要素から成る。その他部分は品詞による。 # 以下はそれを分解する処理の例。 sonota1 = '' sonota2 = '' for go in out: gokan = go[0] if len(go)>=2: yomi = go[1] if len(go)>=3: genkei = go[2] if len(go)>=4: m = r.match(go[3]) ##print "m.group()", m.group(), len(m.group()) if m: if m.group()[len(m.group())-1]=='-': hinshi = m.group()[:len(m.group())-1] else: hinshi = m.group() else: hinshi = go[3] if len(go)>=5: sonota1 = go[4] if len(go)>=6: sonota2 = go[5] #print gokan if hinshi=='名詞' or hinshi=='動詞' or hinshi=='形容詞': # たとえば名詞・動詞・形容詞の時だけwordsに追加する。 words.append([hinshi, gokan, genkei]) words.sort() # print words ### これでは表示がまずいので以下4行に置換え(2011/04/18) for w in words: for u in w: print u, print
原理
文書 | 単語1 | 単語2 | 単語3 | 単語4 | ... |
1 | tf*idf | tf*idf | tf*idf | tf*idf | ... |
2 | tf*idf | tf*idf | tf*idf | tf*idf | ... |
3 | tf*idf | tf*idf | tf*idf | tf*idf | ... |
... | tf*idf | tf*idf | tf*idf | tf*idf | ... |
プログラムと結果 ⇒ 剽窃1