编程作业

|这个作业属于哪个课程| https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018 |
|这个作业要求在哪里| https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879 |
|这个作业的目标| 学习使用github、撰写词汇统计程序 |
|其他参考文献| 《构建之法》 |

解题需求
1.统计文件的字符数(对应输出第一行):

只需要统计Ascii码,汉字不需考虑
空格,水平制表符,换行符,均算字符
2.统计文件的单词总数(对应输出第二行),单词:至少以4个英文字母开头,跟上字母数字符号,单词以分隔符分割,不区分大小写。

英文字母: A-Z,a-z
字母数字符号:A-Z, a-z,0-9
分割符:空格,非字母数字符号
例:file123是一个单词, 123file不是一个单词。file,File和FILE是同一个单词
3.统计文件的有效行数(对应输出第三行):任何包含非空白字符的行,都需要统计。

4.统计文件中各单词的出现次数(对应输出接下来10行),最终只输出频率最高的10个。

频率相同的单词,优先输出字典序靠前的单词。

例如,windows95,windows98和windows2000同时出现时,则先输出windows2000

输出的单词统一为小写格式

然后将统计结果输出到output.txt,输出的格式如下;其中word1和word2 对应具体的单词,number为统计出的个数;换行使用' ',编码统一使用UTF-8。

characters: number
words: number
lines: number
word1: number
word2: number
...

一、CharactersCount(字符数统计类)
public static int charactersCount(String filename) { File file = new File(filename); int cnt = 0;//总行数 try { BufferedReader br = new BufferedReader(new FileReader(file)); int temp; while ((temp = br.read()) != -1) { cnt ++; } br.close(); } catch(Exception e) { System.out.println("文件不存在!"); } return cnt; }

二、WordsCount(单词总数统计类)
public static String[] words = new String[1000];//单词数组 public static int wordsCount(String filename) { File file = new File(filename); String[] str = new String[1000];//仅包含字母和数字的字符串数组 int cnt = 0;//筛选出的字符串个数 int num = 0;//单词数 try { BufferedReader br = new BufferedReader(new FileReader(file)); String tempLine; while ((tempLine = br.readLine()) != null) { if (!tempLine.equals("")) { Pattern pattern = Pattern.compile("\s+|\W+"); String[] newStr = pattern.split(tempLine); for(String ss : newStr) { if (isLetterDigit(ss)) { str[cnt++] = ss; } } } } for (String ss : str) { if (isWord(ss)) { words[num++] = ss; } } br.close(); } catch(IOException e) { e.printStackTrace();//System.out.println("文件不存在!"); } return num; }

isLetterDigit函数
private static boolean isLetterDigit(String str) {//判断仅包含字母和数字的字符串 String regex = "^[a-z0-9A-Z]+$"; return str.matches(regex); }
isWord函数
private static boolean isWord(String str) {//判断是否为单词 if (str != null&&str.length() >= 4) {//此处因为要判断参数是否为空导致空指针异常 char ch1 = str.charAt(0); char ch2 = str.charAt(1); char ch3 = str.charAt(2); char ch4 = str.charAt(3); if (((ch1 >= 'A' && ch1 <= 'Z')||(ch1 >= 'a' && ch1 <= 'z')) &&((ch2 >= 'A' && ch2 <= 'Z')||(ch2 >= 'a' && ch2 <= 'z')) &&((ch3 >= 'A' && ch3 <= 'Z')||(ch3 >= 'a' && ch3 <= 'z')) &&((ch4 >= 'A' && ch4 <= 'Z')||(ch4 >= 'a' && ch4 <= 'z'))) return true; else return false; } else return false; } }
三、LinesCount(有效行数统计类)
public static int linesCount(String filename) { File file = new File(filename); int cnt = 0;//总行数 try { BufferedReader br = new BufferedReader(new FileReader(file)); String tempLine; while ((tempLine = br.readLine()) != null) { cnt++; if (tempLine.equals(""))//判断行为空白字符的行 cnt--; } br.close(); } catch(Exception e) { System.out.println("文件不存在!"); } return cnt; }
四、FrequentWordsCount(频词个数统计类)
public static void frequentWordsCount() { int size = WordsCount.words.length; Map<String,Integer> map = new HashMap<String,Integer>(); int cnt = 0; String[] words = new String[size]; System.arraycopy(WordsCount.words, 0, words, 0, size); for (int i = 0; i < size; i++) { if (words[i] != null) words[i] = words[i].toLowerCase(); } for (int i = 0; i < size; i++) { if (map.containsKey(words[i])) { map.put(words[i],map.get(words[i])+1); } else map.put(words[i],1); } map = sortByValueDescending(map); for(Entry<String, Integer> vo : map.entrySet()) { if (vo.getKey() !=null) { frequentWords[cnt] = vo.getKey(); num[cnt++] = vo.getValue(); } } }
sortByValueDescending函数
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { int compare = o1.getValue().compareTo(o2.getValue()); return -compare; } }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }

原文地址:https://www.cnblogs.com/Allen15773771785/p/14591009.html