【问题标题】:Counting frequency of a string字符串的计数频率
【发布时间】:2011-12-11 10:54:00
【问题描述】:

我本质上想搜索字符串的频率。例如,如果我传入“我”这个词,那么该词在以下句子中的频率:“去了海滩,看到了三个人”应该是 2。我已经构建了这样的方法,在该方法中,我获取一个文本(任意长度),用空格将其拆分为一个数组,然后循环遍历该数组,搜索每个索引是否与该单词匹配。然后,我增加频率计数器并将数字作为字符串返回。方法如下:

private int freq() {
String text = "I went to the beach and I saw three people";
String search = "I";
String[] splitter = text.split("\\s+");
int counter = 0;
   for (int i=0; i<splitter.length; i++)
   {
       if (splitter[i]==search) 
       {
           counter++;
       }
       else
       {

       }
   }
   return counter;
       }

}  

这是在方法之外:

String final = Integer.toString(freq());
System.out.println(final);

但是当我运行它时,我一直得到 0 作为结果。我不知道我做错了什么。

编辑:你们都是对的!多么浪费一个问题:(。

【问题讨论】:

  • 你得到了一个很好的 HashMap :) 说真的,了解基本数据结构以及何时使用它们是非常重要的。

标签: java arrays string return


【解决方案1】:

使用equals 而不是==

if (text[i].equals(search) )
   {
       counter++;
   }

更好的解决方案

使用地图将单词Map&lt;String,Integer&gt; 与频率进行映射。

String [] words = line.split(" ");

Map<String,Integer> frequency = new HashMap<String,Integer>();

for (String word:words){

    Integer f = frequency.get(word);
    //checking null
    if(f==null) f=0;
    frequency.put(word,f+1);
}

然后你可以找到一个特定的单词:

frequency.get(word)

【讨论】:

  • 我开始写关于正在使用的 log(N) 时间的评论,然后意识到 Map 被实例化为一个接口(不起作用)。我将其更改为具体类型。两种常见的 Map 实现是 TreeMap 和 HashMap。 TreeMap 是 log(N) 时间,但您可以对所有内容进行排序。 HashMap 将为您提供 O(1) 插入和 O(1) 查找。
【解决方案2】:

使用equals()方法比较字符串。

if(text[i].equals(search))
{
   counter++;
}

【讨论】:

  • 我猜他的逻辑也不对!他/她应该通过“拆分器”而不是文本来遍历:)
  • 那是一个错字。我正在穿越拆分器
【解决方案3】:
private int freq() {
    String text = "I went to the beach and I saw three people";
    String search = "I";
    String[] splitter = text.split("\\s+");
    int counter = 0;
/* problem: You want to be looping over splitter. */
    for (int i=0; i<text.length; i++)
    {
/* problem: splitter[i].equals(search) */
        if (text[i]==search)
        {   
            counter++;
        }   
    }
    return counter;
}

【讨论】:

    【解决方案4】:

    为了让您的代码按照其他答案工作,请使用 .equals 而不是 ==,但您也可以使用 apache commons lang:

    StringUtils.countMatches(text, search);
    

    http://commons.apache.org/lang/ http://commons.apache.org/lang/apidocs/org/apache/commons/lang3/StringUtils.html#countMatches(java.lang.CharSequence, java.lang.CharSequence)

    【讨论】:

      【解决方案5】:

      字符串应该与 String.equals 进行比较,而不是 ==,后者检查它们是否是相同的 object,而不是它们是否具有相同的 contents

      【讨论】:

        【解决方案6】:

        要比较两个String,您必须使用equals() 方法而不是简单的==

        【讨论】:

          【解决方案7】:

          您可以使用 Map 将单词作为键,将单词的频率作为值。然后在循环内部,尝试使用try-catch块添加+1键关联到当前单词(tryblock),如果单词没有找到“fdist.get(w)”将给出nullpointerexception,然后简单地catch 1 代表价值。

          Map<String,Integer> fdist = new HashMap<String,Integer>();
          for(String w:s.split(" ")){
              try {
                  fdist.put(word, fdist.get(w)+1);
              } catch (Exception e) {
                  fdist.put(word, 1);
              }
          }
          

          【讨论】:

          • 你的答案应该包含对你的代码的解释和它如何解决问题的描述。
          【解决方案8】:

          确定文件中单词的频率。
          这是java中hashmap的基本代码

          File f = new File(fileName);
           Scanner s = new Scanner(f);
             Map<String, Integer> counts =
            new Map<String, Integer>();
                while( s.hasNext() ){
           String word = s.next();
           if( !counts.containsKey( word ) )
           counts.put( word, 1 );
          else
           counts.put( word, 
              counts.get(word) + 1 ); 
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-18
            • 2017-04-19
            • 2016-08-21
            • 2023-03-25
            • 1970-01-01
            相关资源
            最近更新 更多