gpt4 book ai didi

java - 文本分析中如何消除文本文件中的空格?

转载 作者:行者123 更新时间:2023-12-02 04:51:13 24 4
gpt4 key购买 nike

我正在尝试让我的程序显示文本文件中字母的频率,目前它正在显示文本文件中每个单词的频率。例如,如果文本文件中的单词是“i am a man”,它会为每个单词“i”、“am”、“a”、“man”输出 4 倍的字母频率。我需要它来分析它全部作为一个单词,因此删除空格并将其视为“iamaman”。

//

最佳答案

这不是文本中有空格的问题。事实上,在添加计数之前检查 Character.isLetter() 时,您已经注意忽略空格。

主要来说,您只需将 forwhile 循环放在迭代 token 的主循环之外进行最终计数。

import java.util.*;
import java.io.*;

public class J_<countlettersfilereader> {

public static void main(String[] args)throws Exception {
// open the file
Scanner console = new Scanner(System.in);
System.out.print("What is the name of the text file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));

//initialize array with 26 elements
int[] letterArray = new int[26];

while (input.hasNext()) {
String next = input.next().toLowerCase();

//run loop for each line incrementing per character
for (int i = 0; i < next.length(); i++) {
char characters = next.charAt(i);

//ignore all characters which aren't alphabetic
if (Character.isLetter(characters)) {

//if character is uppercase then convert to lowercase
characters = Character.toLowerCase(characters);

//populate array
int index = characters - 'a';
letterArray[index]++;
}}
}

int total = 0;
for(int i = 0; i < letterArray.length; i ++) {
total += letterArray[i];
}

for (char characters = 'a'; characters <= 'z'; characters++) {
int index = characters - 'a';
//print out the analysis
System.out.println("'" + characters + "' entered " + (((double)letterArray[index] / (double)total) * 100)
+ " percent");
}
}
}


$ cat abc.txt
a b c

$ java J_
What is the name of the text file? abc.txt
'a' entered 33.33333333333333 percent
'b' entered 33.33333333333333 percent
'c' entered 33.33333333333333 percent
'd' entered 0.0 percent
'e' entered 0.0 percent
'f' entered 0.0 percent
'g' entered 0.0 percent
'h' entered 0.0 percent
'i' entered 0.0 percent
'j' entered 0.0 percent
'k' entered 0.0 percent
'l' entered 0.0 percent
'm' entered 0.0 percent
'n' entered 0.0 percent
'o' entered 0.0 percent
'p' entered 0.0 percent
'q' entered 0.0 percent
'r' entered 0.0 percent
's' entered 0.0 percent
't' entered 0.0 percent
'u' entered 0.0 percent
'v' entered 0.0 percent
'w' entered 0.0 percent
'x' entered 0.0 percent
'y' entered 0.0 percent
'z' entered 0.0 percent

关于java - 文本分析中如何消除文本文件中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29219498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com