zoukankan      html  css  js  c++  java
  • 查找

    [实验任务三]统计分析

    (1)  用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《哈利波特》 中最常用的短语是什么,等等。

    (2)  要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

    package FrequencyOfOccurrence;
    
    import java.io.*;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String s=GetFiles("d:\Were You Ever a Child.txt","",1); 
            character.WriteFiles(s);
            word.WriteFiles1(s);
            //读入文本
        } 
        @SuppressWarnings("finally")
        public static String GetFiles(String BasePath,String Content, int f) { 
            /* 
             * 读写文件操作 
             */ 
            String s, s2 = new String(); 
            try { 
                if (f == 1) { 
                    BufferedReader in = new BufferedReader(new FileReader(BasePath)); 
                    while ((s = in.readLine()) != null) { 
                        s2 += s + "
    "; 
                    } 
                    in.close(); 
                } 
                if(f==2) 
                { 
                    PrintWriter out1=new PrintWriter(new BufferedWriter(new FileWriter(BasePath))); 
                    out1.println(Content); 
                    out1.close(); 
                } 
            } catch (FileNotFoundException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } finally { 
                return s2; 
            }
        } 
    }
    package FrequencyOfOccurrence;
    
    import java.util.*;
    
    public class character {
        public static void WriteFiles(String s) 
        { 
            String chara=""; 
            for(int i=0;i<s.length();i++) 
            { 
                if( ((s.charAt(i))>='A' && (s.charAt(i))<='Z') || ((s.charAt(i))>='a' && (s.charAt(i))<='z') ) 
                { 
                    chara+=s.charAt(i); 
                    continue; }
            }
                     if (chara == null || chara == ""){
                         System.out.println("所选文本中并未写入内容");
                         }
                        Character maxChar = null;
                        int maxCount = 0;
                        Map<Character,Integer> map = new HashMap<Character, Integer>();//将出现的字母和出现的次数放进一个map中
                        for(int j=0; j < chara.length();j++){
                            if (map.containsKey(chara.charAt(j)))
                              map.put(chara.charAt(j),map.get(chara.charAt(j))+1);//如果map的key包含该字符,则+1
                            else
                                map.put(chara.charAt(j),1);//如不map的key不包含该字符,则初始化为1
                            if (maxCount < map.get(chara.charAt(j))){
                                maxCount = map.get(chara.charAt(j));
                                maxChar =chara.charAt(j);
                            }
                        }               System.out.println("字母"+ maxChar+"最多,次数为"+maxCount);
                }  
        }
    package FrequencyOfOccurrence;
    
    import java.util.*;
    
    public class word {
        public static void WriteFiles1(String s) 
        { 
            String word=""; 
            for(int i=0;i<s.length();i++) 
            { 
                 word+=s.charAt(i); 
            }
                 word= word.toLowerCase();//将字符串中的英文部分的字符全部变为小写
                String regex="[\W]+";//非字母的正则表达式 --W:表示任意一个非单词字符
                /*
                 * 将非字母字符全部替换为空格字符" "
                 * 到这里已经得到一个全小写的纯字母字符串包含有空格字符
         */
                 word= word.replaceAll(regex, " ");
        String[]  words= word.split(" "); //以空格作为分隔符获得字符串数组
        HashMap<String, Integer> strhash = new HashMap<String, Integer>();
        Integer in=null;//用于存放put操作的返回值
        for(String  words1: words){//遍历数组 word
            in=strhash.put(words1, 1);
            if(in!=null){//判断如果返回的不是null,则+1再放进去就是出现的次数
                strhash.put(words1, in+1);
            }
        }
        Set<java.util.Map.Entry<String,Integer>> entrySet=strhash.entrySet();
        String maxStr=null;//用于存放出现最多的单词
        int maxValue=0;//用于存放出现最多的次数                
        for(java.util.Map.Entry<String,Integer> e:entrySet){
            String key=e.getKey();
            Integer value=e.getValue();
            if(value>maxValue){
                maxValue=value;//这里有自动拆装箱
                maxStr=key;
            }
        }
        System.out.println("出现最多的单词是:"+maxStr+"出现了"+maxValue+"次");
    }
        }
    自我抑郁又自我救赎
  • 相关阅读:
    JAVA中线程同步的方法(4种)汇总
    java
    指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。(解决问题总结)
    无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1
    foreach---集合已修改;可能无法执行枚举操作。
    WPF_View中控件使用单例ViewModel
    判断s2是否能够被通过s1做循环移位(rotate)得到的字符串是否包含
    多列转1列 SqlServer 实现oracle10g的 wmsys.wm_concat()--for xml path('')
    异步对象(XMLHttpRequest)的帮助脚本
    在vs2010使用EF出现CS0012: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义
  • 原文地址:https://www.cnblogs.com/zjm15511858030/p/9788784.html
Copyright © 2011-2022 走看看