java 计算i 出现的次数_JAVA算法:按照给定的段落统计单词出现次数(JAVA代码)...
https://blog.csdn.net/seagal890/article/details/92067644
JAVA算法:按照給定的段落統計單詞出現次數(JAVA代碼)
寫一個 JAVA程序以統計一個文本文件?words.txt?中每個單詞出現的頻率。
為了簡單起見,你可以假設:
words.txt只包括小寫字母和?' '?。
每個單詞只由小寫字母組成。
單詞間由一個或多個空格字符分隔。
示例:
假設 words.txt 內容如下:
the day is sunny the the
the sunny is is
你的腳本應當輸出(以詞頻降序排列):
the 4
is 3
sunny 2
day 1
說明:
不要擔心詞頻相同的單詞的排序問題,每個單詞出現的頻率都是唯一的。
算法設計
package com.bean.algorithm.basic;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class CountWords {
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); // 獲取開始時間
String string = "";
Map map = new HashMap();
try {
//讀取文件
FileInputStream fis = new FileInputStream("G://CountWords.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String temp = "";
try {
while ((temp = br.readLine()) != null) {
string = string + temp;
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
// 分割字符串
StringTokenizer st = new StringTokenizer(string); // 用于切分字符串
//初始化計數器
int count;
//初始化word變量
String word;
while (st.hasMoreTokens()) {
//逗號,問號,句號,感嘆號,冒號,雙引號,單引號,換行符號
word = st.nextToken(",?.!:\"\"' '\n");
if (map.containsKey(word)) {
// HashMap 保存數據
count = map.get(word);
//計數器累加
map.put(word, count + 1);
} else {
map.put(word, 1);
}
}
// 排序
Comparator> valueComparator = new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
return o2.getValue() - o1.getValue();
}
};
// 輸出結果
List> list = new ArrayList>(map.entrySet());
Collections.sort(list, valueComparator);
System.out.println("---------------------Words分析結果 ——— 輸出結果----------");
for (Map.Entry entry : list) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
long endTime = System.currentTimeMillis(); // 獲取結束時間
System.out.println("程序運行時間: " + (endTime - startTime) + "ms");
}
}
樣例文本如下:
if you just want to try running findbugs against your own code, you can run findbugs using javawebstart. this will use our new gui under Java 1.5+ and our old gui under java 1.4. the new gui provides a number of new features, but requires java 1.5+. both use exactly the same analysis engine.
程序運行結果
---------------------Words分析結果 ——— 輸出結果----------
new:3
1:3
gui:3
use:2
our:2
java:2
5+:2
you:2
the:2
findbugs:2
under:2
but:1
code:1
against:1
own:1
run:1
your:1
running:1
can:1
number:1
features:1
same:1
engine:1
and:1
provides:1
of:1
if:1
just:1
Java:1
a:1
using:1
will:1
old:1
want:1
this:1
exactly:1
analysis:1
both:1
4:1
javawebstart:1
try:1
to:1
requires:1
程序運行時間: 6ms
標簽:段落,util,JAVA,java,word,單詞,map,new,import
來源: https://blog.csdn.net/mrlin6688/article/details/100556510
總結
以上是生活随笔為你收集整理的java 计算i 出现的次数_JAVA算法:按照给定的段落统计单词出现次数(JAVA代码)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样养成易瘦体质
- 下一篇: 蜂蜜腌柠檬可以减肥吗