Java统计一篇英文单词出现次数

来源:互联网 发布:阴茎毛囊炎 知乎 编辑:程序博客网 时间:2024/05/01 13:16
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();
}
}