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();
}
}
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();
}
}
- Java统计一篇英文单词出现次数
- Java统计一篇文章中出现次数最多的汉字或英文单词 又出现次数的统计
- java 去英文单词出现的次数
- java统计一篇文章中单词出现的次数
- 【java】统计一篇英文文章每个字母出现的次数
- java统计一篇文章中单词出现的次数
- spark统计文献中每个英文单词出现的次数
- java实现读取一篇英文文章,统计其中每个单词出现的次数并排序输出
- Java实现统计一篇文章中每个单词出现的次数
- 黑马程序员——统计文件中每个英文单词出现的次数
- Java 统计英文单词
- 统计一篇文章中一个词组出现的次数
- 统计一篇英文中每个单词出现的次数
- 统计一篇文章中每个单词出现的次数
- java统计字符串中子字符串出现次数
- java按单词出现次数统计单词
- java统计中文字符出现次数
- 字符出现次数统计直方图-Java实现
- CodeforcesRound #136 (Div. 2) D. Little Elephant andArray
- 【leetcode】Reverse Linked List II
- sql server中sql语句中单引号怎么转义?
- c 调用 lua
- [阿里一道笔试题]排序的交换次数
- Java统计一篇英文单词出现次数
- 黑马程序员--多线程2
- Android基础-AppWidget入门
- FFMPEG源码分析:avformat_open_input()(媒体打开函数)
- cocos2d-x CCAction scrollMap
- vs编译器选项
- 安装win7 SP1后或者安装其他补丁后,在应用更新操作第XXX个文件时候遇到致命错误的解决办法
- c语言解析json数据
- 为基于Wordpress的网站添加网站Icon