Как посчитать, сколько раз символы появляются в тексте? - PullRequest
0 голосов
/ 29 января 2020

Во-первых, следующий код запускает текстовый файл с 37.000 символов (работает нормально). Я хочу рассчитать возможность появления каждого персонажа. Итак, для достижения этой цели мне нужно посчитать, сколько раз каждая буква появляется в файле test.txt.

File file = new File("test.txt");
        FileInputStream fileStream = new FileInputStream(file);
        InputStreamReader input = new InputStreamReader(fileStream);
        BufferedReader reader = new BufferedReader(input);

        String line;

        // Initializing counters
        int countWord = 0;
        int sentenceCount = 0;
        int characterCount = 0;
        int whitespaceCount = 0;
        int a,b,c,d,e,f,g,h,i,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z=0;

        // Reading line by line from the
        // file until a null is returned
        while((line = reader.readLine()) != null) {
            if(!(line.equals(""))) {
                characterCount += line.length();


                // \\s+ is the space delimiter in java
                String[] wordList = line.split("\\s+");

                countWord += wordList.length;
                whitespaceCount += countWord -1;

                // [!?.:]+ is the sentence delimiter in java
                String[] sentenceList = line.split("[!?.:]+");

                sentenceCount += sentenceList.length;
            }
        }

        System.out.println("Total number of characters = " + characterCount);
        System.out.println("Total number of whitespaces = " + whitespaceCount);
 }

Я думаю о следующем коде, но я уверен, что это что-то более эффективный с более коротким кодом.

while((line = reader.readLine()) != null)
  if(!(line.equals(""))) {
    characterCount += line.length();
    if (line.equals("a")){
        a++;
    }...
  //same for the rest letters.

Ответы [ 2 ]

1 голос
/ 29 января 2020

Это просто, если создать карту keyed за символом.

  1. Files.lines принимает входной файл и читает строки.
  2. flatMap отображает * От 1009 * до stream of characters
  3. Затем символы группируются по key/value парам character/count.
        Map<String, Long> freq = null;
        try {
        freq = Files.lines(Path.of("testfile.txt"))
                .flatMap(line -> Arrays.stream(line.split("")))
                .filter(str -> str.length() > 0)
                .collect(Collectors.groupingBy(chr -> chr,
                        Collectors.counting()));
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

Это утверждение

        freq.forEach((ch,cnt)->
           System.out.println("char = " + ch +"(" + 
                 Integer.toHexString(ch.charAt(0)) + ")" + " count = " + cnt));

Печатает что-то похожее на это с предоставленным шестнадцатеричным значением.

char =  (20) count = 10
char = a(61) count = 4
char = r(72) count = 1
char = s(73) count = 9
char = d(64) count = 2
char = t(74) count = 8
char = e(65) count = 3
char = h(68) count = 4
char = i(69) count = 6
char = .(2e) count = 2
char = n(6e) count = 3
char = o(6f) count = 2

0 голосов
/ 29 января 2020

Вот пример, который использует Map<Character, Long>:

public static void main(String[] args) {
    // example text, replace this with yours
    String text = "This is a pretty short text example...";
    // data structure holding a character and its count
    Map<Character, Long> chrCounts = new TreeMap<>();
    // check each character in the text
    for (char c : text.toCharArray()) {
        // if that character is present as key...
        if (chrCounts.containsKey(c)) {
            // ... increment its count in the Map
            chrCounts.put(c, chrCounts.get(c) + 1);
        } else {
            // otherwise add it to the map with a 1 as count
            chrCounts.put(c, 1l);
        }
    }

    // print the results
    chrCounts.forEach((c, count) -> System.out.println(c + ": " + count));
}

Напечатанный результат:

 : 6
.: 3
T: 1
a: 2
e: 4
h: 2
i: 2
l: 1
m: 1
o: 1
p: 2
r: 2
s: 3
t: 5
x: 2
y: 1

Обратите внимание, что это подсчитывает каждый символ в String, включая пробелы, запятые, точки, допросы и вопросительные знаки et c.

...