课堂练习----单词统计续
第1步:输出单个文件中的前 N 个最常出现的英语单词。
功能1:输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列。
功能2: 指定文件目录,对目录下每一个文件执行统计的操作。
功能3:指定文件目录,是会递归遍历目录下的所有子目录的文件进行统计单词的功能。
功能4:输出出现次数最多的前 n 个单词,
例如, 提示统计统计前多少名:输入10。 就是输出最常出现单词的前 10 名。 当没有指明数量的时候,我们默认列出所有单词的频率。
第2步:第二步: 支持 stop words
在一本小说里, 频率出现最高的单词一般都是 "a", "it", "the", "and", "this", 这些词, 可以做一个 stop word 文件 (停词表), 在统计词汇的时候,跳过这些词。 我们把这个文件叫 "stopwords.txt" file.
第三步: 想看看常用的短语是什么, 怎么办呢?
先定义短语:"两个或多个英语单词, 它们之间只有空格分隔". 请看下面的例子:hello world //这是一个短语;hello, world //这不是一个短语;同一频率的词组, 按照字典序来排列。
第四步:把动词形态都统一之后再计数。
想找到常用的单词和短语,但是发现英语动词经常有时态和语态的变化,导致同一个词,同一个短语却被认为是不同的。 怎么解决这个问题呢?
假设我们有这样一个文本文件,这个文件的每一行都是这样构成:
动词原型 动词变形1 动词变形2... ,词之间用空格分开。
e.g. 动词 TAKE 有下面的各种变形:take takes took taken taking
我们希望在实现上面的各种功能的时候,有一个选项, 就是把动词的各种变形都归为它的原型来统计。
功能 支持动词形态的归一化。
Text.java
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.text.DecimalFormat; 9 import java.text.NumberFormat; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.Iterator; 13 import java.util.Scanner; 14 public class Text { 15 static int N=5; 16 17 public static String StatList(String str) { 18 StringBuffer sb = new StringBuffer(); 19 HashMap<String ,Integer> has = new HashMap<String ,Integer> (); 20 String[] slist = str.split("[^a-zA-Z\']+"); 21 for (int i = 0; i < slist.length; i++) 22 { 23 if (!has.containsKey(slist[i])) 24 { 25 has.put(slist[i], 1); 26 } 27 else 28 { 29 has.put(slist[i],has.get(slist[i])+1 ); 30 } 31 } 32 Iterator<String> iterator = has.keySet().iterator(); 33 String a[]=new String[100]; 34 int s[]=new int[100]; 35 int judge; 36 int n; 37 Scanner in=new Scanner(System.in); 38 System.out.println("输入前n个最常出现的单词:"); 39 n=in.nextInt(); 40 for(int i=0;i<n;i++) 41 { 42 iterator = has.keySet().iterator(); 43 while(iterator.hasNext()) 44 { 45 String word = (String) iterator.next(); 46 if(s[i]<has.get(word)) 47 { 48 s[i]=has.get(word); 49 a[i]=word; 50 } 51 } 52 judge=woor(a[i]); 53 if(judge==1) 54 { 55 n++; 56 has.remove(a[i]); 57 } 58 else 59 { 60 sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); 61 has.remove(a[i]); 62 } 63 } 64 return sb.toString(); 65 } 66 67 68 public static void main(String[] args) 69 { 70 display(); 71 } 72 public static int woor(String a) 73 { 74 int n=0; 75 File ctoFile = new File("stopword.txt"); 76 InputStreamReader rdCto; 77 try { 78 rdCto = new InputStreamReader(new FileInputStream(ctoFile)); 79 BufferedReader bfReader = new BufferedReader(rdCto); 80 String txtline = null; 81 try { 82 while ((txtline = bfReader.readLine()) != null) 83 { 84 if(txtline.equals(a)) 85 { 86 n=1; 87 } 88 } 89 bfReader.close(); 90 } catch (IOException e) { 91 // TODO Auto-generated catch block 92 e.printStackTrace(); 93 } 94 95 } catch (FileNotFoundException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 return n; 100 } 101 public static ArrayList<String> getFiles(String path) { 102 ArrayList<String> files = new ArrayList<String>(); 103 File file = new File(path); 104 File[] tempList = file.listFiles(); 105 for (int i = 0; i < tempList.length; i++) { 106 if (tempList[i].isFile()) { 107 files.add(tempList[i].toString()); 108 } 109 if (tempList[i].isDirectory()) { 110 } 111 } 112 return files; 113 } 114 private static ArrayList<String> getDirectory(File file) { 115 ArrayList<String> files = new ArrayList<String>(); 116 ArrayList<String> files1 = new ArrayList<String>(); 117 //获取该目录下的文件列表 118 File flist[] = file.listFiles(); 119 for (File f : flist) { 120 if (f.isDirectory()) { 121 files1=getFiles(f.getAbsolutePath()); 122 files.addAll(files1); 123 124 getDirectory(f); 125 } else { 126 files.add(f.getAbsolutePath()); 127 } 128 } 129 return files; 130 } 131 public static void zimu(String path) 132 { 133 try { 134 FileReader fr = new FileReader(path); 135 BufferedReader br = new BufferedReader(fr); 136 DecimalFormat df = new DecimalFormat("#.00"); 137 HashMap<String, Integer> map = new HashMap<String, Integer>(); 138 139 String string =null; 140 Integer count = 0; 141 Integer total = 0; 142 143 try { 144 while ((string=br.readLine())!=null) { 145 char[] ch = string.toCharArray(); 146 147 for (int i = 0; i < ch.length; i++) { 148 if (ch[i] > 'A' && ch[i]< 'z') { 149 150 total++; 151 ch[i] = Character.toLowerCase(ch[i]); 152 count = map.get(ch[i]+""); 153 if (count == null) { 154 count = 1; 155 }else { 156 count++; 157 } 158 map.put(ch[i]+"", count); 159 } 160 } 161 } 162 } catch (IOException e) { 163 // TODO Auto-generated catch block 164 e.printStackTrace(); 165 } 166 ArrayList<String> list = new ArrayList<String>(); 167 list.addAll(map.keySet()); 168 NumberFormat numberFormat = NumberFormat.getInstance(); 169 numberFormat.setMaximumFractionDigits(2); 170 for(int i = 0;i < list.size();i++) 171 { 172 for(int j = 0;j < (list.size() - i-1);j++) 173 { 174 if(map.get(list.get(j)) < map.get(list.get(j+1))) 175 { 176 String t = list.get(j); 177 list.set(j, list.get(j+1)); 178 list.set( j+1, t); 179 } 180 } 181 } 182 for(int i = 0 ; i < list.size();i++) 183 { 184 185 System.out.println(list.get(i) + ":" + map.get(list.get(i)) +" "+ df.format(((float)map.get(list.get(i)))*100/total) + "%"); 186 } 187 188 189 } catch (FileNotFoundException e) { 190 // TODO Auto-generated catch block 191 e.printStackTrace(); 192 } 193 } 194 public static String StatList1(String str) { 195 StringBuffer sb = new StringBuffer(); 196 HashMap<String ,Integer> has = new HashMap<String ,Integer> (); 197 String[] slist = str.split("[^a-zA-Z\']+"); 198 for (int i = 0; i < slist.length; i++) 199 { 200 if (!has.containsKey(slist[i])) 201 { 202 has.put(slist[i], 1); 203 } 204 else 205 { 206 has.put(slist[i],has.get(slist[i])+1 ); 207 } 208 } 209 Iterator<String> iterator = has.keySet().iterator(); 210 String a[]=new String[100]; 211 int s[]=new int[100]; 212 int judge; 213 int n=20; 214 for(int i=0;i<n;i++) 215 { 216 iterator = has.keySet().iterator(); 217 while(iterator.hasNext()) 218 { 219 String word = (String) iterator.next(); 220 if(s[i]<has.get(word)) 221 { 222 s[i]=has.get(word); 223 a[i]=word; 224 } 225 } 226 judge=woor(a[i]); 227 if(judge==1) 228 { 229 n++; 230 has.remove(a[i]); 231 } 232 else 233 { 234 sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); 235 has.remove(a[i]); 236 } 237 } 238 return sb.toString(); 239 } 240 public static String StatList2(String str) { 241 StringBuffer sb = new StringBuffer(); 242 HashMap<String ,Integer> has = new HashMap<String ,Integer> (); 243 String[] slist = str.split("[^a-zA-Z\']+"); 244 for (int i = 0; i < slist.length; i++) 245 { 246 if (!has.containsKey(slist[i])) 247 { 248 has.put(slist[i], 1); 249 } 250 else 251 { 252 has.put(slist[i],has.get(slist[i])+1 ); 253 } 254 } 255 Iterator<String> iterator = has.keySet().iterator(); 256 String a[]=new String[2000]; 257 int s[]=new int[2000]; 258 int judge; 259 int n=1000; 260 String duanyu=""; 261 for(int i=0;i<n;i++) 262 { 263 264 iterator = has.keySet().iterator(); 265 while(iterator.hasNext()) 266 { 267 String word = (String) iterator.next(); 268 if(s[i]<has.get(word)) 269 { 270 s[i]=has.get(word); 271 a[i]=word; 272 } 273 } 274 275 judge=woor(a[i]); 276 if(judge==1) 277 { 278 n++; 279 has.remove(a[i]); 280 System.out.println(duanyu); 281 duanyu=""; 282 } 283 else 284 { 285 duanyu=duanyu+" "+a[i]; 286 has.remove(a[i]); 287 } 288 } 289 return sb.toString(); 290 } 291 public static void display() 292 { 293 File file = new File("a"); 294 int n=0; 295 Scanner in=new Scanner(System.in); 296 while(n!=5) 297 { 298 System.out.println("请选择命令"); 299 System.out.println("5退出"); 300 System.out.println("1字母"); 301 System.out.println("2单词"); 302 System.out.println("3前n个单词"); 303 System.out.println("4短语"); 304 n=in.nextInt(); 305 if(n==1) 306 { 307 zimu("aaa.txt"); 308 309 } 310 if(n==2) 311 { 312 String sz=writeFromFile.readTxtFile("a.txt"); 313 String ltxt=null; 314 System.out.println(ltxt=StatList1(sz)); 315 try { 316 writeFromFile.daochu(ltxt); 317 } catch (IOException e) { 318 // TODO Auto-generated catch block 319 e.printStackTrace(); 320 } 321 } 322 if(n==3) 323 { 324 String sz=writeFromFile.readTxtFile("aaa.txt"); 325 String ltxt=null; 326 System.out.println(ltxt=StatList(sz)); 327 try { 328 writeFromFile.daochu(ltxt); 329 } catch (IOException e) { 330 // TODO Auto-generated catch block 331 e.printStackTrace(); 332 } 333 } 334 if(n==4) 335 { 336 String sz=writeFromFile.readTxtFile("aaa.txt"); 337 String ltxt=null; 338 System.out.println(ltxt=StatList2(sz)); 339 try { 340 writeFromFile.daochu(ltxt); 341 } catch (IOException e) { 342 // TODO Auto-generated catch block 343 e.printStackTrace(); 344 } 345 } 346 } 347 } 348 349 350 351 }
readFromFile.java
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 public class readFromFile { 9 public static String readTxtFile(String filePath){ 10 try { 11 String encoding="GBK"; 12 File file=new File(filePath); 13 if(file.isFile() && file.exists()){ 14 InputStreamReader read = new InputStreamReader( 15 new FileInputStream(file),encoding); 16 BufferedReader bufferedReader = new BufferedReader(read); 17 String lineTxt = null; 18 String lineText=""; 19 while((lineTxt = bufferedReader.readLine()) != null) 20 { 21 lineText+=(lineTxt); 22 } 23 read.close(); 24 return lineText; 25 } 26 else 27 { 28 System.out.println("找不到指定的文件"); 29 } 30 } catch (Exception e) { 31 System.out.println("读取文件内容出错"); 32 e.printStackTrace(); 33 } 34 return null; 35 } 36 public static void daochu(String a) throws IOException 37 { 38 File file=new File("b.txt"); 39 FileOutputStream fos=new FileOutputStream(file); 40 OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); 41 osw.append(a); 42 osw.close(); 43 fos.close(); 44 } 45 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~