teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

訂正

2016/12/18 19:02

投稿

退会済みユーザー
answer CHANGED
@@ -150,4 +150,70 @@
150
150
  }
151
151
  }
152
152
  }
153
+ ```
154
+
155
+ 重複したものを表示させない・降順ソートした表示は以下です
156
+ ```java
157
+ import java.util.*;
158
+ import java.lang.String;
159
+
160
+ /**
161
+ * doc 文書 term 用語
162
+ */
163
+ public class TF1 {
164
+ public static double tf(String term, String[] doc) {
165
+ double result = 0;
166
+ for (String word : doc) {
167
+ if (term.equalsIgnoreCase(word))
168
+ result++;
169
+ }
170
+ return (double) result / doc.length;
171
+ }
172
+
173
+ public static void main(String[] args) {
174
+ String 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";
175
+
176
+ String[] doc2 = str.split(" ");
177
+ TreeMap<String,Double> c=new TreeMap<>();
178
+
179
+ for (String words : doc2) {
180
+ double tf1 = tf(words, doc2);
181
+ if (!str.equalsIgnoreCase(words)) {
182
+ c.put(words,tf1);
183
+ }
184
+
185
+ }
186
+ System.out.println("重複した単語を表示させない");
187
+ for(String er:c.keySet()){
188
+ System.out.println(er+":"+c.get(er));
189
+ }
190
+ TreeMap<String,Double> c2=new TreeMap<>(new CX(c));
191
+ c2.putAll(c);
192
+ System.out.println("\n\n降順にソート");
193
+
194
+ for(String er:c2.keySet()){
195
+ System.out.println(er+":"+c.get(er));
196
+ }
197
+ }
198
+
199
+
200
+ }
201
+
202
+ class CX implements Comparator<String> {
203
+ private Map<String, Double> map;
204
+ public CX(Map<String, Double> map) {
205
+ this.map = map;
206
+ }
207
+ public int compare(String key1, String key2) {
208
+ double value1 = map.get(key1);
209
+ double value2 = map.get(key2);
210
+ if(value1 == value2)
211
+ return key1.toLowerCase().compareTo(key2.toLowerCase());
212
+ else if(value1 < value2)
213
+ return 1;
214
+ else
215
+ return -1;
216
+ }
217
+ }
218
+
153
219
  ```

2

訂正

2016/12/18 19:02

投稿

退会済みユーザー
answer CHANGED
@@ -137,7 +137,7 @@
137
137
  }
138
138
 
139
139
  public static void main(String[] args) {
140
- String str = "An ant goes to a place which is a kind of a hole.";
140
+ String 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";
141
141
 
142
142
  String[] doc2 = str.split(" ");
143
143
 

1

訂正

2016/12/18 18:04

投稿

退会済みユーザー
answer CHANGED
@@ -116,4 +116,38 @@
116
116
  }
117
117
 
118
118
 
119
+ ```
120
+
121
+ 質問文のエラーを訂正したものが以下です
122
+ ```java
123
+ import java.util.List;
124
+ import java.lang.String;
125
+
126
+ /**
127
+ * doc 文書 term 用語
128
+ */
129
+ public class TF1 {
130
+ public static double tf(String term, String[] doc) {
131
+ double result = 0;
132
+ for (String word : doc) {
133
+ if (term.equalsIgnoreCase(word))
134
+ result++;
135
+ }
136
+ return (double) result / doc.length;
137
+ }
138
+
139
+ public static void main(String[] args) {
140
+ String str = "An ant goes to a place which is a kind of a hole.";
141
+
142
+ String[] doc2 = str.split(" ");
143
+
144
+ for (String words : doc2) {
145
+ double tf1 = tf(words, doc2);
146
+ if (!str.equalsIgnoreCase(words)) {
147
+ System.out.print(words + ":");
148
+ System.out.println(tf1);
149
+ }
150
+ }
151
+ }
152
+ }
119
153
  ```