第十一周进度总结

所花时间:1天+1下午+1晚上

代码行:500(其中有一些代码是由冗余,可以简化)

博客量:2篇

了解到知识点 :

文本读出,并且规划为单词,很重要!

遇到的难题是短语的分析,近期的想法是使用无用词,以及除了空格分隔单词的标点符号,来进行标记,同时看出文本单词总数,也是十分的重要。

  1 package txt读入;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileReader;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.util.Scanner;
 10 
 11 
 12 public class Sum {
 13 public static void main(String[] args) {
 14     System.out.println("请输入您想完成的功能:");
 15     int i=0;
 16 for(int n=1;n<10;n++)
 17     {System.out.println("1:统计字母;2:统计单词;3:指定单词;4:短语");
 18     
 19     Scanner scan=new Scanner(System.in);
 20     i=scan.nextInt();
 21     if(i==1)
 22     {
 23         Zimu();
 24     }
 25     if(i==2)
 26     {
 27         try {
 28             Quan();
 29         } catch (IOException e) {
 30             // TODO Auto-generated catch block
 31             e.printStackTrace();
 32         }
 33     }
 34     if(i==3)
 35     {
 36         try {
 37             Danci();
 38         } catch (IOException e) {
 39             // TODO Auto-generated catch block
 40             e.printStackTrace();
 41         }
 42     }
 43     if(i==4)
 44     {
 45         try {
 46             Daunyu();
 47         } catch (IOException e) {
 48             // TODO Auto-generated catch block
 49             e.printStackTrace();
 50         }
 51     }
 52 System.out.println("是否退出 退出0,不退出-其他");
 53 n=scan.nextInt();
 54 if(n==0)
 55 {
 56     break;
 57 }
 58     }
 59 }
 60 static void Zimu()
 61 {
 62      try {
 63          char shu[] = new char[1000];
 64          char zimu[] = new char[52];
 65          int j=0;
 66          int count[]=new int[52];
 67          String pathname="E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\1.txt";
 68          File filename=new File(pathname);
 69          InputStreamReader reader=new InputStreamReader(new FileInputStream(filename));
 70          BufferedReader br=new BufferedReader(reader);
 71          String line[]=new String[100];;    
 72          for(int i=0;i<line.length;i++){
 73          line[i]=br.readLine();    
 74          }
 75          br.close();
 76          int k=0;
 77           while(line[k]!=null) {
 78               for(int i=0;i<line[k].length();i++) {                                
 79                    shu[j]=line[k].charAt(i);
 80                    j++;                     
 81               }
 82               k++;
 83          }    
 84          for(int i=0;i<shu.length;i++) {
 85               switch(shu[i]) {
 86               case 'a':zimu[0]='a';count[0]++;break;
 87               case 'b':zimu[1]='b';count[1]++;break;
 88               case 'c':zimu[2]='c';count[2]++;break;
 89               case 'd':zimu[3]='d';count[3]++;break;
 90               case 'e':zimu[4]='e';count[4]++;break;
 91               case 'f':zimu[5]='f';count[5]++;break;
 92               case 'g':zimu[6]='g';count[6]++;break;
 93               case 'h':zimu[7]='h';count[7]++;break;
 94               case 'i':zimu[8]='i';count[8]++;break;
 95               case 'j':zimu[9]='j';count[9]++;break;
 96               case 'k':zimu[10]='k';count[10]++;break;
 97               case 'l':zimu[11]='l';count[11]++;break;
 98               case 'm':zimu[12]='m';count[12]++;break;
 99               case 'n':zimu[13]='n';count[13]++;break;
100               case 'o':zimu[14]='o';count[14]++;break;
101               case 'p':zimu[15]='p';count[15]++;break;
102               case 'q':zimu[16]='q';count[16]++;break;
103               case 'r':zimu[17]='r';count[17]++;break;
104               case 's':zimu[18]='s';count[18]++;break;
105               case 't':zimu[19]='t';count[19]++;break;
106               case 'u':zimu[20]='u';count[20]++;break;
107               case 'v':zimu[21]='v';count[21]++;break;
108               case 'w':zimu[22]='w';count[22]++;break;
109               case 'x':zimu[23]='x';count[23]++;break;
110               case 'y':zimu[24]='y';count[24]++;break;
111               case 'z':zimu[25]='z';count[25]++;break;
112               case 'A':zimu[26]='A';count[26]++;break;
113               case 'B':zimu[27]='B';count[27]++;break;
114               case 'C':zimu[28]='C';count[28]++;break;
115               case 'D':zimu[29]='D';count[29]++;break;
116               case 'E':zimu[30]='E';count[30]++;break;
117               case 'F':zimu[31]='F';count[31]++;break;
118               case 'G':zimu[32]='G';count[32]++;break;
119               case 'H':zimu[33]='H';count[33]++;break;
120               case 'I':zimu[34]='I';count[34]++;break;
121               case 'J':zimu[35]='G';count[35]++;break;
122               case 'K':zimu[36]='K';count[36]++;break;
123               case 'L':zimu[37]='L';count[37]++;break;
124               case 'M':zimu[38]='M';count[38]++;break;
125               case 'N':zimu[39]='N';count[39]++;break;
126               case 'O':zimu[40]='O';count[40]++;break;
127               case 'P':zimu[41]='P';count[41]++;break;
128               case 'Q':zimu[42]='Q';count[42]++;break;
129               case 'R':zimu[43]='R';count[43]++;break;
130               case 'S':zimu[44]='S';count[44]++;break;
131               case 'T':zimu[45]='T';count[45]++;break;
132               case 'U':zimu[46]='U';count[46]++;break;
133               case 'V':zimu[47]='V';count[47]++;break;
134               case 'W':zimu[48]='W';count[48]++;break;
135               case 'X':zimu[49]='X';count[49]++;break;
136               case 'Y':zimu[50]='Y';count[50]++;break;
137               case 'Z':zimu[51]='Z';count[51]++;
138               }
139          }    
140          int ci=0;
141          int sum=0;
142          System.out.println("短文中各字母出现情况统计如下:");
143          for(int i=0;i<52;i++)
144          {
145              if(count[i]!=0) {
146                  ci++;
147                  sum+=count[i];
148                  System.out.println(ci+".字母"+zimu[i]+"的出现次数是:"+count[i]);
149                  }
150          }   
151          System.out.println("字母共计:"+sum+"个");
152      }catch (Exception e) {
153          e.printStackTrace();
154      }
155 }
156 static void Quan()throws IOException
157 {
158       Word word=new Word();                                      //单词的链头
159         Word lian,xin;                                             
160         String str="";
161         FileReader f=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\1.txt");                //读取英文文件
162         char[] c=new char[1];                                 //每次读取一个字母
163         int b=0;
164         boolean exist=false;                              //判断单词是否存在于  word 链中
165         while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后
166         {
167             //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
168             if(String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
169             {
170                 lian=word;
171                 while(lian!=null)            
172                 {
173                     if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
174                     {
175                         lian.geshu++;exist=true;break;
176                     }
177                     else
178                     {
179                         lian=lian.next;
180                     }
181                 }
182                 if(exist==false)                        //如果不存在,则在单词链中添加
183                 {
184                     xin=new Word(str,1);
185                     xin.next=word.next;
186                     word.next=xin;
187                     str="";
188                 }
189                 else
190                 {
191                     exist=false;
192                     str="";
193                 }
194             }
195             else                                      //单词
196             {
197                 str+=String.valueOf(c);
198             }
199         }
200         int N=20;
201         for(int i=1;i<=N;i++)                   
202         {
203             xin=new Word("",0);
204             lian=word.next;
205             //找到单词链中个数最多的
206             while(lian!=null)
207             {
208                 if(lian.geshu>xin.geshu)
209                 {
210                     xin=lian;
211                 }
212                 lian=lian.next;
213             }
214             //输出单词链中个数最多的
215             System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
216             lian=word;
217             //删除单词链中单词个数最多的
218             while(lian.next!=null)
219             {
220                 if(lian.next.value.equalsIgnoreCase(xin.value))
221                 {
222                     lian.next=lian.next.next;
223                     break;
224                 }
225                 lian=lian.next;
226             }
227         }
228     }
229 static void Daunyu()throws IOException
230 {
231      Word word=new Word();                                      //单词的链头
232         Word lian,xin;                                             
233         String str="";
234         FileReader f=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\飘英文版.txt");                //读取英文文件 
235         char[] c=new char[1];                                 //每次读取一个字母
236         int b=0;
237         boolean exist=false;                              //判断单词是否存在于  word 链中
238         while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后
239         {
240             //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
241             if(String.valueOf(c).equals(" ")||String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
242             {
243                 
244                 lian=word;
245                 while(lian!=null)            
246                 {
247 
248                     if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
249                     {
250                         lian.geshu++;exist=true;break;
251                     }
252                     else
253                     {
254                         if(str.equals("a")||str.equals("the")||str.equals("and"))
255                         {
256                             break;
257                         }
258                         lian=lian.next;
259                     }
260                     
261                 }
262 
263                 if(exist==false)                        //如果不存在,则在单词链中添加
264                 {
265                     xin=new Word(str,1);
266                     xin.next=word.next;
267                     word.next=xin;
268                     str="";
269                 }
270                 else
271                 {
272                     exist=false;
273                     str="";
274                 }
275             }
276             else                                      //单词
277             {
278                 str+=String.valueOf(c);
279             }
280         }
281         
282         
283         //   循环10次
284         System.out.println("请输入您想查询的前几个出现此处最多的单词");
285         Scanner scan=new Scanner(System.in);
286         int N=scan.nextInt();
287         for(int i=1;i<=N;i++)                   
288         {
289             xin=new Word("",0);
290             lian=word.next;
291             //找到单词链中个数最多的
292             while(lian!=null)
293             {
294                 if(lian.geshu>xin.geshu)
295                 {
296                     xin=lian;
297                 }
298                 lian=lian.next;
299             }
300             //输出单词链中个数最多的
301             System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
302             lian=word;
303             //删除单词链中单词个数最多的
304             while(lian.next!=null)
305             {
306                 if(lian.next.value.equalsIgnoreCase(xin.value))
307                 {
308                     lian.next=lian.next.next;
309                     break;
310                 }
311                 lian=lian.next;
312             }
313         }
314     }
315 static void add()throws IOException
316 {
317     System.out.println("");
318 }
319 static void Danci()throws IOException
320 {
321     Word word=new Word();                                      //单词的链头
322     Word lian,xin;                                             
323     String str="";
324     FileReader f=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\飘英文版.txt");                //读取英文文件 
325     char[] c=new char[1];                                 //每次读取一个字母
326     int b=0;
327     boolean exist=false;                              //判断单词是否存在于  word 链中
328     Word word1=new Word();
329     Word lian1,xin1;
330     String str1="";
331     FileReader f1=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\stopwords.txt");   
332     char[] c1=new char[1];
333     int b1=0;
334     boolean exist1=false;
335     //stopwords文本
336      while((b1=f1.read(c1))!=-1)                              //每次读取一个字母直到最后
337         {
338             //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
339             if(String.valueOf(c1).equals("\r")||String.valueOf(c1).equals("\n")||String.valueOf(c1).equals(" ")||String.valueOf(c1).equals(",")||String.valueOf(c1).equals(".")||String.valueOf(c1).equals("\"")||String.valueOf(c1).equals("'"))
340             {
341                 
342                 lian1=word1;
343                 while(lian1!=null)            
344                 {
345 
346                     if(lian1.value.equalsIgnoreCase(str1))           //如果单词在单词链中存在,则单词个数++
347                     {
348                         lian1.geshu++;exist1=true;break;
349                     }
350                     else
351                     {
352                         lian1=lian1.next;
353                     }
354                     
355                 }
356 
357                 if(exist1==false)                        //如果不存在,则在单词链中添加
358                 {
359                     xin1=new Word(str1,1);
360                     xin1.next=word1.next;
361                     word1.next=xin1;
362                     str1="";
363                 }
364                 else
365                 {
366                     exist1=false;
367                     str1="";
368                 }
369             }
370             else                                      //单词
371             {
372                 str1+=String.valueOf(c1);
373             }
374         }
375      while(true)
376      {
377          xin1=new Word("",0);
378             lian1=word1.next;
379             //找到单词链中个数最多的
380             while(lian1!=null)
381             {
382                System.out.println(lian1.value);
383                 lian1=lian1.next;
384             }       
385          if(lian1==null)
386          {
387              break;
388          }
389      }
390      //飘文本
391     while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后
392     {
393         //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
394         if(String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
395         {
396             
397             lian=word;
398             while(lian!=null)            
399             {
400 
401                 if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
402                 {
403                     lian.geshu++;exist=true;break;
404                 }
405                 else
406                 {
407                     if(str.equals("a")||str.equals("the")||str.equals("and"))
408                     {
409                         break;
410                     }
411                     lian=lian.next;
412                 }
413                 
414             }
415 
416             if(exist==false)                        //如果不存在,则在单词链中添加
417             {
418                 xin=new Word(str,1);
419                 xin.next=word.next;
420                 word.next=xin;
421                 str="";
422             }
423             else
424             {
425                 exist=false;
426                 str="";
427             }
428         }
429         else                                      //单词
430         {
431             str+=String.valueOf(c);
432         }
433     }
434     
435     
436     //   循环10次
437     System.out.println("请输入您想查询的前几个出现此处最多的单词");
438     Scanner scan=new Scanner(System.in);
439     int N=scan.nextInt();
440     for(int i=1;i<=N;i++)                   
441     {
442         xin=new Word("",0);
443         lian=word.next;
444         //找到单词链中个数最多的
445         while(lian!=null)
446         {
447             if(lian.geshu>xin.geshu)
448             {
449                 xin=lian;
450             }
451             lian=lian.next;
452         }
453         //输出单词链中个数最多的
454         System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
455         lian=word;
456         //删除单词链中单词个数最多的
457         while(lian.next!=null)
458         {
459             if(lian.next.value.equalsIgnoreCase(xin.value))
460             {
461                 lian.next=lian.next.next;
462                 break;
463             }
464             lian=lian.next;
465         }
466     }
467 }
468 
469 }

猜你喜欢

转载自www.cnblogs.com/smartisn/p/10851189.html