英文单词课堂小测

 

  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_2 {
 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         // TODO Auto-generated method stub
 72 //        ArrayList<String> fil=getFiles("a");//目录下的文件
 73 //        ArrayList<String> fil=getDirectory(file);//目录下的文件和子目录里的文件
 74 //        for(int i=0;i<fil.size();i++)
 75 //        {
 76 //        String filePath = fil.get(i);
 77 //        String sz=writeFromFile.readTxtFile(filePath);
 78 //        zimu(filePath);
 79 //        String ltxt=null;
 80 //        System.out.println(ltxt=StatList(sz)); 
 81 //        try {
 82 //        writeFromFile.daochu(ltxt);
 83 //        } catch (IOException e) {
 84 //        // TODO Auto-generated catch block
 85 //        e.printStackTrace();
 86 //        }
 87 //        }
 88     }
 89     public static int woor(String a)
 90     {
 91         int n=0;
 92         File ctoFile = new File("stopword.txt");
 93         InputStreamReader rdCto;
 94         try {
 95             rdCto = new InputStreamReader(new FileInputStream(ctoFile));
 96             BufferedReader bfReader = new BufferedReader(rdCto);
 97             String txtline = null;
 98             try {
 99                 while ((txtline = bfReader.readLine()) != null) 
100                 {
101                     if(txtline.equals(a))
102                         {
103                             n=1;
104                         }
105                 }
106                 bfReader.close();
107             } catch (IOException e) {
108                 // TODO Auto-generated catch block
109                 e.printStackTrace();
110             }
111              
112         } catch (FileNotFoundException e) {
113             // TODO Auto-generated catch block
114             e.printStackTrace();
115         }
116         
117         
118         
119         
120 //        int n=0;
121 //        String[] biao= {"a","the","an","it","and","this","I","t","s"};
122 //        for(int i=0;i<biao.length;i++)
123 //        {
124 //            if(biao[i].equals(a))
125 //            {
126 //                n=1;
127 //            }
128 //        }
129         return n;
130     }
131     public static ArrayList<String> getFiles(String path) {
132         ArrayList<String> files = new ArrayList<String>();
133         File file = new File(path);
134         File[] tempList = file.listFiles();
135         for (int i = 0; i < tempList.length; i++) {
136             if (tempList[i].isFile()) {
137                 files.add(tempList[i].toString());
138             }
139             if (tempList[i].isDirectory()) {
140             }
141         }
142         return files;
143     }
144     private static ArrayList<String> getDirectory(File file) {
145         ArrayList<String> files = new ArrayList<String>();
146         ArrayList<String> files1 = new ArrayList<String>();
147         //获取该目录下的文件列表
148         File flist[] = file.listFiles();
149         for (File f : flist) {
150             if (f.isDirectory()) {
151                 // 如果f是一个目录
152                 files1=getFiles(f.getAbsolutePath());
153                 files.addAll(files1);
154                
155                 getDirectory(f);
156             } else {
157                 //如果f是一个文件
158                 files.add(f.getAbsolutePath());
159             }
160         }
161         return files;
162     }
163     public static void zimu(String path)
164     {
165         try {
166             //IO操作读取文件内容
167             FileReader fr = new FileReader(path);
168             BufferedReader br = new BufferedReader(fr);
169             DecimalFormat df = new DecimalFormat("#.00");
170             HashMap<String, Integer> map = new HashMap<String, Integer>();
171             
172             String string =null;
173             Integer count = 0;//每个字母的次数
174             Integer total = 0;//总共多少个字母
175             
176             try {
177                 while ((string=br.readLine())!=null) {
178                     char[] ch = string.toCharArray();
179                     
180                     for (int i = 0; i < ch.length; i++) {
181                         if (ch[i] > 'A' && ch[i]< 'z') {
182                             
183                         total++;
184                         ch[i] = Character.toLowerCase(ch[i]);
185                         count = map.get(ch[i]+"");
186                         if (count == null) {
187                             count = 1;
188                         }else {
189                             count++;
190                         }
191                         map.put(ch[i]+"", count);
192                     }
193                     }
194                 }
195             } catch (IOException e) {
196                 // TODO Auto-generated catch block
197                 e.printStackTrace();
198             }
199              ArrayList<String> list = new ArrayList<String>();
200              list.addAll(map.keySet());  //将单词添加到list中
201              NumberFormat numberFormat = NumberFormat.getInstance();
202              
203             // 设置精确到小数点后2位
204      
205             numberFormat.setMaximumFractionDigits(2);
206              
207              //排序按照出现次数降序排列
208              for(int i = 0;i < list.size();i++)
209              {
210                  
211                  for(int j = 0;j < (list.size() - i-1);j++)     //list.size() - i-1因为要用到i+1要考虑是否超出范围的问题
212                  {
213                      
214                      if(map.get(list.get(j)) < map.get(list.get(j+1)))
215                      {
216                          String t = list.get(j);
217                          list.set(j, list.get(j+1));
218                          list.set( j+1, t);
219                      }
220                  }
221              }
222              for(int i = 0 ; i < list.size();i++)
223                 {
224                  
225                     System.out.println(list.get(i) + ":" + map.get(list.get(i)) +"   "+ df.format(((float)map.get(list.get(i)))*100/total) + "%");
226                 }
227             
228             
229         } catch (FileNotFoundException e) {
230             // TODO Auto-generated catch block
231             e.printStackTrace();
232         }
233     }
234     public static String StatList1(String str) {
235         StringBuffer sb = new StringBuffer();
236         HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
237         String[] slist = str.split("[^a-zA-Z\']+");
238         for (int i = 0; i < slist.length; i++) 
239         {
240             if (!has.containsKey(slist[i])) 
241             { 
242                    has.put(slist[i], 1);
243             } 
244             else 
245             {
246                    has.put(slist[i],has.get(slist[i])+1 );
247             }
248         }
249         Iterator<String> iterator = has.keySet().iterator();
250         String a[]=new String[100];
251         int s[]=new int[100];
252         int judge;
253         int n=20;
254         for(int i=0;i<n;i++)
255         {
256             iterator = has.keySet().iterator();
257             while(iterator.hasNext())
258             {
259                 String word = (String) iterator.next();
260                 if(s[i]<has.get(word))
261                 {
262                     s[i]=has.get(word);
263                     a[i]=word;
264                 }
265              }
266             judge=woor(a[i]);
267             if(judge==1)
268             {
269                 n++;
270                 has.remove(a[i]);
271             }
272             else
273             {
274              sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n");
275              has.remove(a[i]);
276             }
277         }
278              return sb.toString();
279         }
280     public static String StatList2(String str) {
281         StringBuffer sb = new StringBuffer();
282         HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
283         String[] slist = str.split("[^a-zA-Z\']+");
284         for (int i = 0; i < slist.length; i++) 
285         {
286             if (!has.containsKey(slist[i])) 
287             { 
288                    has.put(slist[i], 1);
289             } 
290             else 
291             {
292                    has.put(slist[i],has.get(slist[i])+1 );
293             }
294         }
295         Iterator<String> iterator = has.keySet().iterator();
296         String a[]=new String[2000];
297         int s[]=new int[2000];
298         int judge;
299         int n=1000;
300         String duanyu="";
301         for(int i=0;i<n;i++)
302         {
303             
304             iterator = has.keySet().iterator();
305             while(iterator.hasNext())
306             {
307                 String word = (String) iterator.next();
308                 if(s[i]<has.get(word))
309                 {
310                     s[i]=has.get(word);
311                     a[i]=word;
312                 }
313              }
314             
315             judge=woor(a[i]);
316             if(judge==1)
317             {
318                 n++;
319                 has.remove(a[i]);
320                 System.out.println(duanyu);
321                 duanyu="";
322             }
323             else
324             {
325                 duanyu=duanyu+" "+a[i];
326                 has.remove(a[i]);
327             }
328         }
329              return sb.toString();
330         }
331     public static void display()
332     {
333         File file = new File("a");
334         int n=0;
335         Scanner in=new Scanner(System.in);
336         while(n!=5)
337         {
338         System.out.println("请选择命令");
339         System.out.println("5退出");
340         System.out.println("1字母");
341         System.out.println("2单词");
342         System.out.println("3前n个单词");
343         System.out.println("4短语");
344         n=in.nextInt();
345         if(n==1)
346         {
347             zimu("a.txt");
348             
349         }
350         if(n==2)
351         {
352             String sz=writeFromFile.readTxtFile("a.txt");
353             String ltxt=null;
354             System.out.println(ltxt=StatList1(sz)); 
355             try {
356             writeFromFile.daochu(ltxt);
357             } catch (IOException e) {
358             // TODO Auto-generated catch block
359             e.printStackTrace();
360             }
361         }
362         if(n==3)
363         {
364             String sz=writeFromFile.readTxtFile("a.txt");
365             String ltxt=null;
366             System.out.println(ltxt=StatList(sz)); 
367             try {
368             writeFromFile.daochu(ltxt);
369             } catch (IOException e) {
370             // TODO Auto-generated catch block
371             e.printStackTrace();
372             }
373         }
374         if(n==4)
375         {
376             String sz=writeFromFile.readTxtFile("a.txt");
377             String ltxt=null;
378             System.out.println(ltxt=StatList2(sz)); 
379             try {
380             writeFromFile.daochu(ltxt);
381             } catch (IOException e) {
382             // TODO Auto-generated catch block
383             e.printStackTrace();
384             }
385         }
386         }
387     }
388     
389 
390 
391 }
text1
 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 //import java.io.Reader; 
 9 //从文本文档中读入
