我有一个程序,用户可以输入一个句子,它可以将每个单词拆分成一个数组。另外,我需要计算每个单词的频率。例如, Apple是苹果手机,结果是 Apple-1;是-2;一个-1; A-1;电话-1 即可。
请帮我解决这个问题,我不知道如何计算每个单词的频率。
这是我的代码:
public static void main(String[] args)
{
while (true)
{
System.out.println("Enter a sentence:");
Scanner keyboard = new Scanner(System.in);
String sentence = keyboard.nextLine();
if (sentence.isEmpty()) // quit the program when user enter an empty string
{
break;
}
else
{
StringTokenizer st = new StringTokenizer(sentence);
List<String> sentenceElement = new ArrayList<String>();
while (st.hasMoreTokens())
{
sentenceElement.add(st.nextToken());
}
System.out.println(sentenceElement);
}
}
非常感谢!!
答案 0 :(得分:4)
您可以使用HashMap
,其中的字词为Key
,而Value
则出现:
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
String[] myPhrase = keyboard.nextLine().split(" ");
HashMap<String, Integer> myWordsCount = new HashMap<String, Integer>();
for (String s : myPhrase){
if (myWordsCount.containsKey(s)) myWordsCount.replace(s, myWordsCount.get(s) + 1);
else myWordsCount.put(s, 1);
}
System.out.println(myWordsCount);
}
<强>输出强>
One two three four and again three and four
{four=2, and=2, One=1, again=1, two=1, three=2}
答案 1 :(得分:0)
获取字符串数组后,您可以尝试从Java 10开始的以下代码。它使用流来获取频率图。
import java.util.Arrays;
import java.util.stream.Collectors;
public class StringFrequencyMap {
public static void main(String... args) {
String[] wordArray = {"Apple", "is", "an", "apple", "is", "a", "phone"};
var freqCaseSensitive = Arrays.stream(wordArray)
.collect(Collectors.groupingBy(x -> x, Collectors.counting()));
//If you want case insensitive then use
var freqCaseInSensitive = Arrays.stream(wordArray)
.collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));
System.out.println(freqCaseSensitive);
System.out.println(freqCaseInSensitive);
}
}
输出:
{a=1, apple=1, Apple=1, phone=1, is=2, an=1}
{a=1, apple=2, phone=1, is=2, an=1}