1. 下面的无法运行。
  1. @Override
  2. protected void map(LongWritable key, Text value,
  3. Mapper<LongWritable, Text, Text, DoubleWritable>.Context context)
  4. throws IOException, InterruptedException {
  5. Configuration conf = context.getConfiguration();
  6. int tot = Integer.parseInt(conf.get("TOTALWORDS"));
  7.  
  8. System.out.println("total === " + total);
  9. System.out.println("tot = " + tot);
  10.  
  11. // 输入的格式如下:
  12. // ALB weekend 1
  13. // ALB weeks 3
  14. Map<String, List<String>> baseMap = new HashMap<String, List<String>>(); // 保存基础数据
  15. // Map<String, List<Double>> priorMap = new HashMap<String, List<Double>>(); // 保存每个单词出现的概率
  16.  
  17. String[] temp = value.toString().split("\t");
  18. // 先将数据存到baseMap中
  19. if (temp.length == 3) {
  20. // 文件夹名类别名temp[0]
  21. String wordAndNumber = null;
  22. wordAndNumber = temp[1] + "\t" + temp[2];
  23. if (baseMap.containsKey(temp[0])) {
  24.  
  25. baseMap.get(temp[0]).add(wordAndNumber);
  26. } else {
  27. List<String> oneList = new ArrayList<String>();
  28. oneList.add(wordAndNumber);
  29. baseMap.put(temp[0], oneList);
  30. }
  31.  
  32. } // 读取数据完毕,全部保存在baseMap中
  33.  
  34. // 两层循环计算出每个类别中每个单词的概率
  35.  
  36. Iterator<Map.Entry<String, List<String>>> iterators = baseMap.entrySet().iterator();
  37. while (iterators.hasNext()) {// 遍历类别
  38. Map.Entry<String, List<String>> iterator = iterators.next();
  39. int allWordsInClass = 0;
  40.  
  41. // list遍历
  42. Iterator<String> its = iterator.getValue().iterator();
  43.  
  44. // 得到每个类别的单词总数
  45. while (its.hasNext()) {
  46. String[] temp1 = its.next().split("\t");
  47. allWordsInClass += Integer.parseInt(temp1[1]);
  48. }
  49. System.out.println(allWordsInClass);// 这个数据没有计算成功????
  50.  
  51. //
  52. // Map<String, List<Double>> pMap = new HashMap<String, List<Double>>();
  53. // List<Double> pList = new ArrayList<Double>();
  54. // 遍历每个单词的词频计算器概率
  55. while (its.hasNext()) {
  56. String[] temp1 = its.next().split("\t");
  57. double p = (Integer.parseInt(temp1[1]) + 1) / (allWordsInClass + total);
  58. String classAndWord = iterator.getKey() + "\t" + temp1[0];
  59. className.set(classAndWord);
  60. number.set(p);
  61. LOG.info("------>p = " + p);
  62. // context.write(className, number);
  63. mos.write(iterator.getKey(), temp1[0], p);
  64. }
  65.  
  66. }
  67. }
  1.  
  1. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, DoubleWritable>.Context context)
  2. throws IOException, InterruptedException {
  3. Configuration conf = context.getConfiguration();
  4. int tot = Integer.parseInt(conf.get("TOTALWORDS"));
  5.  
  6. System.out.println("total === " + total);
  7. System.out.println("tot = " + tot);
  8.  
  9. // 输入的格式如下:
  10. // ALB weekend 1
  11. // ALB weeks 3
  12. Map<String, Map<String, Integer>> baseMap = new HashMap<String, Map<String, Integer>>(); // 保存基础数据
  13. Map<String, Map<String, Double>> priorMap = new HashMap<String, Map<String, Double>>(); // 保存每个单词出现的概率
  14.  
  15. String[] temp = value.toString().split("\t");
  16. // 先将数据存到baseMap中
  17. if (temp.length == 3) {
  18. // 文件夹名类别名
  19. if (baseMap.containsKey(temp[0])) {
  20. baseMap.get(temp[0]).put(temp[1], Integer.parseInt(temp[2]));
  21. } else {
  22. Map<String, Integer> oneMap = new HashMap<String, Integer>();
  23. oneMap.put(temp[1], Integer.parseInt(temp[2]));
  24. baseMap.put(temp[0], oneMap);
  25. }
  26.  
  27. } // 读取数据完毕,全部保存在baseMap中
  28.  
  29. // 两层循环计算出每个类别中每个单词的概率
  30. Iterator<Map.Entry<String, Map<String, Integer>>> iterators = baseMap.entrySet().iterator();
  31. while (iterators.hasNext()) {// 遍历类别
  32. Map.Entry<String, Map<String, Integer>> iterator = iterators.next();
  33. int allWordsInClass = 0;
  34.  
  35. for (Map.Entry<String, Integer> entry : iterator.getValue().entrySet()) {// 遍历类别中的单词,先求出类别中的单词总数
  36. allWordsInClass += entry.getValue();
  37. }
  38. System.out.println(allWordsInClass);//这个数据没有计算成功
  39. //
  40. Map<String, Double> pMap = new HashMap<String, Double>();
  41. for (Map.Entry<String, Integer> entry : iterator.getValue().entrySet()) {// 在遍历每个单词的个数计算单词出现的概率
  42. double p = (entry.getValue()+ 1.0) / (allWordsInClass + tot);//
  43. pMap.put(entry.getKey(), p);
  44. priorMap.put(iterator.getKey(), pMap);
  45. className.set(iterator.getKey() + "\t" + entry.getKey());
  46. number.set(p);
  47. LOG.info("------>p = " + p);
  48.  
  49. context.write(className, number);
  50. // mos.write(iterator.getKey(), entry.getKey(), p);
  51. }
  52.  
  53. }
  54.  
  55. /*
  56. * value.set(temp[1]); number.set(Integer.parseInt(temp[2]));
  57. * mos.write(value, number, dirName);
  58. */
  59. }

