Java Google Guava MultiSet返回不正确的值

Java Google Guava MultiSet返回不正确的值,java,guava,word-count,multiset,Java,Guava,Word Count,Multiset,我正在使用Google Guava API计算字数 public static void main(String args[]) { String txt = "Lemurs of Madagascar is a reference work and field guide giving descriptions and biogeographic data for all the known lemur species in Madagascar (ring-taile

我正在使用Google Guava API计算字数

public static void main(String args[])
    {
        String txt = "Lemurs of Madagascar is a reference work and field guide giving descriptions and biogeographic data for all the known lemur species in Madagascar (ring-tailed lemur pictured). It also provides general information about lemurs and their history and helps travelers identify species they may encounter. The primary contributor is Russell Mittermeier, president of Conservation International. The first edition in 1994 received favorable reviews for its meticulous coverage, numerous high-quality illustrations, and engaging discussion of lemur topics, including conservation, evolution, and the recently extinct subfossil lemurs. The American Journal of Primatology praised the second edition's updates and enhancements. Lemur News appreciated the expanded content of the third edition (2010), but was concerned that it was not as portable as before. The first edition identified 50 lemur species and subspecies, compared to 71 in the second edition and 101 in the third. The taxonomy promoted by these books has been questioned by some researchers who view these growing numbers of lemur species as insufficiently justified inflation of species numbers.";

        Iterable<String> result = Splitter.on(" ").trimResults(CharMatcher.DIGIT)
                   .omitEmptyStrings().split(txt);
        Multiset<String> words = HashMultiset.create(result);

        for(Multiset.Entry<String> entry : words.entrySet())
        {
            String word = entry.getElement();
            int count = words.count(word);
            System.out.printf("%S %d", word, count);
            System.out.println();
        }
    }
然而,我变得像这样:

Lemurs 1
Lemurs 1
Lemurs 1
我做错了什么?

使用
printf(“%S%d”,words,count)
和大写字母
S
隐藏了单词“狐猴”的不同大写字母分别被计算的细节。当我运行该程序时,我看到

  • “狐猴”的一次出现,其周期未被修剪
  • 一次出现的“狐猴”全小写
  • 出现一次首字母大写的“狐猴”

MultiSet
工作正常。仔细查看您的结果-将
printf
切换到例如
“|%S |%d”
将有助于:

|lemurs.| 1
|lemurs| 1
|Lemurs| 1
很明显,这些都是3个不同的字符串。本例中的解决方案是简单地去掉所有非字母字符,并将所有单词小写

|lemurs.| 1
|lemurs| 1
|Lemurs| 1