標籤:util syn substr 關鍵字 inter color short 程式 turn
1. 建立一個類,實現統計文字檔中各類字元和字串的個數的功能,要求實現:
a) 按字元統計,輸出各個字元的數量
b) 按單詞統計,輸出各個單詞的數量
2. 在b)的基礎上實現一個類keywordIdentifier,讀入一個Java程式源檔案,輸出各個關鍵字的個數(注釋中出現的關鍵字不計入關鍵字個數)
思考:統計1:如果文字檔只包含26個英文字母並且用空格分離,那麼只需要使用數組就可以對字元計數,用空格分離得到字串可以對字串計數(是否區分大小寫問題)。如果文字檔是英文,但是包含各種標點及其他符號,則使用集合key-value的形式來對字元計數,按字元依次讀取,然後將26個字母以外的所有字元作為空白格處理,再使用空格將其分離,最後對字串統計計數。
關鍵字統計:需要所有關鍵字集合,對每行判斷是否為注釋,再對非注釋進行空格分離,對關鍵字計數(採用key-value形式)
1.統計字元和單詞 StringCounter.java
import java.util.Map;import java.util.TreeMap;public class StringCounter { private Map<String, Integer> charCount = new TreeMap<String, Integer>(); private Map<String, Integer> wordCount = new TreeMap<String, Integer>(); public void charCounter(String s) { Integer freq; for(int i = 0; i < s.length(); i++) { freq = charCount.get(s.substring(i, i+1)); if(freq == null) { freq = new Integer(1); } else { freq = new Integer(freq.intValue() + 1); } charCount.put(s.substring(i, i+1), freq); } } public void wordCounter(String s) { //將除過a-zA-Z0-9的其他符號轉為空白格,再將多個空格轉為一個空格 String s1 = s.replaceAll("\\W", " "); String s2 = s1.replaceAll(" +", " "); String[] s3 = s2.split(" "); Integer freq; for(String str : s3) { freq = wordCount.get(str); if(freq == null) { freq = new Integer(1); } else { freq = new Integer(freq.intValue() + 1); } wordCount.put(str, freq); } } public void output() { System.out.println(charCount); System.out.println(wordCount); }}
2.測試 使用的文字檔為隨意的英文文本
import java.io.IOException;import java.io.RandomAccessFile;public class CountTest { public static void main(String[] args) throws IOException { StringCounter sc = new StringCounter(); long filePoint = 0; String s; RandomAccessFile file = new RandomAccessFile("string.txt", "r"); long fileLength = file.length(); while(filePoint < fileLength) { s = file.readLine(); //處理及計數 sc.charCounter(s); sc.wordCounter(s); filePoint = file.getFilePointer(); } file.close(); sc.output(); }}
3.關鍵詞統計
先將所有關鍵字放入map中,以關鍵字為鍵,次數0為值,再讀文本統計個數
import java.io.IOException;import java.io.RandomAccessFile;import java.util.Map;import java.util.TreeMap;public class keywordIdentifier { private Map<String, Integer> keyWordCount = new TreeMap<String, Integer>(); public void keyWord() throws IOException { long filePoint = 0; String s; RandomAccessFile file = new RandomAccessFile("keyword.txt", "r"); long fileLength = file.length(); while(filePoint < fileLength) { s = file.readLine(); String[] s1 = s.split(" "); Integer freq = new Integer(0); for(String word : s1) { keyWordCount.put(word, freq); } filePoint = file.getFilePointer(); } file.close(); } public void keyWordCounter(String s){ int pos = s.indexOf("//"); if(pos == -1) { pos = s.length(); } String sub = s.substring(0, pos);
String sub1 = sub.replaceAll("\\W", " "); String str = sub1.replaceAll(" +", " "); String[] s1 = str.split(" "); Integer freq; for(String word : s1) { freq = keyWordCount.get(word); if(freq != null) { freq = new Integer(freq.intValue() + 1); keyWordCount.put(word, freq); } } } public void output() { System.out.println(keyWordCount); } }
4.測試關鍵詞統計
import java.io.*;public class CountTest { public static void main(String[] args) throws IOException { keywordIdentifier sc = new keywordIdentifier(); sc.keyWord(); long filePoint = 0; String s; RandomAccessFile file = new RandomAccessFile("CountTest.java", "r"); long fileLength = file.length(); while(filePoint < fileLength) { s = file.readLine(); sc.keyWordCounter(s); filePoint = file.getFilePointer(); } file.close(); sc.output(); }}
輸出:
{abstract=0, assert=0, boolean=0, break=0, byte=0, case=0, catch=0, char=0, class="1", const=0, continue=0, default=0, do=0, double=0, else=0, enum=0, extends=0, final=0, finally=0, float=0, for=0, goto=0, if=0, implements=0, import=1, instanceof=0, int=0, interface=0, long=2, native=0, new=2, package=1, private=0, protected=0, public=2, return=0, short=0, static=1, strictfp=0, super=0, switch=0, synchronized=0, this=0, throw=0, throws=1, transient=0, try=0, void=1, volatile=0, while=1}
文本統計器(Java)