TF IDF 完成
出来ました。
初心者なので、プログラムがきたないことは許して下さい・・・。
これでもがんばったんだよお!!
import net.moraleboost.mecab.Tagger;
import net.moraleboost.mecab.Node;
import java.util.Hashtable;
public class Test6 { double[] doubleArray = new double[10]; double allhz = 0; double TF; int r; int p = 0; int q = 0; int htango; int z; public void IDF(String text) throws Exception { System.out.println("Original text: " + text); System.out.println(); System.out.println("解析"); Tagger tagger = new Tagger("UTF-8", ""); Node node = tagger.parse(text); String surface[] = new String[100]; String feature[] = new String[100]; String kakunou[] = new String[100]; for (int i = 0; i < 30; i++) { surface[i] = node.next(); feature[i] = node.feature(); } int t = 0; double hz = 0; for (int i = 0; i < 30; i++) { String[] fruit = feature[i].split(",", 0); if (fruit[0].equals("名詞")) { kakunou[t] = surface[i]; t++; hz = hz + 1; } } Hashtable arakakunou = new Hashtable();// 同じ名詞が何個か数える for (q = 0; q < hz; q++) { arakakunou.put(kakunou[q], new Integer(0)); } for (q = 0; q < hz; q++) { p = (Integer) arakakunou.get(kakunou[q]) + 1; arakakunou.put(kakunou[q], new Integer(p)); } if (arakakunou.get("//調べたい語") != null) { htango = (Integer) arakakunou.get("//調べたい語"); } // 環境にINTが格納されている TF = htango / hz; // tfの計算 System.out.println("一つのレポート内で「調べたい語」が現れた数" + htango); System.out.println("一つのレポート内の全単語数" + hz); System.out.println("「調べたい語」のTF(ひとつのレポート内「調べたい語」が現れた数/一つのレポート内の全単語数:" + TF); htango = 0; if (arakakunou.get("調べたい語") != null) { r = 1; } z = z + r; r = 0; } public Test6() throws Exception { System.out.println("MeCab version: " + Tagger.version()); System.out.println(); String[] text = new String[10]; text[0] = "テキスト入力"; text[2] = "テキスト入力"; text[3] = "テキスト入力"; text[4] = "テキスト入力"; text[5] = "テキスト入力"; text[6] = "テキスト入力"; text[7] = "テキスト入力"; text[8] = "テキスト入力"; text[9] = "テキスト入力"; for (int i = 0; i < text.length; i++) { IDF(text[i]); doubleArray[i]= TF; System.out.println("TF:" + doubleArray[i] ); } double lg1; double lg2; // r = r +1; lg1 = Math.log(text.length / z); System.out.println("全部のレポートの数:" + text.length); System.out.println("全部のレポートの「調べたい語」の出現回数" + z); System.out.println("IDF:" + lg1); for (int p = 0; p < text.length; p++) { System.out.println("「調べたい語」のTF-IDF"+p+":"+ doubleArray[p]* lg1); } } public static void main(String[] args) throws Exception { Test6 test = new Test6(); } }