All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.toolchain.memoryDatabase.invertedIndex.InvertedIndex Maven / Gradle / Ivy

package com.neko233.toolchain.memoryDatabase.invertedIndex;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 倒排索引
 *
 * @author SolarisNeko
 */
public class InvertedIndex {
    // <分词, >
    private final Map>> invertedIndex = new ConcurrentHashMap<>();

    /**
     * @param documentId    改文章的文档 id
     * @param documentWords 出现过的分词
     */
    public void addDocument(int documentId, List documentWords) {
        for (int i = 0; i < documentWords.size(); i++) {
            String word = documentWords.get(i);
            Map> documentFrequencyMap = invertedIndex.computeIfAbsent(word, k -> new ConcurrentHashMap<>());
            long documentFrequency = i + 1;

            Set documentIds = documentFrequencyMap.computeIfAbsent(documentFrequency, k -> ConcurrentHashMap.newKeySet());
            documentIds.add(documentId);
        }
    }

    public Map> getDocumentFrequencyMap(String word) {
        return invertedIndex.get(word);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy