課堂測試:使用者需求:英語的26 個字母的頻率在一本小說中是如何分布的?某類型文章中常出現的單詞是什麼?某作家最常用的詞彙是什麼?《Harry Potter》 中最常用的短語是什麼,等等。 要求:輸出單個檔案中的前 N 個最常出現的英語單詞,并将結果輸入到文本檔案中。
要求1:輸出某個英文文本檔案中 26 字母出現的頻率,由高到低排列,并顯示字母出現的百分比,精确到小數點後面兩位。 字母頻率 = 這個字母出現的次數 / (所有A-Z,a-z字母出現的總數) 如果兩個字母出現的頻率一樣,那麼就按照字典序排列。
源代碼為:
package Frequency;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class Frequency {
public static void main(String[] args)throws IOException
{
List list=new ArrayList<>();
DecimalFormat df=new DecimalFormat("######0.00");
FileInputStream fip = new FileInputStream("C:\\\\Users\\\\向瑜\\\\eclipse-workspace\\\\Harry Potter\\\\Harry Potter and the Sorcerer's Stone.txt");//存放《Harry Potter》檔案的位址
InputStreamReader reader = new InputStreamReader(fip, "gbk");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
}
reader.close();
fip.close();
int i;
String A=sb.toString();
String M="abcdefghijklmnopqrstuvwxyz";
String temp = "";
char NUM[]=new char[A.length()];
char Z[]=new char[26];
int X[]=new int[26];
int MAX=0;
Z=M.toCharArray();
for(int k=0;k<26;k++)
{
X[k]=0;
for(i=0;i
{
NUM[i]=A.charAt(i);
if(Z[k]==NUM[i]||Z[k]==ch(NUM[i]))
{
X[k]++;
}
}
}
System.out.println("這篇文章中英文字母個數分别為:");
double sum=0;
System.out.println("排序如下:");
for(i=0;i<25;i++)
for(int k=0;k<25-i;k++)
{
if(X[k]
{
int temp2=X[k];
X[k]=X[k+1];
X[k+1]=temp2;
char temp3=Z[k];
Z[k]=Z[k+1];
Z[k+1]=temp3;
}
}
for(i=0;i<26;i++) {
sum=sum+X[i];
}
for(i=0;i<26;i++)
{
double jkl=(X[i])/sum*100;
System.out.println(Z[i]+"字母個數為:"+X[i]+"字母頻率為:"+df.format(jkl)+"%");
}
}
static char ch(char c)
{
if(!(c>=97&&c<=122))
c+=32;
return c;
}
}
運作結果截圖:

要求2:輸出單個檔案中的前 N 個最常出現的英語單詞。 作用:一個用于統計文本檔案中的英語單詞出現頻率的控制台程式; 單詞:以英文字母開頭,由英文字母和字母數字元号組成的字元串視為一個單詞。單詞以分隔符分割且不區分大小寫。在輸出時,所有單詞都用小寫字元表示。
源代碼為:
package Words;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Single
{
public static int n=0;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String s;
int count=0;
int num=1;
//作為FileReader和FileWriter讀取的對象
String file1="C:\\\\\\\\Users\\\\\\\\向瑜\\\\\\\\eclipse-workspace\\\\\\\\Harry Potter\\\\\\\\Harry Potter and the Sorcerer's Stone.txt";//存放《Harry Potter》檔案的位址
String file2="C:\\\\\\\\Users\\\\\\\\向瑜\\\\\\\\eclipse-workspace\\\\\\\\Harry Potter\\\\\\\\Harry Potter and the Sorcerer's Stone1.txt";//在存放《Harry Potter》檔案的位址下建立的空白檔案夾
try
{
BufferedReader a=new BufferedReader(new FileReader(file1));
BufferedWriter b=new BufferedWriter(new FileWriter(file2));
StringBuffer c=new StringBuffer();
//将檔案内容存入StringBuffer中
while((s = a.readLine()) != null)
{
//用于拼接字元串
c.append(s);
}
//将StringBuffer轉換成String,然後再将所有字元轉化成小寫字元
String m=c.toString().toLowerCase();
//比對由數字和26個字母組成的字元串
String [] d=m.split("[^a-zA-Z0-9]+");
//周遊數組将其存入Map中
Map myTreeMap=new TreeMap();
for(int i = 0; i < d.length; i++) {
//containsKey()方法用于檢查特定鍵是否在TreeMap中映射
if(myTreeMap.containsKey(d[i])) {
count = myTreeMap.get(d[i]);
myTreeMap.put(d[i], count + 1);
}
else {
myTreeMap.put(d[i], 1);
}
}
//通過比較器實作排序
List> list = new ArrayList>(myTreeMap.entrySet());
//按降序排序
Collections.sort(list, new Comparator>() {
public int compare(Entry k1, Entry k2) {
//傳回兩個單詞出現次數較多的那個單詞的出現次數
return k2.getValue().compareTo(k1.getValue());
}
});
System.out.println("請輸入要輸出頻率最高的前N個單詞:");
n=input.nextInt();
for(Map.Entry map : list) {
if(num <= n) {
//按内容輸出到指定檔案中去
b.write("出現次數第" + num + "的單詞為:" + map.getKey() + ",出現頻率為" + map.getValue() + "次");
//換行
b.newLine();
//輸出到程式控制台
System.out.println(map.getKey() + ":" + map.getValue());
num++;
}
//輸出完畢退出
else break;
}
//關閉檔案指針
a.close();
b.close();
}
catch(FileNotFoundException e)
{
System.out.println("找不到指定檔案");
}
catch(IOException e)
{
System.out.println("檔案讀取錯誤");
}
System.out.println("輸出完成");
}
}
運作截圖為: