1importjava.util.*;2importjava.io.*;34publicclass SHQ {56staticArrayList<ArrayList<String>> c =newArrayList<ArrayList<String>>();78staticArrayList<String> nam =newArrayList<String>();910publicstaticvoidmain(String[] args){1112try(BufferedWriter bw =newBufferedWriter(newFileWriter("write.txt"))){// 結果出力ファイル名13 bw.write("start");14 bw.newLine();15}catch(Exception e){16}1718Console co =System.console();19System.out.print("file name:");20String st ="";21while(!(st = co.readLine()).equals("end")){2223ADD(st);24 nam.add(st);25System.out.print("file name:");26}27System.out.println("end");28STT();2930}3132staticvoidSTT(){3334try(BufferedWriter bw =newBufferedWriter(newFileWriter("write.txt",35true))){36int count =0;37for(ArrayList<String> q : c){38 bw.write(nam.get(count)+"ファイルのtf,idf,tf×idf");39 bw.newLine();40for(String w : q){41 bw.write("単語"+ w +"のtf:");42int count2 =0;43for(String f : q){44if(f.equals(w))45 count2++;46}47 bw.write(""+(double) count2 / q.size());48 bw.newLine();4950 bw.write("単語"+ w +"のidf:");51int couny =0;52for(ArrayList<String> y : c){5354for(String h : y){55if(w.equals(h)){56 couny++;57break;58}59}6061}62// System.out.println((double)c.size()/couny);63double asd =Math.log((double) c.size()/ couny)+1;64 bw.write(""+ asd);65 bw.newLine();66 bw.write("単語"+ w +"のtf×idf:"67+(Math.log((double) c.size()/ couny)+1)68*((double) count2 / q.size()));69 bw.newLine();70 bw.newLine();71}72 count++;73 bw.newLine();74 bw.newLine();75 bw.newLine();76 bw.newLine();77}7879}catch(Exception e){80System.out.print("allerr");81}8283}8485staticvoidADD(String s){8687ArrayList<String> a =null;8889try(BufferedReader br =newBufferedReader(newFileReader(s))){90 a =newArrayList<String>();9192String data ="";9394while((data = br.readLine())!=null){9596String[] aa = data.split(" +");9798for(String sd : aa){99 a.add(sd);100}101102}103104}catch(Exception e){105System.out.println("err3");106}107if(a !=null){108 c.add(a);109}110111}112113
質問文のエラーを訂正したものが以下です
java
1importjava.util.List;2importjava.lang.String;34/**
5 * doc 文書 term 用語
6 */7publicclass TF1 {8publicstaticdoubletf(String term,String[] doc){9double result =0;10for(String word : doc){11if(term.equalsIgnoreCase(word))12 result++;13}14return(double) result / doc.length;15}1617publicstaticvoidmain(String[] args){18String str ="It is a Java beginner who is studying Java by self taught. I'd like to create a program that reads English text files in Java and displays TFIDF of each English word, but I can not do it well by all means. I tried to make only TF. I do not know the meaning of the error. Since I do not know how to read the file, I typed in English directly. I omitted it here because it is long. I also want to display descending order";1920String[] doc2 = str.split(" ");2122for(String words : doc2){23double tf1 =tf(words, doc2);24if(!str.equalsIgnoreCase(words)){25System.out.print(words +":");26System.out.println(tf1);27}28}29}30}
重複したものを表示させない・降順ソートした表示は以下です
java
1importjava.util.*;2importjava.lang.String;34/**
5 * doc 文書 term 用語
6 */7publicclass TF1 {8publicstaticdoubletf(String term,String[] doc){9double result =0;10for(String word : doc){11if(term.equalsIgnoreCase(word))12 result++;13}14return(double) result / doc.length;15}1617publicstaticvoidmain(String[] args){18String str ="It is a Java beginner who is studying Java by self taught. I'd like to create a program that reads English text files in Java and displays TFIDF of each English word, but I can not do it well by all means. I tried to make only TF. I do not know the meaning of the error. Since I do not know how to read the file, I typed in English directly. I omitted it here because it is long. I also want to display descending order";1920String[] doc2 = str.split(" ");21TreeMap<String,Double> c=newTreeMap<>();2223for(String words : doc2){24double tf1 =tf(words, doc2);25if(!str.equalsIgnoreCase(words)){26 c.put(words,tf1);27}2829}30System.out.println("重複した単語を表示させない");31for(String er:c.keySet()){32System.out.println(er+":"+c.get(er));33}34TreeMap<String,Double> c2=newTreeMap<>(newCX(c));35c2.putAll(c);36System.out.println("\n\n降順にソート");3738for(String er:c2.keySet()){39System.out.println(er+":"+c.get(er));40}41}424344}4546class CX implementsComparator<String>{47privateMap<String,Double> map;48publicCX(Map<String,Double> map){49this.map = map;50}51publicintcompare(String key1,String key2){52double value1 = map.get(key1);53double value2 = map.get(key2);54if(value1 == value2)55return key1.toLowerCase().compareTo(key2.toLowerCase());56elseif(value1 < value2)57return1;58else59return-1;60}61}62