10 public class writeFromFile {
11     public static String readTxtFile(String filePath){ 
12     try { 
13         String encoding="GBK"; 
14         File file=new File(filePath); 
15         if(file.isFile() && file.exists()){ //判断文件是否存在 
16         InputStreamReader read = new InputStreamReader( 
17         new FileInputStream(file),encoding);//考虑到编码格式 
18         BufferedReader bufferedReader = new BufferedReader(read); 
19         String lineTxt = null;
20         String lineText="";
21         while((lineTxt = bufferedReader.readLine()) != null)
22             { 
23             lineText+=(lineTxt); 
24             } 
25         read.close(); 
26         return lineText;
27         }
28         else
29             { 
30             System.out.println("找不到指定的文件"); 
31             } 
32         } catch (Exception e) { 
33         System.out.println("读取文件内容出错"); 
34         e.printStackTrace(); 
35         } 
36         return null;
37     } 
38     public static void daochu(String a) throws IOException
39     {
40         File file=new File("b.txt");
41         FileOutputStream fos=new FileOutputStream(file);
42         OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
43         osw.append(a);
44         osw.close();
45         fos.close();
46     }
47 }
text2

 

 


 


 

 

 

posted @ 2019-05-04 00:33  夜神风  阅读(277)  评论(0)    收藏  举报