统计一篇英语作文中各个单词出现的次数,并把出现最多的单词找出。
1 package wordcont;
2
3
4 import java.io.BufferedReader;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.StringTokenizer;
13 import java.util.TreeMap;
14 import java.util.TreeSet;
15 import wordcont.WordEntity;
16
17 public class WordCont {
18
19 public void displayWordCount(String fileName){
20 //字符统计
21 try {
22 BufferedReader reader = new BufferedReader(new FileReader(fileName));
23 String line = null;
24 TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
25
26 while((line=reader.readLine())!=null){
27 line = line.toLowerCase();
28 String str[] = line.split("\\s+");
29 for(int i = 0; i<str.length; i++){
30 String word = str[i].trim();
31 if(tm.containsKey(word)){
32 tm.put(word, tm.get(word)+1);
33 }else{
34 tm.put(word, 1);
35 }
36 }
37 }
38 //输出我们想要的字符串格式
39 System.out.println("按字典序输出为:");
40 Iterator iterator=tm.entrySet().iterator();
41 while(iterator.hasNext())
42 {
43 System.out.println(iterator.next());
44 }
45
46 } catch (FileNotFoundException e) {
47 e.printStackTrace();
48 }catch (IOException e) {
49 e.printStackTrace();
50 }
51 }
52
53 public void displayFrequencyWord(String fileName){
54 //显示输出
55 try {
56 BufferedReader br = new BufferedReader(new FileReader(fileName));
57 String s;
58 StringBuffer sb = new StringBuffer();
59 while ((s = br.readLine()) != null) {
60 sb.append(s);
61 }
62
63 Map<String,Integer> map = new HashMap<String, Integer>();
64 StringTokenizer st = new StringTokenizer(sb.toString(),",.! \n");
65 while (st.hasMoreTokens()) {
66 String letter = st.nextToken().trim();
67 int count;
68 if (!map.containsKey(letter)) {
69 count = 1;
70 } else {
71 count = map.get(letter).intValue() + 1;
72 }
73 map.put(letter,count);
74 }
75
76 Set<WordEntity> set = new TreeSet<WordEntity>();
77 for (String key : map.keySet()) {
78 set.add(new WordEntity(key,map.get(key)));
79 }
80
81 System.out.println("出现频率最高的单词:");
82 Iterator<WordEntity> it1 = set.iterator();
83 int count=it1.next().getCount();
84 for (Iterator<WordEntity> it = set.iterator(); it.hasNext(); ) {
85 WordEntity w = it.next();
86
87 if (w.getCount()==count)// 当输出3个后跳出循环
88 //break;
89
90 System.out.println(w.getKey() + " 出现的次数为: "+ w.getCount());
91
92 }
93 } catch (FileNotFoundException e) {
94 System.out.println("文件未找到~!");
95 } catch (IOException e) {
96 System.out.println("文件读异常~!");
97 }
98
99 }
100
101 }
102
103
104
105
106 package wordcont;
107
108 import wordcont.WordEntity;
109
110 public class WordEntity implements Comparable<WordEntity>{
111 @Override
112 public int compareTo(WordEntity o) {
113 int cmp = count.intValue() - o.count.intValue();
114 return (cmp == 0 ? key.compareTo(o.key) : -cmp);
115 //只需在这儿加一个负号就可以决定是升序还是降序排列 -cmp降序排列,cmp升序排列
116 //因为TreeSet会调用WorkForMap的compareTo方法来决定自己的排序
117 }
118
119 private String key;
120 private Integer count;
121
122 public WordEntity ( String key,Integer count) {
123 this.key = key;
124 this.count = count;
125 }
126
127 public WordEntity(){
128
129 }
130
131 @Override
132 public String toString() {
133 return key + " 出现的次数为:" + count;
134 }
135
136 public String getKey() {
137 return key;
138 }
139
140 public Integer getCount() {
141 return count;
142 }
143 }
144
145
146
147
148 package wordcont;
149
150 import java.util.Scanner;
151
152 import wordcont.WordCont;
153
154 public class Main {
155
156 /**
157 * @param args
158 */
159 public static void main(String[] args) {
160 System.out.println("输入文件路径:\n");
161 Scanner in=new Scanner(System.in);
162 String line=in.nextLine();
163 String fileName= line.trim();
164 WordCont wc = new WordCont();
165 wc.displayWordCount(fileName);
166 wc.displayFrequencyWord(fileName);
167 }
168
169 }