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

質問編集履歴

1

タイトルをかえました

2020/07/02 22:02

投稿

tapipi
tapipi

スコア13

title CHANGED
File without changes
body CHANGED
@@ -6,84 +6,4 @@
6
6
 
7
7
  今のところ、
8
8
  「\s+」1つ以上のスペースの正規表現というのは検索できたのですが、
9
- これ全体で何を指しているのかわかりません…(わかり次第追記はしていくつもりです)
9
+ これ全体で何を指しているのかわかりません…(わかり次第追記はしていくつもりです)
10
-
11
-
12
- ### 該当のソースコード
13
-
14
- ```java
15
- package practice.test20160812;
16
-
17
- import java.io.BufferedReader;
18
- import java.io.FileNotFoundException;
19
- import java.io.FileReader;
20
- import java.io.IOException;
21
- import java.util.ArrayList;
22
- import java.util.Collections;
23
- import java.util.HashMap;
24
- import java.util.List;
25
- import java.util.Map;
26
-
27
- public class Accumulate {
28
-
29
- private static final String FILE_PATH = "resource\RAMBLES_IN_FLORIDA_PART_1.txt";
30
-
31
- private static final String SEPARATOR = "(\s+?|\.|,|;)";
32
-
33
- public static void main(String[] args) {
34
-
35
- // 集計
36
- Map<String, Integer> map = new HashMap<>();
37
- try (FileReader fr = new FileReader(FILE_PATH);
38
- BufferedReader br = new BufferedReader(fr)){
39
- String line;
40
- while ((line = br.readLine()) != null) {
41
- String[] words = line.split(SEPARATOR);
42
- for (String word : words) {
43
- if (!word.isEmpty()) {
44
- if (map.containsKey(word)) {
45
- int count = map.get(word) + 1;
46
- map.put(word, count);
47
- } else {
48
- map.put(word, 1);
49
- }
50
- }
51
- }
52
-
53
- }
54
- } catch (FileNotFoundException e) {
55
- System.out.println("ファイルが見つかりませんでした。");
56
- } catch (IOException e) {
57
- System.out.println("読み取りに失敗しました。");
58
- }
59
-
60
- // 出現数で降順に並べ替え、つづりの長さ最大値取得
61
- List<String> list = new ArrayList<>();
62
- int maxLengthOfSpelling = 0;
63
- for (String key : map.keySet()) {
64
- list.add(key);
65
-
66
- if (maxLengthOfSpelling < key.length()) {
67
- maxLengthOfSpelling = key.length();
68
- }
69
- }
70
- Collections.sort(list, (o1, o2) -> {
71
- return - map.get(o1) + map.get(o2);
72
- });
73
-
74
- // 上位10件出力
75
- System.out.println("出現回数トップ10");
76
- String format = "%-" + maxLengthOfSpelling + "s: %3d";
77
- for (String word : list) {
78
- int count = map.get(word);
79
- if (10 <= count) {
80
- System.out.printf(format, word, count);
81
- System.out.println();
82
- }
83
- }
84
-
85
-
86
- }
87
-
88
- }
89
- ```