すこしだけオブジェクト指向学んだーー★
下のプログラムがとってもきたなかったので書き換えました★
実はちょっとまだしたの方にまだバグがあるんだけど・・・・・><☆
プログラムって難しいなあ・・・・・・・・(涙)
新たに調整中ーーー♪
import net.moraleboost.mecab.MeCabException;
import net.moraleboost.mecab.Tagger;
import net.moraleboost.mecab.Node;
import java.util.Hashtable;
import java.net.*;
import java.nio.charset.CharacterCodingException;
import java.io.*;
import java.util.*;
public class Tfidf { ArrayList<String> kakunou = new ArrayList<String>(); Hashtable<String, Integer> arakakunou = new Hashtable<String, Integer>(); String text; // String[] text = new String[5]; String[] text1 = new String[300]; // ------------------------------------ // テキストをdiviveにいれるよ public Tfidf() throws Exception { } // 単語を区切る public void divide() {// MECABを使って単語を区切る try { int t = 0;// 格納に使う数だよ System.out.println("Original text: " + text); System.out.println("解析"); Tagger tagger = new Tagger("UTF-8", ""); Thread.sleep(20000); Node node = tagger.parse(text); String surface[] = new String[1000]; String feature[] = new String[1000]; int seiri = 0; for (int i = 0; i < 1000; i++) { surface[i] = node.next();// 区切った単語をとってきてください if (surface[i] != null) { seiri = seiri + 1; feature[i] = node.feature(); // 区切った単語の種類を見てね(名詞?形容詞?) } } for (int i = 0; i < seiri; i++) { String[] meisi = feature[i].split(",", 0);// コンマで区切って名詞を調べよう if (meisi[0].equals("名詞")) {// もし名詞だったら this.kakunou.add(surface[i]);// 格納してね。つまりkakunou[t]の中は名詞の文字ですよ。 t = t + 1; // hz = hz + 1; } } // return kakunou; } catch (MeCabException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } catch (CharacterCodingException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } catch (InterruptedException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } //区切った名詞だけの単語がカクノウに保存されました public void seiri() throws Exception { for (int q = 0; q < this.kakunou.size(); q++) { arakakunou.put(this.kakunou.get(q), new Integer(0)); } for (int q = 0; q < this.kakunou.size(); q++) { arakakunou.put(this.kakunou.get(q), new Integer(arakakunou.get(this.kakunou.get(q)) + 1)); } } public double count(String str) throws Exception { if (arakakunou.get(str) != null) { return arakakunou.get(str); } else { return 0; } } // 格納された値を入れるとTFが帰ってきますよ public double TF(String str) throws Exception { // 同じ名詞が何個か数える double TF; TF = this.count(str) / (double) this.kakunou.size(); return TF; } public double TorF(String str) throws Exception { if (count(str) != 0) { return 1; } else { return 0; } } // ----------------------------IDFを計算しますよーーーーーーーーーーーーーーーーーーーーーーーーーーーーー public static void main(String[] args) throws Exception { Tfidf[] test = new Tfidf[4]; double tf; double idf; for (int i = 0; i < 4; i++) { test[i] = new Tfidf(); } test[0].text="調べてたいテキスト"; test[1].text="調べたいテキスト"; test[2].text="調べたいテキスト"; test[3].text="調べたいテキスト"; for (int i = 0; i < 4; i++) { // test.divide(test.text[i]); test[i].divide(); test[i].seiri(); tf = test[i].TF("調べる単語"); // test[i].IDF(4); idf = Math.log(4 / (test[0].TorF("調べる単語") + test[1].TorF("調べる単語") + test[2].TorF("調べる単語") + test[3].TorF("調べる単語"))); System.out.println(test[0].TorF("調べる単語") + test[1].TorF("調べる単語") + test[2].TorF("調べる単語") + test[3].TorF("調べる単語")); double tfidf = tf * idf; System.out.println("TFIDF" + tfidf); } } }
まだまだだけどがんばりまーーす★☆