繁体   English   中英

获得词频的更有效方法

[英]More efficient way of getting frequency of words

我想通过单词的开头来计算ArrayList中每个单词的频率。 例如[cat,cog,mouse]将表示以c开头的两个单词和以m开头的一个单词。 我的代码工作正常,但是字母表中有26个字母, 如果 s则需要更多。 还有其他方法吗?

public static void  countAlphabeticalWords(ArrayList<String> arrayList) throws IOException
{
    int counta =0, countb=0, countc=0, countd=0,counte=0;
    String word = "";
    for(int i = 0; i<arrayList.size();i++)
    {

        word = arrayList.get(i);

          if (word.charAt(0) == 'a' || word.charAt(0) == 'A'){ counta++;}
          if (word.charAt(0) == 'b' || word.charAt(0) == 'B'){ countb++;}    

    }
    System.out.println("The number of words begining with A are: " + counta);
    System.out.println("The number of words begining with B are: " + countb);

}

使用地图

public static void  countAlphabeticalWords(List<String> arrayList) throws IOException {
  Map<Character,Integer> counts = new HashMap<Character,Integer>();
  String word = "";

  for(String word : list) {
    Character c = Character.toUpperCase(word.charAt(0));
    if (counts.containsKey(c)) {
      counts.put(c, counts.get(c) + 1);
    }
    else {
      counts.put(c, 1);
    }
  }

  for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
    System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
  }

或使用Map和AtomicInteger(根据Jarrod Roberson)

public static void  countAlphabeticalWords(List<String> arrayList) throws IOException {
  Map<Character,AtomicInteger> counts = new HashMap<Character,AtomicInteger>();
  String word = "";

  for(String word : list) {
    Character c = Character.toUpperCase(word.charAt(0));
    if (counts.containsKey(c)) {
      counts.get(c).incrementAndGet();
    }
    else {
      counts.put(c, new AtomicInteger(1));
    }
  }

  for (Map.Entry<Character, AtomicInteger> entry : counts.entrySet()) {
    System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
  }

最佳实践

切勿执行list.get(i) ,而应使用for(element : list) 并且永远不要在签名中使用ArrayList ,而应使用Interface List这样您可以更改实现。

这个怎么样? 考虑到单词仅以[a-zA-Z]开头:

public static int[] getCount(List<String> arrayList) {
    int[] data = new int[26];
    final int a = (int) 'a';

    for(String s : arrayList) {
        data[((int) Character.toLowerCase(s.charAt(0))) - a]++;
    }

    return data;
}

编辑:

出于好奇 ,我做了一个非常简单的测试,将我的方法和Steph的方法与map进行了比较。 列出236个项目,进行10000000次迭代(不打印结果):我的代码花费了大约10000ms,Steph花费了大约65000ms。

测试: http//pastebin.com/HNBgKFRk

数据: http//pastebin.com/UhCtapZZ

现在,每个字符都可以转换为整数,表示ASCII十进制。 例如, (int)'a'是97. 'z'的ASCII十进制是122 http://www.asciitable.com/

您可以为字符创建一个查找表:

int characters = new int[128]

然后在算法循环中,使用ASCII十进制作为索引并增加值:

word = arrayList.get(i);
characters[word.charAt(0)]++;

最后,您可以打印字符的出现情况:

for (int i = 97; i<=122; i++){
  System.out.println(String.format("The number of words beginning with %s are: %d", (char)i, characters[i]));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM