From 294d595513a36873bdcf19322a53148431e70dc2 Mon Sep 17 00:00:00 2001 From: jinluo123 <76473553+jinluo123@users.noreply.github.com> Date: Thu, 11 Mar 2021 13:44:47 +0800 Subject: [PATCH 01/16] Create 221801414 --- 221801414 | 1 + 1 file changed, 1 insertion(+) create mode 100644 221801414 diff --git a/221801414 b/221801414 new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/221801414 @@ -0,0 +1 @@ + From bb5fff05a002be7d59990e2495da7f32d03e6ba8 Mon Sep 17 00:00:00 2001 From: jinluo123 <76473553+jinluo123@users.noreply.github.com> Date: Thu, 11 Mar 2021 13:48:47 +0800 Subject: [PATCH 02/16] Delete 221801414 --- 221801414 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 221801414 diff --git a/221801414 b/221801414 deleted file mode 100644 index 8b137891..00000000 --- a/221801414 +++ /dev/null @@ -1 +0,0 @@ - From 91e7571cb300dad5aba50fba6dc548fd32c04999 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Fri, 12 Mar 2021 10:30:36 +0800 Subject: [PATCH 03/16] commit test commit test --- 221801414/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 221801414/README.md diff --git a/221801414/README.md b/221801414/README.md new file mode 100644 index 00000000..a3635a81 --- /dev/null +++ b/221801414/README.md @@ -0,0 +1,14 @@ + + +# PersonalProject-Java + + +传入参数如:input.txt output.txt + +功能:统计一个txt文件的字符数,行数,单词数,并输出数量前十的单词 + +作业链接:https://edu.cnblogs.com/campus/fzu/2021SpringSoftwareEngineeringPractice/homework/11740 + +博客链接:https://www.cnblogs.com/6666-6666/p/14438156.html + + From 9133aabeab327a6a193b3cec4a3d2a22d4056ff1 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 14:57:39 +0800 Subject: [PATCH 04/16] Update README.md test test test --- 221801414/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/221801414/README.md b/221801414/README.md index a3635a81..f792c311 100644 --- a/221801414/README.md +++ b/221801414/README.md @@ -1,4 +1,8 @@ - +test +test +test +test +test # PersonalProject-Java From d281b799617f543add93f9596eeddffd9015510c Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 15:49:36 +0800 Subject: [PATCH 05/16] Create Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit countCharAndWord功能 --- 221801414/src/wordCount/Lib.java | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 221801414/src/wordCount/Lib.java diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java new file mode 100644 index 00000000..885095a9 --- /dev/null +++ b/221801414/src/wordCount/Lib.java @@ -0,0 +1,55 @@ +package wordCount; +import java.io.*; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +public class Lib { + private int charCount=0; + private int wordCount=0; + private int lineCount=0; + private static String wordMatch = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; + private BufferedReader reader=null; + private BufferedWriter writer=null; + private String fileInPath=""; + private String fileOutPath=""; + private HashMap hashMap = new HashMap(); + Lib(String fileIn,String fileOut){ + fileInPath=fileIn; + fileOutPath=fileOut; + } + public void countCharWord() throws IOException { + int str; + String buffer=""; + reader=new BufferedReader(new FileReader(fileInPath)); + while((str=reader.read())>=0 && str<=127 ) { + charCount++; + if(Character.isLetterOrDigit(str)) { + buffer+=(char)str; + } + else { + if(isWord(buffer)) { + wordCount++; + String buf = buffer.toLowerCase(); + if (hashMap.containsKey(buf)) { + int occurs=hashMap.get(buf); + hashMap.put(buf, occurs+1); + } + else { + hashMap.put(buf, 1); + } + buf=""; + } + buffer=""; + } + } + reader.close(); + } + public boolean isWord(String buffer) { + if(buffer.matches(wordMatch)) + return true; + else + return false; + } + +} From 80523716f952656bed0de185534de0d188916cf9 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 15:55:12 +0800 Subject: [PATCH 06/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加countLine()功能 --- 221801414/src/wordCount/Lib.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java index 885095a9..69c7844e 100644 --- a/221801414/src/wordCount/Lib.java +++ b/221801414/src/wordCount/Lib.java @@ -9,6 +9,7 @@ public class Lib { private int wordCount=0; private int lineCount=0; private static String wordMatch = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; + private static String lineMatch = "(^|\n)\\s*\\S+"; private BufferedReader reader=null; private BufferedWriter writer=null; private String fileInPath=""; @@ -52,4 +53,25 @@ public boolean isWord(String buffer) { return false; } + public void countLine() throws IOException { + int str; + StringBuilder builder = new StringBuilder(); + try { + reader=new BufferedReader(new FileReader(fileInPath)); + while ((str=reader.read())!=-1) + { + builder.append((char)str); + } + Pattern pattern = Pattern.compile(lineMatch); + Matcher matcher = pattern.matcher(builder); + while (matcher.find()){ + lineCount++; + } + } catch (IOException e) { + e.printStackTrace(); + } + finally { + reader.close(); + } + } } From 78f19df8d011f73cacad3acf0bd78a4faf05b5bb Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 16:02:29 +0800 Subject: [PATCH 07/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加sortWordOccurs()功能 --- 221801414/src/wordCount/Lib.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java index 69c7844e..e624e7f8 100644 --- a/221801414/src/wordCount/Lib.java +++ b/221801414/src/wordCount/Lib.java @@ -15,6 +15,8 @@ public class Lib { private String fileInPath=""; private String fileOutPath=""; private HashMap hashMap = new HashMap(); + private HashMap hashMaps = new HashMap(); + private ArrayList> List; Lib(String fileIn,String fileOut){ fileInPath=fileIn; fileOutPath=fileOut; @@ -52,7 +54,6 @@ public boolean isWord(String buffer) { else return false; } - public void countLine() throws IOException { int str; StringBuilder builder = new StringBuilder(); @@ -74,4 +75,11 @@ public void countLine() throws IOException { reader.close(); } } + public void sortWordOccurs() { + hashMaps = hashMap.entrySet().stream().sorted(Map.Entry.comparingByValue() + .reversed().thenComparing(Map.Entry.comparingByKey())).limit(10) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, + (e1, e2) -> e1, LinkedHashMap::new)); + List = new ArrayList>(hashMaps.entrySet()); + } } From e17650de5a53b100179200806c7d4ca4b1938c4c Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 16:06:10 +0800 Subject: [PATCH 08/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加printFile函数 --- 221801414/src/wordCount/Lib.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java index e624e7f8..03b5cd5a 100644 --- a/221801414/src/wordCount/Lib.java +++ b/221801414/src/wordCount/Lib.java @@ -82,4 +82,16 @@ public void sortWordOccurs() { (e1, e2) -> e1, LinkedHashMap::new)); List = new ArrayList>(hashMaps.entrySet()); } + public void printFile() throws IOException { + writer=new BufferedWriter(new FileWriter(fileOutPath)); + writer.write("characters: " + charCount + "\n"); + writer.write("words: " + wordCount + "\n"); + writer.write("lines: " + lineCount + "\n"); + + for(HashMap.Entry map:List) + { + writer.write(map.getKey() + ": " + map.getValue() + "\n"); + } + writer.close(); + } } From ac29da0fae00751228ea9d870232d2149be042ba Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 16:09:47 +0800 Subject: [PATCH 09/16] Create wordCount.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加wordCount.java --- 221801414/src/wordCount/wordCount.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 221801414/src/wordCount/wordCount.java diff --git a/221801414/src/wordCount/wordCount.java b/221801414/src/wordCount/wordCount.java new file mode 100644 index 00000000..ccb1efc7 --- /dev/null +++ b/221801414/src/wordCount/wordCount.java @@ -0,0 +1,26 @@ +package wordCount; +import java.io.*; +import java.util.*; +public class wordCount { + private String fileIn; + private String fileOut; + public wordCount(String fileInPath,String fileOutPath) { + this.fileIn=fileInPath; + this.fileOut=fileOutPath; + } + public void Counting() { + Lib lib=new Lib(fileIn,fileOut); + try { + lib.countCharWord(); + lib.countLine(); + lib.sortWordOccurs(); + lib.printFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + public static void main(String[] args) { + wordCount wordcount=new wordCount(args[0],args[1]); + wordcount.Counting(); + } +} From 90b855649966cc2d5424aa52283226b81e4d105b Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 16:16:16 +0800 Subject: [PATCH 10/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit countLine的一点改动 --- 221801414/src/wordCount/Lib.java | 39 ++++++++++++-------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java index 03b5cd5a..5ecf760c 100644 --- a/221801414/src/wordCount/Lib.java +++ b/221801414/src/wordCount/Lib.java @@ -8,8 +8,9 @@ public class Lib { private int charCount=0; private int wordCount=0; private int lineCount=0; - private static String wordMatch = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; - private static String lineMatch = "(^|\n)\\s*\\S+"; + private StringBuilder builder = new StringBuilder(); + private final static String wordMatch = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; + private final static String lineMatch = "(^|\n)\\s*\\S+"; private BufferedReader reader=null; private BufferedWriter writer=null; private String fileInPath=""; @@ -25,14 +26,16 @@ public void countCharWord() throws IOException { int str; String buffer=""; reader=new BufferedReader(new FileReader(fileInPath)); + while((str=reader.read())>=0 && str<=127 ) { charCount++; + builder.append((char)str); if(Character.isLetterOrDigit(str)) { buffer+=(char)str; } else { if(isWord(buffer)) { - wordCount++; + wordCount++; String buf = buffer.toLowerCase(); if (hashMap.containsKey(buf)) { int occurs=hashMap.get(buf); @@ -55,25 +58,11 @@ public boolean isWord(String buffer) { return false; } public void countLine() throws IOException { - int str; - StringBuilder builder = new StringBuilder(); - try { - reader=new BufferedReader(new FileReader(fileInPath)); - while ((str=reader.read())!=-1) - { - builder.append((char)str); - } - Pattern pattern = Pattern.compile(lineMatch); - Matcher matcher = pattern.matcher(builder); - while (matcher.find()){ - lineCount++; - } - } catch (IOException e) { - e.printStackTrace(); - } - finally { - reader.close(); - } + Pattern pattern = Pattern.compile(lineMatch); + Matcher matcher = pattern.matcher(builder); + while (matcher.find()){ + lineCount++; + } } public void sortWordOccurs() { hashMaps = hashMap.entrySet().stream().sorted(Map.Entry.comparingByValue() @@ -87,11 +76,11 @@ public void printFile() throws IOException { writer.write("characters: " + charCount + "\n"); writer.write("words: " + wordCount + "\n"); writer.write("lines: " + lineCount + "\n"); - + for(HashMap.Entry map:List) { writer.write(map.getKey() + ": " + map.getValue() + "\n"); } - writer.close(); + writer.close(); } -} +} \ No newline at end of file From 7903a3673e4882ef2847dbc800f3ac7e5163ef5b Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sat, 13 Mar 2021 18:21:24 +0800 Subject: [PATCH 11/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 性能优化:去掉正则表达式 --- 221801414/src/wordCount/Lib.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/wordCount/Lib.java index 5ecf760c..2af3d383 100644 --- a/221801414/src/wordCount/Lib.java +++ b/221801414/src/wordCount/Lib.java @@ -9,7 +9,6 @@ public class Lib { private int wordCount=0; private int lineCount=0; private StringBuilder builder = new StringBuilder(); - private final static String wordMatch = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; private final static String lineMatch = "(^|\n)\\s*\\S+"; private BufferedReader reader=null; private BufferedWriter writer=null; @@ -52,10 +51,25 @@ public void countCharWord() throws IOException { reader.close(); } public boolean isWord(String buffer) { - if(buffer.matches(wordMatch)) + if (buffer.length()>=4) + { + char buf[] = buffer.toCharArray(); + for(char temp:buf) { + if(!Character.isLetterOrDigit(temp)) { + return false; + } + } + return true; + } + else + { + return false; + } + /*if(buffer.matches(wordMatch)) return true; else return false; + */ } public void countLine() throws IOException { Pattern pattern = Pattern.compile(lineMatch); @@ -63,6 +77,7 @@ public void countLine() throws IOException { while (matcher.find()){ lineCount++; } + } public void sortWordOccurs() { hashMaps = hashMap.entrySet().stream().sorted(Map.Entry.comparingByValue() From f941fda27e0997e3f528dcd3d94c803306ad988e Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sun, 14 Mar 2021 18:21:26 +0800 Subject: [PATCH 12/16] =?UTF-8?q?=E4=B8=8A=E4=BC=A0codestyle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上传codestyle文件,删除src目录下的wordCount改为将.java文件直接放在src下 --- 221801414/codestyle.md | 47 ++++++++++++++++++++ 221801414/src/{wordCount => }/Lib.java | 0 221801414/src/{wordCount => }/wordCount.java | 0 3 files changed, 47 insertions(+) create mode 100644 221801414/codestyle.md rename 221801414/src/{wordCount => }/Lib.java (100%) rename 221801414/src/{wordCount => }/wordCount.java (100%) diff --git a/221801414/codestyle.md b/221801414/codestyle.md new file mode 100644 index 00000000..bb56f865 --- /dev/null +++ b/221801414/codestyle.md @@ -0,0 +1,47 @@ +参照自《码出高效_阿里巴巴Java开发手册》 +## 缩进 +> + 缩进采用4个空格。 +## 变量命名 +> + 代码中的命名严禁使用拼音与英文混合的方式,更不允许直接使用中文的方式。 +> + 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束。 +> + 参数名、成员变量、局部变量都统一使用lowerCamelCase风格,必须遵从驼峰形式。 +> + 杜绝完全不规范的缩写,避免望文不知义。 + +## 每行最多字符数 +> + 单行字符数限制不超过 120个,超出需要换行,换行时遵循如下原则: + + 第二行相对第一行缩进 4个空格,从第三行开始,不再继续缩进,参考示例。 + 运算符与下文一起换行。 + 方法调用的点符号与下文一起换行。 + 在多个参数超长,逗号后进行换行。 + 在括号前不要换行,见反例。 + +## 函数最大行数 +> + 函数最大行数不要超过100行。 +## 函数、类命名 +> + 方法名使用lowerCamelCase风格,必须遵从驼峰形式。 +> + 类名使用UpperCamelCase风格,必须遵从驼峰形式。 +## 常量 +> + 常量命名全部大写,单词间用下划线隔开,力求语义表达完整清楚,不要嫌名字长。 +## 空行规则 +> + 大括号的使用约定。如果是大括号内为空,则简洁地写成{}即可,不需要换行;如果是非空代码块则: + + 左大括号前不换行。 + 左大括号后换行。 + 右大括号前换行。 + 右大括号后还有else等代码则不换行;表示终止右大括号后必须换行。 +> + 方法体内的执行语句组、变量的定义语句组、不同的业务逻辑之间或者不同的语义之间插入一个空行。相同业务逻辑和语义之间不需要插入空行。 + +## 注释规则 +> + 方法内部单行注释,在被注释语句上方另起一行,使用//注释。方法内部多行注释使用/* */注释,注意与代码对齐。 +> + 与其“半吊子”英文来注释,不如用中文注释把问题说清楚。专有名词与关键字保持英文原文即可。 +## 空格 +> + if/for/while/switch/do等保留字与左右括号之间都必须加空格。 +> + 任何运算符左右必须加一个空格。 +> + 方法参数在定义和传入时,多个参数逗号后边必须加空格。 +## 其他规则 +> + 多个不同的运算符同时存在时合理使用括号来明确优先级。 +> + 在使用正则表达式时,利用好其预编译功能有效加快正则匹配速度。 + + 说明:不要在方法体内定义:Pattern pattern = Pattern.compile(规则); + diff --git a/221801414/src/wordCount/Lib.java b/221801414/src/Lib.java similarity index 100% rename from 221801414/src/wordCount/Lib.java rename to 221801414/src/Lib.java diff --git a/221801414/src/wordCount/wordCount.java b/221801414/src/wordCount.java similarity index 100% rename from 221801414/src/wordCount/wordCount.java rename to 221801414/src/wordCount.java From d5cbfa6a9a7fa5c8433031176bf44b022a7bfbbc Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sun, 14 Mar 2021 18:28:50 +0800 Subject: [PATCH 13/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=81=B5=E5=BE=AAcodestyle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改代码遵循codestyle --- 221801414/src/Lib.java | 84 ++++++++++++++++++++---------------- 221801414/src/wordCount.java | 16 ++++--- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/221801414/src/Lib.java b/221801414/src/Lib.java index 2af3d383..768269de 100644 --- a/221801414/src/Lib.java +++ b/221801414/src/Lib.java @@ -5,57 +5,63 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; public class Lib { - private int charCount=0; - private int wordCount=0; - private int lineCount=0; + private int charCount = 0; + private int wordCount = 0; + private int lineCount = 0; + private static final int MAX_NUM = 10; private StringBuilder builder = new StringBuilder(); - private final static String lineMatch = "(^|\n)\\s*\\S+"; - private BufferedReader reader=null; - private BufferedWriter writer=null; - private String fileInPath=""; - private String fileOutPath=""; + //private static final String WORD_REGEX_RULE = "[a-zA-Z]{4,}[a-zA-Z0-9]*"; + //private static final Pattern wordPattern = Pattern.compile(WORD_REGEX_RULE); + private static final String LINE_REGEX_RULE = "(^|\n)\\s*\\S+"; + private static final Pattern linePattern = Pattern.compile(LINE_REGEX_RULE); + private BufferedReader reader = null; + private BufferedWriter writer = null; + private String fileInPath = ""; + private String fileOutPath = ""; private HashMap hashMap = new HashMap(); private HashMap hashMaps = new HashMap(); private ArrayList> List; - Lib(String fileIn,String fileOut){ - fileInPath=fileIn; - fileOutPath=fileOut; + + Lib(String fileIn, String fileOut) { + fileInPath = fileIn; + fileOutPath = fileOut; } + public void countCharWord() throws IOException { int str; - String buffer=""; + String buffer = ""; reader=new BufferedReader(new FileReader(fileInPath)); - while((str=reader.read())>=0 && str<=127 ) { + while((str=reader.read()) >= 0 && str <= 127 ) { charCount++; builder.append((char)str); - if(Character.isLetterOrDigit(str)) { - buffer+=(char)str; + if (Character.isLetterOrDigit(str)) { + buffer += (char)str; } else { - if(isWord(buffer)) { + if (isWord(buffer)) { wordCount++; - String buf = buffer.toLowerCase(); - if (hashMap.containsKey(buf)) { - int occurs=hashMap.get(buf); - hashMap.put(buf, occurs+1); + String lowerBuffer = buffer.toLowerCase(); + if (hashMap.containsKey(lowerBuffer)) { + int occurs = hashMap.get(lowerBuffer); + hashMap.put(lowerBuffer, occurs+1); } else { - hashMap.put(buf, 1); + hashMap.put(lowerBuffer, 1); } - buf=""; + lowerBuffer = ""; } - buffer=""; + buffer = ""; } } reader.close(); } public boolean isWord(String buffer) { - if (buffer.length()>=4) + if (buffer.length() >= 4) { char buf[] = buffer.toCharArray(); - for(char temp:buf) { - if(!Character.isLetterOrDigit(temp)) { + for (char temp : buf) { + if (!Character.isLetterOrDigit(temp)) { return false; } } @@ -65,35 +71,37 @@ public boolean isWord(String buffer) { { return false; } - /*if(buffer.matches(wordMatch)) + /*if(buffer.matches(WORD_REGEX_RULE)) return true; else return false; */ } + public void countLine() throws IOException { - Pattern pattern = Pattern.compile(lineMatch); - Matcher matcher = pattern.matcher(builder); - while (matcher.find()){ - lineCount++; + Matcher matcher = linePattern.matcher(builder); + while (matcher.find()) { + lineCount++; } } + public void sortWordOccurs() { - hashMaps = hashMap.entrySet().stream().sorted(Map.Entry.comparingByValue() - .reversed().thenComparing(Map.Entry.comparingByKey())).limit(10) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, - (e1, e2) -> e1, LinkedHashMap::new)); - List = new ArrayList>(hashMaps.entrySet()); + hashMaps = hashMap.entrySet().stream() + .sorted(Map.Entry.comparingByValue() + .reversed().thenComparing(Map.Entry.comparingByKey())).limit(MAX_NUM) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue + , (e1, e2) -> e1, LinkedHashMap::new)); + List = new ArrayList > (hashMaps.entrySet()); } + public void printFile() throws IOException { writer=new BufferedWriter(new FileWriter(fileOutPath)); writer.write("characters: " + charCount + "\n"); writer.write("words: " + wordCount + "\n"); writer.write("lines: " + lineCount + "\n"); - for(HashMap.Entry map:List) - { + for(HashMap.Entry map : List) { writer.write(map.getKey() + ": " + map.getValue() + "\n"); } writer.close(); diff --git a/221801414/src/wordCount.java b/221801414/src/wordCount.java index ccb1efc7..f261ae59 100644 --- a/221801414/src/wordCount.java +++ b/221801414/src/wordCount.java @@ -1,15 +1,17 @@ package wordCount; import java.io.*; import java.util.*; -public class wordCount { +public class WordCount { private String fileIn; private String fileOut; - public wordCount(String fileInPath,String fileOutPath) { - this.fileIn=fileInPath; - this.fileOut=fileOutPath; + + public WordCount(String fileInPath, String fileOutPath) { + this.fileIn = fileInPath; + this.fileOut = fileOutPath; } public void Counting() { - Lib lib=new Lib(fileIn,fileOut); + Lib lib = new Lib(fileIn, fileOut); + try { lib.countCharWord(); lib.countLine(); @@ -20,7 +22,7 @@ public void Counting() { } } public static void main(String[] args) { - wordCount wordcount=new wordCount(args[0],args[1]); + WordCount wordcount = new WordCount(args[0],args[1]); wordcount.Counting(); } -} +} \ No newline at end of file From b4c39b51cc0f52f82e794af45eea37a5e637b883 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sun, 14 Mar 2021 19:33:08 +0800 Subject: [PATCH 14/16] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加注释 --- 221801414/src/Lib.java | 18 ++++++++++++++++-- .../src/{wordCount.java => WordCount.java} | 6 +++--- 2 files changed, 19 insertions(+), 5 deletions(-) rename 221801414/src/{wordCount.java => WordCount.java} (86%) diff --git a/221801414/src/Lib.java b/221801414/src/Lib.java index 768269de..db2c213d 100644 --- a/221801414/src/Lib.java +++ b/221801414/src/Lib.java @@ -22,11 +22,13 @@ public class Lib { private HashMap hashMaps = new HashMap(); private ArrayList> List; - Lib(String fileIn, String fileOut) { + public Lib(String fileIn, String fileOut) { fileInPath = fileIn; fileOutPath = fileOut; } - + /* + * ͳַ͵ + */ public void countCharWord() throws IOException { int str; String buffer = ""; @@ -56,6 +58,9 @@ public void countCharWord() throws IOException { } reader.close(); } + /* + * жǷΪ + */ public boolean isWord(String buffer) { if (buffer.length() >= 4) { @@ -78,6 +83,9 @@ public boolean isWord(String buffer) { */ } + /* + * ͳ + */ public void countLine() throws IOException { Matcher matcher = linePattern.matcher(builder); while (matcher.find()) { @@ -86,6 +94,9 @@ public void countLine() throws IOException { } + /* + * hashMapеĵ + */ public void sortWordOccurs() { hashMaps = hashMap.entrySet().stream() .sorted(Map.Entry.comparingByValue() @@ -95,6 +106,9 @@ public void sortWordOccurs() { List = new ArrayList > (hashMaps.entrySet()); } + /* + * Ҫӡļ + */ public void printFile() throws IOException { writer=new BufferedWriter(new FileWriter(fileOutPath)); writer.write("characters: " + charCount + "\n"); diff --git a/221801414/src/wordCount.java b/221801414/src/WordCount.java similarity index 86% rename from 221801414/src/wordCount.java rename to 221801414/src/WordCount.java index f261ae59..4a34a1d5 100644 --- a/221801414/src/wordCount.java +++ b/221801414/src/WordCount.java @@ -2,8 +2,8 @@ import java.io.*; import java.util.*; public class WordCount { - private String fileIn; - private String fileOut; + private String fileIn; //ļ + private String fileOut; //ļ public WordCount(String fileInPath, String fileOutPath) { this.fileIn = fileInPath; @@ -25,4 +25,4 @@ public static void main(String[] args) { WordCount wordcount = new WordCount(args[0],args[1]); wordcount.Counting(); } -} \ No newline at end of file +} From 91676ace467de774475bf4d86bdaee89bfcf7790 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sun, 14 Mar 2021 21:58:37 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=E6=94=B9=E6=AD=A3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试发现错误 --- 221801414/src/Lib.java | 21 +++++++++++++++------ 221801414/src/WordCount.java | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/221801414/src/Lib.java b/221801414/src/Lib.java index db2c213d..8f14c506 100644 --- a/221801414/src/Lib.java +++ b/221801414/src/Lib.java @@ -1,4 +1,4 @@ -package wordCount; +package WordCount; import java.io.*; import java.util.*; import java.util.regex.Matcher; @@ -47,8 +47,7 @@ public void countCharWord() throws IOException { if (hashMap.containsKey(lowerBuffer)) { int occurs = hashMap.get(lowerBuffer); hashMap.put(lowerBuffer, occurs+1); - } - else { + } else { hashMap.put(lowerBuffer, 1); } lowerBuffer = ""; @@ -56,6 +55,16 @@ public void countCharWord() throws IOException { buffer = ""; } } + if (isWord(buffer)) { //ͳһ + wordCount++; + String lowerBuffer = buffer.toLowerCase(); + if (hashMap.containsKey(lowerBuffer)) { + int occurs = hashMap.get(lowerBuffer); + hashMap.put(lowerBuffer, occurs+1); + } else { + hashMap.put(lowerBuffer, 1); + } + } reader.close(); } /* @@ -64,9 +73,9 @@ public void countCharWord() throws IOException { public boolean isWord(String buffer) { if (buffer.length() >= 4) { - char buf[] = buffer.toCharArray(); - for (char temp : buf) { - if (!Character.isLetterOrDigit(temp)) { + char charArrayBuf[] = buffer.toCharArray(); + for (int i = 0; i < 4; i++) { + if (!Character.isLetter(charArrayBuf[i])) { return false; } } diff --git a/221801414/src/WordCount.java b/221801414/src/WordCount.java index 4a34a1d5..862840cb 100644 --- a/221801414/src/WordCount.java +++ b/221801414/src/WordCount.java @@ -1,4 +1,4 @@ -package wordCount; +package WordCount; import java.io.*; import java.util.*; public class WordCount { From 53e1488ed2ddecf1de28d980e07f4194179b26f8 Mon Sep 17 00:00:00 2001 From: jinluo123 <2391395313@qq.com> Date: Sun, 14 Mar 2021 23:07:16 +0800 Subject: [PATCH 16/16] Update Lib.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一点点代码规范改动 --- 221801414/src/Lib.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/221801414/src/Lib.java b/221801414/src/Lib.java index 8f14c506..2887693b 100644 --- a/221801414/src/Lib.java +++ b/221801414/src/Lib.java @@ -39,8 +39,7 @@ public void countCharWord() throws IOException { builder.append((char)str); if (Character.isLetterOrDigit(str)) { buffer += (char)str; - } - else { + } else { if (isWord(buffer)) { wordCount++; String lowerBuffer = buffer.toLowerCase(); @@ -55,14 +54,14 @@ public void countCharWord() throws IOException { buffer = ""; } } - if (isWord(buffer)) { //ͳһ + if (isWord(buffer)) { //жһַ wordCount++; String lowerBuffer = buffer.toLowerCase(); if (hashMap.containsKey(lowerBuffer)) { int occurs = hashMap.get(lowerBuffer); hashMap.put(lowerBuffer, occurs+1); } else { - hashMap.put(lowerBuffer, 1); + hashMap.put(lowerBuffer, 1); } } reader.close(); @@ -80,9 +79,7 @@ public boolean isWord(String buffer) { } } return true; - } - else - { + } else { return false; } /*if(buffer.matches(WORD_REGEX_RULE))