数据结构 统计一段输入的字符串单词出现的频度
发布网友
发布时间:2022-12-21 04:28
我来回答
共1个回答
热心网友
时间:2023-06-28 05:30
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
/**
* 字典类,记录文章中出现过的所有单词及其次数
* @author Administrator
*
*/
public class Dictionary {
private HashMap< String, Integer > dictionary;
private int wordsCount;
/**
* 字典这个类的构造函数
*/
public Dictionary() {
dictionary = new HashMap< String, Integer >();
wordsCount = 0;
}
/**
* 向字典里插入一个单词
* @param word
*/
public void insert( String word ) {
if ( dictionary.containsKey( word ) ) {
int currentCount = dictionary.get( word );
dictionary.put( word, currentCount + 1 );
} else {
dictionary.put( word, 1 );
}
wordsCount++;
}
/**
* 取得字典里所有不同的单词
* @return
*/
public int getDifferentWordsNum() {
return dictionary.size();
}
/**
* 返回字典里的所有单词 * 其出现次数
* @return
*/
public int getAllWordsNum() {
return wordsCount;
}
/**
* 展示字典中存放的所有单词及其出现次数
*/
public void displayDictionary() {
for ( Iterator< String > it = dictionary.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
System.out.print( key );
System.out.print( ": " );
System.out.println( dictionary.get( key ) );
}
}
public static void main( String[] args ) throws Exception {
//这里放置你所说的段落
String passage = "public static void main( String[] args ) {";
Scanner scanner = new Scanner( passage );
Dictionary dict = new Dictionary();
while ( scanner.hasNextLine() ) {
String line =scanner.nextLine();
boolean isBlankLine = line.matches( "\\W" ) || line.length() == 0;
if ( isBlankLine ) {
continue;
}
String[] words = line.split( "\\W" );
for ( String word : words ) {
if ( word.length() != 0 ) {
dict.insert( word );
}
}
}
dict.displayDictionary();
}
}