回答編集履歴

3

訂正

2016/12/18 19:02

投稿

退会済みユーザー
test CHANGED
@@ -303,3 +303,135 @@
303
303
  }
304
304
 
305
305
  ```
306
+
307
+
308
+
309
+ 重複したものを表示させない・降順ソートした表示は以下です
310
+
311
+ ```java
312
+
313
+ import java.util.*;
314
+
315
+ import java.lang.String;
316
+
317
+
318
+
319
+ /**
320
+
321
+ * doc 文書 term 用語
322
+
323
+ */
324
+
325
+ public class TF1 {
326
+
327
+ public static double tf(String term, String[] doc) {
328
+
329
+ double result = 0;
330
+
331
+ for (String word : doc) {
332
+
333
+ if (term.equalsIgnoreCase(word))
334
+
335
+ result++;
336
+
337
+ }
338
+
339
+ return (double) result / doc.length;
340
+
341
+ }
342
+
343
+
344
+
345
+ public static void main(String[] args) {
346
+
347
+ 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";
348
+
349
+
350
+
351
+ String[] doc2 = str.split(" ");
352
+
353
+ TreeMap<String,Double> c=new TreeMap<>();
354
+
355
+
356
+
357
+ for (String words : doc2) {
358
+
359
+ double tf1 = tf(words, doc2);
360
+
361
+ if (!str.equalsIgnoreCase(words)) {
362
+
363
+ c.put(words,tf1);
364
+
365
+ }
366
+
367
+
368
+
369
+ }
370
+
371
+ System.out.println("重複した単語を表示させない");
372
+
373
+ for(String er:c.keySet()){
374
+
375
+ System.out.println(er+":"+c.get(er));
376
+
377
+ }
378
+
379
+ TreeMap<String,Double> c2=new TreeMap<>(new CX(c));
380
+
381
+ c2.putAll(c);
382
+
383
+ System.out.println("\n\n降順にソート");
384
+
385
+
386
+
387
+ for(String er:c2.keySet()){
388
+
389
+ System.out.println(er+":"+c.get(er));
390
+
391
+ }
392
+
393
+ }
394
+
395
+
396
+
397
+
398
+
399
+ }
400
+
401
+
402
+
403
+ class CX implements Comparator<String> {
404
+
405
+ private Map<String, Double> map;
406
+
407
+ public CX(Map<String, Double> map) {
408
+
409
+ this.map = map;
410
+
411
+ }
412
+
413
+ public int compare(String key1, String key2) {
414
+
415
+ double value1 = map.get(key1);
416
+
417
+ double value2 = map.get(key2);
418
+
419
+ if(value1 == value2)
420
+
421
+ return key1.toLowerCase().compareTo(key2.toLowerCase());
422
+
423
+ else if(value1 < value2)
424
+
425
+ return 1;
426
+
427
+ else
428
+
429
+ return -1;
430
+
431
+ }
432
+
433
+ }
434
+
435
+
436
+
437
+ ```

2

訂正

2016/12/18 19:02

投稿

退会済みユーザー
test CHANGED
@@ -276,7 +276,7 @@
276
276
 
277
277
  public static void main(String[] args) {
278
278
 
279
- String str = "An ant goes to a place which is a kind of a hole.";
279
+ 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";
280
280
 
281
281
 
282
282
 

1

訂正

2016/12/18 18:04

投稿

退会済みユーザー
test CHANGED
@@ -235,3 +235,71 @@
235
235
 
236
236
 
237
237
  ```
238
+
239
+
240
+
241
+ 質問文のエラーを訂正したものが以下です
242
+
243
+ ```java
244
+
245
+ import java.util.List;
246
+
247
+ import java.lang.String;
248
+
249
+
250
+
251
+ /**
252
+
253
+ * doc 文書 term 用語
254
+
255
+ */
256
+
257
+ public class TF1 {
258
+
259
+ public static double tf(String term, String[] doc) {
260
+
261
+ double result = 0;
262
+
263
+ for (String word : doc) {
264
+
265
+ if (term.equalsIgnoreCase(word))
266
+
267
+ result++;
268
+
269
+ }
270
+
271
+ return (double) result / doc.length;
272
+
273
+ }
274
+
275
+
276
+
277
+ public static void main(String[] args) {
278
+
279
+ String str = "An ant goes to a place which is a kind of a hole.";
280
+
281
+
282
+
283
+ String[] doc2 = str.split(" ");
284
+
285
+
286
+
287
+ for (String words : doc2) {
288
+
289
+ double tf1 = tf(words, doc2);
290
+
291
+ if (!str.equalsIgnoreCase(words)) {
292
+
293
+ System.out.print(words + ":");
294
+
295
+ System.out.println(tf1);
296
+
297
+ }
298
+
299
+ }
300
+
301
+ }
302
+
303
+ }
304
+
305
+ ```