如何使用Java 8流获取Map中最常用的单词以及相应的出现频率?

Cac*_*eli 8 java multimap java-8 java-stream

我有一个IndexEntry看起来像这样的课:

public class IndexEntry implements Comparable<IndexEntry>
{
    private String word;
    private int frequency;
    private int documentId;
    ...
    //Simple getters for all properties
    public int getFrequency()
    {
        return frequency;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

我将这个类的对象存储在Guava中SortedSetMultimap(每个键允许多个值),我将一个String单词映射到一些IndexEntrys.在幕后,它将每个单词映射到一个单词SortedSet<IndexEntry>.

我试图在文档中实现一种文档的单词索引结构及其出现频率.

我知道如何计算最常用的词,但我似乎无法得到这个词本身.

以下是我必须得到最常用词的计数,其中entriesSortedSetMultimap,与辅助方法一起:

public int mostFrequentWordFrequency()
{
    return entries
            .keySet()
            .stream()
            .map(this::totalFrequencyOfWord)
            .max(Comparator.naturalOrder()).orElse(0);
}

public int totalFrequencyOfWord(String word)
{
    return getEntriesOfWord(word)
            .stream()
            .mapToInt(IndexEntry::getFrequency)
            .sum();
}

public SortedSet<IndexEntry> getEntriesOfWord(String word)
{
    return entries.get(word);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试学习Java 8的功能,因为它们看起来非常有用.但是,我似乎无法按照我想要的方式运行流.我希望能够在流的末尾同时拥有单词和它的频率,但是除非我有这个单词,否则我可以很容易地得到该单词的总出现次数.

目前,我一直以a结尾Stream<SortedSet<IndexEntry>>,我无能为力.我不知道如何在没有频率的情况下获得最频繁的单词,但如果我有频率,我似乎无法跟踪相应的单词.我尝试创建一个WordFrequencyPairPOJO类来存储它们,但后来我只有一个Stream<SortedSet<WordFrequencyPair>>,我无法弄清楚如何将它映射到有用的东西.

我错过了什么?

Jac*_* G. 6

我认为使用documentId作为关键TreeMultimap而不是word:的关键是更好的设计:

import com.google.common.collect.*;

public class Main {

    TreeMultimap<Integer, IndexEntry> entries = TreeMultimap.<Integer, IndexEntry>create(Ordering.arbitrary(), Ordering.natural().reverse());

    public static void main(String[] args) {
        // Add elements to `entries`

        // Get the most frequent word in document #1
        String mostFrequentWord = entries.get(1).first().getWord();
    }

}

class IndexEntry implements Comparable<IndexEntry> {

    private String word;

    private int frequency;

    private int documentId;

    public String getWord() {
        return word;
    }

    public int getFrequency() {
        return frequency;
    }

    public int getDocumentId() {
        return documentId;
    }

    @Override
    public int compareTo(IndexEntry i) {
        return Integer.compare(frequency, i.frequency);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下方法实现之前的方法:

public static int totalFrequencyOfWord(String word) {
    return entries.values()
                   .stream()
                   .filter(i -> word.equals(i.getWord()))
                   .mapToInt(IndexEntry::getFrequency)
                   .sum();
}

/**
 * This method iterates through the values of the {@link TreeMultimap},
 * searching for {@link IndexEntry} objects which have their {@code word}
 * field equal to the parameter, word.
 *
 * @param word
 *     The word to search for in every document.
 * @return
 *     A {@link List<Pair<Integer, Integer>>} where each {@link Pair<>}
 *     will hold the document's ID as its first element and the frequency
 *     of the word in the document as its second element.
 *
 * Note that the {@link Pair} object is defined in javafx.util.Pair
 */  
public static List<Pair<Integer, Integer>> totalWordUses(String word) {
    return entries.values()
                   .stream()
                   .filter(i -> word.equals(i.getWord()))
                   .map(i -> new Pair<>(i.getDocumentId(), i.getFrequency()))
                   .collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)

  • 别客气!我用新方法编辑了帖子.它返回一个`List <Pair <Integer,Integer >>`,它保存文档中特定单词的文档ID和频率.它基本上是一个元组列表. (2认同)