diff --git a/221801414/README.md b/221801414/README.md new file mode 100644 index 00000000..f792c311 --- /dev/null +++ b/221801414/README.md @@ -0,0 +1,18 @@ +test +test +test +test +test + +# 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 + + 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/Lib.java b/221801414/src/Lib.java new file mode 100644 index 00000000..2887693b --- /dev/null +++ b/221801414/src/Lib.java @@ -0,0 +1,129 @@ +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 final int MAX_NUM = 10; + private StringBuilder builder = new StringBuilder(); + //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; + + public 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++; + builder.append((char)str); + if (Character.isLetterOrDigit(str)) { + buffer += (char)str; + } else { + 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); + } + lowerBuffer = ""; + } + 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(); + } + /* + * жǷΪ + */ + public boolean isWord(String buffer) { + if (buffer.length() >= 4) + { + char charArrayBuf[] = buffer.toCharArray(); + for (int i = 0; i < 4; i++) { + if (!Character.isLetter(charArrayBuf[i])) { + return false; + } + } + return true; + } else { + return false; + } + /*if(buffer.matches(WORD_REGEX_RULE)) + return true; + else + return false; + */ + } + + /* + * ͳ + */ + public void countLine() throws IOException { + Matcher matcher = linePattern.matcher(builder); + while (matcher.find()) { + lineCount++; + } + + } + + /* + * hashMapеĵ + */ + public void sortWordOccurs() { + 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) { + writer.write(map.getKey() + ": " + map.getValue() + "\n"); + } + writer.close(); + } +} \ No newline at end of file diff --git a/221801414/src/WordCount.java b/221801414/src/WordCount.java new file mode 100644 index 00000000..862840cb --- /dev/null +++ b/221801414/src/WordCount.java @@ -0,0 +1,28 @@ +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(); + } +}