diff --git a/201621123001/.keep b/201621123001/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/201621123001/WordCountTest.java b/201621123001/WordCountTest.java new file mode 100644 index 0000000000000000000000000000000000000000..89f1b73950e45a876201188059fd3a5ae03a6eb4 --- /dev/null +++ b/201621123001/WordCountTest.java @@ -0,0 +1,70 @@ +package Mysoftwork; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class WordCountTest { + + @Test + public void testCharCount1() throws Exception{//测试中文无效 + //fail("Not yet implemented"); + String path ="input1.txt"; + assertEquals("characters: 0",WordCount.charCount(path)); + } + + @Test + public void testWordCount1() throws Exception{ + String path ="input1.txt"; + assertEquals("words:0",WordCount.wordCount(path)); + } + + @Test + public void testLineCount1() throws Exception { + String path ="input1.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + @Test + public void testCharCount2() throws Exception{//测试按顺序输出 + //fail("Not yet implemented"); + String path ="input2.txt"; + assertEquals("characters: 21",WordCount.charCount(path)); + } + + @Test + public void testWordCount2() throws Exception{ + String path ="input2.txt"; + assertEquals("words:2",WordCount.wordCount(path)); + } + + @Test + public void testLineCount2() throws Exception { + String path ="input2.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + @Test + public void testCharCount3() throws Exception{//测试不区分大小写 + //fail("Not yet implemented"); + String path ="input3.txt"; + assertEquals("characters: 21",WordCount.charCount(path)); + } + + @Test + public void testWordCount3() throws Exception{ + String path ="input3.txt"; + assertEquals("words:2",WordCount.wordCount(path)); + } + + @Test + public void testLineCount3() throws Exception { + String path ="input3.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + +} diff --git a/201621123001/input1.txt b/201621123001/input1.txt new file mode 100644 index 0000000000000000000000000000000000000000..c15250bc0d2c9c7a1b58295690aa97f94573c5b2 --- /dev/null +++ b/201621123001/input1.txt @@ -0,0 +1 @@ +我是张艺琳 \ No newline at end of file diff --git a/201621123001/input2.txt b/201621123001/input2.txt new file mode 100644 index 0000000000000000000000000000000000000000..fac3e2640b3dc23841343ada03da0baea03ceb5c --- /dev/null +++ b/201621123001/input2.txt @@ -0,0 +1 @@ +windows2000 windows98 \ No newline at end of file diff --git a/201621123001/src/.keep b/201621123001/src/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/201621123001/src/WordCount.java b/201621123001/src/WordCount.java new file mode 100644 index 0000000000000000000000000000000000000000..217be21883872b39ab88ce6c277d968e812a7c1f --- /dev/null +++ b/201621123001/src/WordCount.java @@ -0,0 +1,120 @@ +package Mysoftwork; + +import java.io.*; +import java.util.*; + +public class WordCount { + public static String resultPath = "/Users/zhangyilin/IdeaProjects/softwork2/src/result.txt"; + public static List lists = new ArrayList<>(); + + public static void main(String[] args) throws Exception { + Scanner input = new Scanner(System.in); + Map treeMap = new TreeMap(); + System.out.println("请输入文件名:"); + String path = input.next(); +// int wordsNum = 0;//单词总数 +// InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 +// BufferedReader br = new BufferedReader(isr); +// String str = br.readLine(); +// isr.close();//关闭 + System.out.println(charCount(path)); + System.out.println(wordCount(path)); + System.out.println(lineCount(path)); + for (int i = 0; i < lists.size(); i++) { + String s = lists.get(i).toLowerCase(); + if (!treeMap.containsKey(s)) { + treeMap.put(s, 1); + } else { + int n = treeMap.get(s); + treeMap.put(s, n + 1); + } + } + System.out.println(sortWord(treeMap)); + writefile(resultPath,charCount(path)+"\n"+wordCount(path)+"\n"+ + lineCount(path)+"\n"+sortWord(treeMap)); + +// + } + + public static String sortWord(Map treeMap) throws Exception{ + List> list = new ArrayList>(treeMap.entrySet()); + Collections.sort(list, ((o1, o2) -> o2.getValue().compareTo(o1.getValue()))); + int n = 10; + String s[] = new String[n]; + + int i =0; + for (Map.Entry entry : list) { + if (n-- == 0) + break; + String key = entry.getKey(); + Integer value = entry.getValue(); + s[i] = "<" + key + ">:" + value; +// System.out.print("<" + key + ">:" + value); +// System.out.println(s[i]); + i++; + } + String string = s[0]+"\n"; + for (int j = 1; j 31 && getchar < 127 || getchar == 10) { + charNum++; + } + } + return "characters: " + charNum; + } + + public static void writefile(String path, String content) throws Exception { + try { + OutputStream out = new FileOutputStream(path); + out.write(content.getBytes()); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + public static String lineCount(String path)throws Exception{ + int lineNum = 0; + InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 + BufferedReader br = new BufferedReader(isr); + String str = br.readLine(); + while (str != null) {//统计有效行的字符 + lineNum++; + str = br.readLine(); + } + + return "lines:" + lineNum; + } + public static String wordCount(String path)throws Exception{ + int wordsNum = 0;//单词总数 + InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 + BufferedReader br = new BufferedReader(isr); + String str = br.readLine(); + while (str != null) {//统计有效行的字符 + String wordsArr[] = str.split("\\s*[^0-9a-zA-Z]+");//根据分隔符为数字字母以外的存放在数组 + for (String word : wordsArr) { + //以4个英文字母开头,跟上字母数字符号 + if (word.matches("[a-zA-Z]{4,}[a-zA-Z0-9]*")) { + lists.add(word); + } + } + str = br.readLine(); + } + wordsNum = lists.size(); + + return "words:" + wordsNum; + } + + +}