map map的更多相关文章

  1. Map map=new HashMap(); 为什么是这样

    Map是接口,hashMap是Map的一种实现.接口不能被实例化. Map map=new HashMap(); 就是将map实例化成一个hashMap.这样做的好处是调用者不需要知道map具体的实现 ...

  2. 笔记 freemark list标签迭代Map<Map<String,Object>集合排序问题

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. 工作中出现一个比较特殊的问题,在模板ftl文件中,一般用list迭代map 举例: 后台: // 传入的参数 ...

  3. Map map=new HashMap()

    Map是接口,hashMap是Map的一种实现.接口不能被实例化.Map map=new HashMap(); 就是将map实例化成一个hashMap.这样做的好处是调用者不需要知道map具体的实现, ...

  4. mybatia的mypper.xml文件,参数类型为map,map里有一个键值对的值为数组,如何解析,例子可供参考,接上文,发现更简便的方法,不必传数组,只需传字符串用逗号隔开即可

    是这样的 先看参数 map.put("orgId", "1818"); map.put("childDeps", "1000,10 ...

  5. java将对象转map,map转对象工具类

    /** * 将map转换为一个对象 * * @param map * @param beanClass * @return * @throws Exception */ public static O ...

  6. Collections.unmodifiableMap(Map map)

    public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)返回指定映射 ...

  7. go语言笔记——map map 默认是无序的,不管是按照 key 还是按照 value 默认都不排序

    示例 8.1 make_maps.go package main import "fmt" func main() { var mapLit map[string]int //va ...

  8. 为什么常用 Map<> map = new HashMap()

    在初学Java的时候,经常能看到教材上的写法,使用了接口Map来引用一个map,而不是它的具体实现,那么这样做的好处是什么呢? <Effective Java>第52条:通过接口引用对象 ...

  9. jsp循环map map的key值不固定

    <c:if test="${not empty parammap}"> <c:forEach items="${parammap }" var ...

随机推荐

  1. [转]Publishing and Running ASP.NET Core Applications with IIS

    本文转自:https://weblog.west-wind.com/posts/2016/Jun/06/Publishing-and-Running-ASPNET-Core-Applications- ...

  2. Centos更换yum源,安装ssh server

    先连上网,然后更换yum源 1. 新建的用户没有sudo权限,所以首先切换到root用户su -输入密码 2. 备份之前的yum源mv /etc/yum.repos.d/CentOS-Base.rep ...

  3. spark 编程向导

    http://spark.apache.org/docs/latest/programming-guide.html

  4. ASP.NET MVC Controller向View传值的几种方式

    上几篇博文提到MVC和WebForm的区别,主要是MVC的Controller和View将传统的WebForm的窗体和后台代码做了解耦,这篇博文简单介绍一下在MVC中Controller向View是如 ...

  5. Failed to load c++ bson extension, using pure JS version

    Failed to load c++ bson extension, using pure JS version npm install mongodbnpm install bson npm ins ...

  6. PHP数据库扩展mysqli的函数试题

    1.mysqli链接数据库的方式是什么? 2.mysqli获取链接错误号的属性是什么? 3.mysqli获取链接错误信息的属性是什么? 4.mysqli执行sql语句的函数是什么? 5.mysqli获 ...

  7. Array.length vs Array.prototype.length

    I found that both the Array Object and Array.prototype have the length property. I am confused on us ...

  8. Ztree当节点没有下级时不显示下拉图标

    select o.*,(select count(*) from sys_org t where t.orgsupid=o.orgid) isLeaf from sys_org o where 1=1

  9. Spring Boot 系列教程9-swagger-前后端分离后的标准

    前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...

  10. UVA106 - Fermat vs. Pythagoras

    假设x为奇数,y为偶数,则z为奇数,2z与2x的最大公因数为2,2z和2x可分别写作 2z = (z + x) + (z - x) 2x = (z + x) - (z - x) 那么跟据最大公因数性质 ...