Google Guava 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-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 3

Однако я получаю так:

Lemurs 1
Lemurs 1
Lemurs 1

Что я делаю неправильно?

2 ответа

Решение

MultiSet работает отлично. Внимательно посмотрите на свои результаты - переключение printf например "|%S| %d" поможет:

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

Сразу видно, что это все 3 разные строки. Решение в этом случае состоит в том, чтобы просто убрать все неалфавитные символы и вставить все слова в нижний регистр.

С помощью printf("%S %d", words, count) с большой буквы S скрывает детали, что различные заглавные буквы слова "лемуры" учитываются отдельно. Когда я запускаю эту программу, я вижу

  • один случай "лемуров". с периодом не обрезается
  • одно вхождение "лемуров" все строчные
  • одно вхождение "лемуров" с заглавной буквой
Другие вопросы по тегам