zoukankan      html  css  js  c++  java
  • 合计文件中英文单词出现次数

    判断一个小文件中单词存在次数:

    package test;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    public class WordCount1 
    {
       public static void main(String[] args) throws IOException
       {
           //先创建一个流对文件进行读取
           //创建一个容器,存放读取的单词 可变的map中,key:单词,value:次数
           BufferedReader br=new  BufferedReader(new FileReader("D:"+File.separator+"piao.txt"));//map存放引用数据类型
           Map<String,Integer> map=new HashMap<String,Integer>();
           String line=null;
           //读取文件,将向map中添加单词
           while((line=br.readLine())!=null)
           {
               //将读取的一行内容拆分成单词
              String[] spit=line.split("
    ");  //split(String regex) 根据给定的正则表达式的匹配来拆分此字符串。
              //将数组中每个单词取出放在map中
              for(String word:spit)
              {
                  //判断单词是否存在,如果不存在,word为key,1为value存入map中
                  if(!map.containsKey(word))    //如果该映射将一个或多个键映射到指定值,则返回 true。
                  {
                       map.put(word, 1);
                  }
                  else {
                      //如果单词存在,将value值取出+1,再放入map中
                      int value=map.get(word)+1;
                      map.put(word,value);
                  }
              }
           }
           System.out.println(map);
       }
    }

    统计多个文件中单词次数:

    package test;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    public class WordCount2 
    {
       //统计多个文件中单词出现次数:分而治之
        //1,每次统计一个文件单词出现次数
        //2,将1中得到的结果合并
        public static void main(String[] args) throws IOException 
        {
            //分布读取一个文件
            Map<String,Integer> map= requestone("D:"+File.separator+"停用词.txt");
            Map<String,Integer> map1= requestone("D:"+File.separator+"停用词1.txt");
            Map<String,Integer> map2= requestone("D:"+File.separator+"停用词2.txt");
            Map<String,Integer> map3= requestone("D:"+File.separator+"停用词3.txt");
            System.out.print(map+"
    ");
            System.out.print(map1+"
    ");
            System.out.print(map2+"
    ");
            System.out.print(map3+"
    ");
            
            Map<String,Integer> resultmap=mergerResult(map,map1,map2,map3);
            System.out.println(resultmap);
        }
        private static Map<String, Integer> mergerResult(Map<String, Integer> ... maps) 
        {
        // 结果合并,遍历每一个map集合,将结果放在新的map中
            Map<String,Integer> resultmap=new HashMap<String, Integer>();
            for(Map<String,Integer> m:maps)
            {
                //循环遍历每一个map,将结果放在新的map中
                for(Entry<String, Integer>kv:m.entrySet())
                    //取出Entry中的每个key判断,如果不存在,存在新的map中
                {
                    String key=kv.getKey();
                    int value=kv.getValue();
                    if(!resultmap.containsKey(key))
                    {
                        resultmap.put(key, value);
                    }
                    else
                    {
                       Integer values=resultmap.get(key)+value;
                       resultmap.put(key, values);
                    }
                }
            }
        return resultmap;
        }
        public static Map<String,Integer> requestone(String path) throws IOException
        {
             //先创建一个流对文件进行读取
            //创建一个容器,存放读取的单词 可变的map中,key:单词,value:次数
            BufferedReader br=new  BufferedReader(new FileReader(path));//map存放引用数据类型
            Map<String,Integer> map=new HashMap<String,Integer>();
            String line=null;
            //读取文件,将向map中添加单词
            while((line=br.readLine())!=null)
            {
                //将读取的一行内容拆分成单词
               String[] spit=line.split("
    ");  //split(String regex) 根据给定的正则表达式的匹配来拆分此字符串。
               //将数组中每个单词取出放在map中
               for(String word:spit)
               {
                   //判断单词是否存在,如果不存在,word为key,1为value存入map中
                   if(!map.containsKey(word))    //如果该映射将一个或多个键映射到指定值,则返回 true。
                   {
                        map.put(word, 1);
                   }
                   else {
                       //如果单词存在,将value值取出+1,再放入map中
                       int value=map.get(word)+1;
                       map.put(word,value);
                   }
               }
            }
            return map;
        }
    }
  • 相关阅读:
    SseEmitter broken pipe
    沉淀vue相关知识(主要还是个人积累用)
    简述箭头函数基本使用和this指向的问题
    前端渲染和后端渲染,前端路由和后端路由
    .net工程师学习vue的心路历程(三)
    .net工程师学习vue的心路历程(一)
    初始mongodb(一)
    .net工程师学习vue的心路历程(二)
    MySQL自我保护参数
    mongodb副本集添加节点
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/11723198.html
Copyright © 2011-2022 走看看