Example usage for java.util TreeMap putIfAbsent

List of usage examples for java.util TreeMap putIfAbsent

Introduction

In this page you can find the example usage for java.util TreeMap putIfAbsent.

Prototype

default V putIfAbsent(K key, V value) 

Source Link

Document

If the specified key is not already associated with a value (or is mapped to null ) associates it with the given value and returns null , else returns the current value.

Usage

From source file:org.apdplat.superword.tools.SentenceScorer.java

public static TreeMap<Float, Map<String, List<String>>> score(String path, int limit) {
    //?  //from w  w w  . j ava2 s. c o  m
    Set<String> fileNames = TextAnalyzer.getFileNames(path);
    //?
    Map<String, AtomicInteger> frequency = TextAnalyzer.frequency(fileNames);
    //?
    TreeMap<Float, Map<String, List<String>>> sentences = new TreeMap<>();
    //??????
    Set<Integer> hashes = new HashSet<>();
    Set<String> repeat = new HashSet<>();
    //?????????
    int count = 0;
    for (String fileName : fileNames) {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new BufferedInputStream(new FileInputStream(fileName))))) {
            String book = Paths.get(fileName).toFile().getName().replace(".txt", "");
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                int hc = line.hashCode();
                if (hashes.contains(hc)) {
                    repeat.add(line);
                    continue;
                }
                hashes.add(hc);
                //
                float score = score(line, frequency);
                if (score > 0) {
                    if (count >= limit) {
                        LOGGER.debug("?????" + limit + "?");
                        return sentences;
                    }
                    count++;
                    sentences.putIfAbsent(score, new HashMap<>());
                    sentences.get(score).putIfAbsent(book, new ArrayList<>());
                    sentences.get(score).get(book).add(line);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    LOGGER.debug("????" + repeat.size());
    AtomicInteger i = new AtomicInteger();
    repeat.forEach(r -> {
        LOGGER.debug("\t" + i.incrementAndGet() + "?" + r);
    });
    LOGGER.debug("???" + count);
    return sentences;
}