Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算字符串哈希映射中出现的单词数_Java_Hashmap - Fatal编程技术网

Java 如何计算字符串哈希映射中出现的单词数

Java 如何计算字符串哈希映射中出现的单词数,java,hashmap,Java,Hashmap,我想知道如何修复代码,使输出正确无误。我只能编辑代码的特定部分。多谢各位 这是我的密码 import java.util.HashMap; public class OccurenceChecker { public static void main(String[] args) { //CANT BE FIXED String phrase = "Good Morning. Welcome to my store. My store is

我想知道如何修复代码,使输出正确无误。我只能编辑代码的特定部分。多谢各位

这是我的密码

import java.util.HashMap;

public class OccurenceChecker {
    public static void main(String[] args) 
    { 

        //CANT BE FIXED
        String phrase = "Good Morning. Welcome to my store. My store is a grocery store.";

        HashMap<String, Integer> map = new HashMap<String, Integer>();
        String[] ignored = phrase.split("\n\t\r(){},:;!?.[]");

        //CAN BE FIX THIS POINT ON.
        for (String ignore : ignored) 
        {
            Integer count = map.get(ignore);
            if (count == null) 
            {
                count = 0;
            }
            map.put(ignore, count + 1);
        }

        for (int i = 0; i< ignored.length; i++)
        {
            System.out.println(ignored[i]);
        }
        System.out.println(map);
    }
}
我的输出

{=2, a=1, Morning=1, grocery=1, Welcome=1, is=1, to=1, store=3, Good=1, my=1, My=1}

你的方法不完全正确(如果你有其他符号怎么办?)。这样做:

  • 将所有非字母数字字符替换为空格
  • 基于分割的空间(
    \\s+
  • 对于拆分数组中的每个字符串:a。检查是否有与字符串相等的键:是:获取值,增加计数并 将值放回原处。否:插入值为1的新键

  • 你的方法不完全正确(如果你有其他符号怎么办?)。这样做:

  • 将所有非字母数字字符替换为空格
  • 基于分割的空间(
    \\s+
  • 对于拆分数组中的每个字符串:a。检查是否有与字符串相等的键:是:获取值,增加计数并 将值放回原处。否:插入值为1的新键

  • 以下几点建议供您考虑:

    在正则表达式中,
    \W
    指的是任何不是单词字符的东西(即任何不是字母的东西)

    如果希望在任何标点符号或空格上拆分,则应在regexp中的
    \W
    后面加一个
    +
    。这将把所有后续的分隔符作为同一分隔符的一部分进行计数。这就是为什么您当前在回答中得到了
    {=2
    (在您的输入中有两个“.”实例,被拆分为delimiter、null、delimiter)

    看起来您希望“my”和“my”被视为同一个字符串。在这种情况下,您应该在将它们添加到映射之前使用
    toLowerCase

    如果您使用的是Java8,那么在映射中维护正在运行的增量的一种简单方法是

    Map<String,Integer> wordCount = new HashMap<>();
    wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
    
    Map wordCount=newhashmap();
    wordCount.put(word,wordCount.getOrDefault(word,0)+1);
    
    同样,使用Java8,您可以一次性完成所有这一切

    Map<String,Long> wordCount = Arrays.stream(phrase.toLowerCase().split("\\W+"))
        .collect(Collectors.groupingBy(Function.identy(), Collectors.counting());
    
    Map wordCount=Arrays.stream(短语.toLowerCase().split(\\W+))
    .collect(Collectors.groupingBy(Function.identy(),Collectors.counting());
    
    一些建议供您考虑:

    在正则表达式中,
    \W
    指的是任何不是单词字符的东西(即任何不是字母的东西)

    如果您希望在任何标点符号或空格上拆分,那么您的regexp中应该在
    \W
    之后有一个
    +
    。这将把所有后续的标点符号都计算为同一分隔符的一部分。这就是为什么您当前在回答中得到
    {=2
    (有两个“.”的实例在您的输入中,拆分会将其解释为分隔符(null,delimiter)

    看起来您希望“my”和“my”被视为同一个字符串。在这种情况下,您应该在将它们添加到映射之前使用
    toLowerCase

    如果您使用的是Java8,那么在映射中维护正在运行的增量的一种简单方法是

    Map<String,Integer> wordCount = new HashMap<>();
    wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
    
    Map wordCount=newhashmap();
    wordCount.put(word,wordCount.getOrDefault(word,0)+1);
    
    同样,使用Java8,您可以一次性完成所有这一切

    Map<String,Long> wordCount = Arrays.stream(phrase.toLowerCase().split("\\W+"))
        .collect(Collectors.groupingBy(Function.identy(), Collectors.counting());
    
    Map wordCount=Arrays.stream(短语.toLowerCase().split(\\W+))
    .collect(Collectors.groupingBy(Function.identy(),Collectors.counting());
    
    我将以短跑运动员的回答为基础,因为他完全忽略了问题中可以和不能改变的东西

    尽可能使用nuch Java 8。这在您的情况下并不起作用,因为映射已经初始化,所以创建另一个映射并替换它很奇怪

    map = Arrays.stream(ignored)
            .filter(s -> !s.isEmpty()) // removed empty strings
            .map(String::toLowerCase) // makes all the strings lower case
            .collect(Collectors.groupingBy(Function.identy(), Collectors.counting());
    
    使用更基本的Java8特性并使用最初创建的映射

    Arrays.stream(ignored)
            .filter(s -> !s.isEmpty()) // removed empty strings
            .map(String::toLowerCase) // makes all the strings lower case
            .forEach(s -> map.put(s, map.getOrDefault(s, 0) + 1)
    
    没有Java 8

    for (final String s : ignored) {
        if (s.isEmpty()) {
            continue; // skip empty strings
        }
        final String lowerS = s.toLowerCase();
        if (map.containsKey(lowerS)) {
            map.put(lowerS, map.get(lowerS) + 1)
        } else {
            map.put(lowerS, 1)
        }
    }
    

    我将以短跑运动员的回答为基础,因为他完全忽略了问题中可以和不能改变的东西

    尽可能使用nuch Java 8。这在您的情况下并不起作用,因为映射已经初始化,所以创建另一个映射并替换它很奇怪

    map = Arrays.stream(ignored)
            .filter(s -> !s.isEmpty()) // removed empty strings
            .map(String::toLowerCase) // makes all the strings lower case
            .collect(Collectors.groupingBy(Function.identy(), Collectors.counting());
    
    使用更基本的Java8特性并使用最初创建的映射

    Arrays.stream(ignored)
            .filter(s -> !s.isEmpty()) // removed empty strings
            .map(String::toLowerCase) // makes all the strings lower case
            .forEach(s -> map.put(s, map.getOrDefault(s, 0) + 1)
    
    没有Java 8

    for (final String s : ignored) {
        if (s.isEmpty()) {
            continue; // skip empty strings
        }
        final String lowerS = s.toLowerCase();
        if (map.containsKey(lowerS)) {
            map.put(lowerS, map.get(lowerS) + 1)
        } else {
            map.put(lowerS, 1)
        }
    }
    

    一个解决方案是,你可以用空格替换所有标点符号,然后用空格分割,然后用计数将所有的东西放在地图上。一个解决方案是,你可以用空格替换所有标点符号,然后用空格分割,然后用计数将所有的东西放在地图上。非常感谢,有没有关于小写字母应该放在哪里的具体建议?我尝试添加text.toLowerCase(),但它不起作用。@CookieMonst3r
    String.toLowerCase
    在上有文档记录。我不知道为什么它对您不起作用。您有错误吗?非常感谢,关于小写字母的位置有什么具体建议吗?我尝试添加文本。toLowerCase(),但它不起作用。@CookieMonst3r
    String.toLowerCase
    在中有文档记录。我不知道为什么它对您不起作用。您有错误吗?非常感谢Andre!非常感谢Andre!