Example usage for com.google.common.collect ImmutableMultiset count

List of usage examples for com.google.common.collect ImmutableMultiset count

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultiset count.

Prototype

int count(@Nullable Object element);

Source Link

Document

Returns the number of occurrences of an element in this multiset (the count of the element).

Usage

From source file:org.trnltk.experiment.morphology.ambiguity.AmbiguityClassifier.java

public static void main(String[] args) throws IOException, JSONException {
    int numberOfWords = 0;
    int numberOfParseResults = 0;
    final Multiset<ParseResultDifference> differenceSet = HashMultiset.create();
    final Multiset<ParseResultDifference> differenceSetWithoutRootDifferences = HashMultiset.create();

    final File folder = new File("D:\\devl\\data\\1MSentences\\split");

    final File[] files = folder.listFiles();
    if (files == null)
        throw new RuntimeException();

    final StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from  www  .  j  a  v  a2  s .  co  m*/
    for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
        File file = files[fileIndex];
        System.out.println("Processing file " + file);
        //            final BufferedReader reader = new BufferedReader(new FileReader(file));
        //            while (reader.ready()) {
        //                reader.readLine();
        //            }
        final ParseResultReader parseResultReader = new ParseResultReader();
        final ParseResultDiffTool parseResultDiffTool = new ParseResultDiffTool();

        final List<WordParseResultEntry> parseResultEntries = parseResultReader
                .getParseResultEntries(Files.newReader(file, Charsets.UTF_8));
        numberOfWords += parseResultEntries.size();
        //noinspection ForLoopReplaceableByForEach
        for (int parseResultEntryIndex = 0; parseResultEntryIndex < parseResultEntries
                .size(); parseResultEntryIndex++) {
            WordParseResultEntry parseResultEntry = parseResultEntries.get(parseResultEntryIndex);
            final List<ParseResult> parseResults = parseResultEntry.getParseResults();
            numberOfParseResults += parseResults.size();
            for (int i = 0; i < parseResults.size(); i++) {
                final ParseResult leftParseResult = parseResults.get(i);
                for (int j = i + 1; j < parseResults.size(); j++) {
                    final ParseResult rightParseResult = parseResults.get(j);

                    final ParseResultDifference difference = parseResultDiffTool.findDifference(leftParseResult,
                            rightParseResult);
                    final boolean added = differenceSet.add(difference);
                    if (added && difference.hasNoRootDifference() && difference.hasPartDifference())
                        differenceSetWithoutRootDifferences.add(difference);
                }
            }
        }
        //noinspection ConstantConditions
        if (fileIndex == 0)
            break;
    }

    stopWatch.stop();
    final long time = stopWatch.getTime();
    System.out.println(stopWatch);
    System.out.println(Long.valueOf(time).doubleValue() / (51));

    System.out.println("Number of words : " + numberOfWords);
    System.out.println("Number of parseResults : " + numberOfParseResults);
    System.out.println("Number of distinct differences : " + differenceSet.elementSet().size());
    System.out.println("numberOfDistinctDifferencesWithoutRootDifference : "
            + differenceSetWithoutRootDifferences.elementSet().size());

    final ImmutableMultiset<ParseResultDifference> sortedDifferenceSetWithoutRootDifferences = Multisets
            .copyHighestCountFirst(differenceSetWithoutRootDifferences);
    for (ParseResultDifference parseResultDifference : sortedDifferenceSetWithoutRootDifferences.elementSet()) {
        final int count = sortedDifferenceSetWithoutRootDifferences.count(parseResultDifference);
        if (count > 100) {
            System.out.println(count);
            System.out.println(parseResultDifference);
        }
    }

}

From source file:com.publictransitanalytics.scoregenerator.output.ComparativeSectorReachInformation.java

private static Map<SimplePath, Integer> getPathCounts(final Set<MovementPath> bestPaths)
        throws InterruptedException {
    final Map<SimplePath, Integer> pathCounts;
    final TreeMultimap<Integer, SimplePath> frequencyMap = TreeMultimap.create(Integer::compareTo,
            (p1, p2) -> p1.toString().compareTo(p2.toString()));

    if (bestPaths != null) {
        final ImmutableMultiset.Builder<SimplePath> bestSimplePathsBuilder = ImmutableMultiset.builder();
        for (final MovementPath bestPath : bestPaths) {
            bestSimplePathsBuilder.add(new SimplePath(bestPath));
        }//  ww  w  .ja  va2  s  . c  om
        final ImmutableMultiset<SimplePath> bestSimplePaths = bestSimplePathsBuilder.build();

        for (final SimplePath path : bestSimplePaths.elementSet()) {
            frequencyMap.put(bestSimplePaths.count(path), path);
        }

        pathCounts = new LinkedHashMap<>();
        for (final Integer frequency : frequencyMap.keySet().descendingSet()) {
            final NavigableSet<SimplePath> pathsForFrequency = frequencyMap.get(frequency);
            for (final SimplePath pathForFrequency : pathsForFrequency) {
                pathCounts.put(pathForFrequency, frequency);
            }
        }
    } else {
        pathCounts = null;
    }
    return pathCounts;
}

From source file:de.topobyte.osm4j.utils.executables.OsmContributorHistogram.java

private void run() throws IOException {
    long total = 0;

    OsmIterator iterator = createIterator();
    while (iterator.hasNext()) {
        EntityContainer entityContainer = iterator.next();
        OsmEntity entity = entityContainer.getEntity();
        OsmMetadata metadata = entity.getMetadata();

        total++;//from ww  w .j av  a 2s .  c  o m

        if (metadata == null) {
            continue;
        }

        long uid = metadata.getUid();
        String username = metadata.getUser();
        counter.add(uid);

        if (!map.containsEntry(uid, username)) {
            map.put(uid, username);
        }
    }

    if (counter.isEmpty()) {
        System.out.println("No metadata found");
        return;
    }

    long sum = 0;

    ImmutableMultiset<Long> histogram = Multisets.copyHighestCountFirst(counter);

    long first = histogram.iterator().next();
    int firstCount = histogram.count(first);
    int firstLength = String.format("%d", firstCount).length();
    String pattern = String.format("[%%6.2f%%%%] %%%dd: %%d (%%s)", firstLength);

    for (long id : histogram.elementSet()) {
        int count = histogram.count(id);
        sum += count;
        double done = sum / (double) total;
        List<String> names = new ArrayList<>(map.get(id));
        Collections.sort(names);
        System.out.println(String.format(pattern, done * 100, count, id, Joiner.on(",").join(names)));
    }

    finish();
}

From source file:com.publictransitanalytics.scoregenerator.output.SectorReachInformation.java

public SectorReachInformation(final Set<MovementPath> bestPaths, final int count,
        final Set<LocalDateTime> reachTimes) throws InterruptedException {

    reachCount = count;/*from   w w w  .  ja va2 s. c  o  m*/
    this.reachTimes = reachTimes.stream().map(time -> time.toLocalTime().toString())
            .collect(Collectors.toSet());

    final TreeMultimap<Integer, SimplePath> frequencyMap = TreeMultimap.create(Integer::compareTo,
            (p1, p2) -> p1.toString().compareTo(p2.toString()));

    if (bestPaths != null) {
        final ImmutableMultiset.Builder<SimplePath> bestSimplePathsBuilder = ImmutableMultiset.builder();
        for (final MovementPath bestPath : bestPaths) {
            bestSimplePathsBuilder.add(new SimplePath(bestPath));
        }
        final ImmutableMultiset<SimplePath> bestSimplePaths = bestSimplePathsBuilder.build();

        for (final SimplePath path : bestSimplePaths.elementSet()) {
            frequencyMap.put(bestSimplePaths.count(path), path);
        }

        pathCounts = new LinkedHashMap<>();
        for (final Integer frequency : frequencyMap.keySet().descendingSet()) {
            final NavigableSet<SimplePath> pathsForFrequency = frequencyMap.get(frequency);
            for (final SimplePath pathForFrequency : pathsForFrequency) {
                pathCounts.put(pathForFrequency, frequency);
            }
        }
    } else {
        pathCounts = null;
    }
}

From source file:cpw.mods.inventorysorter.SortingHandler.java

private void distributeInventory(Action.ActionContext context, Multiset<ItemStackHolder> itemcounts) {
    InventoryCrafting ic = (InventoryCrafting) context.slot.inventory;
    Multiset<ItemStackHolder> slotCounts = TreeMultiset.create(new InventoryHandler.ItemStackComparator());
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                slotCounts.add(new ItemStackHolder(is));
            }/*  w ww  .j a  v a2s.  c  om*/
        }
    }

    final ImmutableMultiset<ItemStackHolder> staticcounts = ImmutableMultiset.copyOf(itemcounts);
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                ItemStackHolder ish = new ItemStackHolder(is);
                int count = staticcounts.count(ish);
                int slotNum = slotCounts.count(ish);
                final int occurrences = count / slotNum;
                itemcounts.remove(ish, occurrences);
                is.stackSize = occurrences;
            }
        }
    }
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                ItemStackHolder ish = new ItemStackHolder(is);
                if (itemcounts.count(ish) > 0) {
                    is.stackSize += itemcounts.setCount(ish, 0);
                }
            }
        }
    }
    for (int slot = context.slotMapping.begin; slot < context.slotMapping.end + 1; slot++) {
        context.player.openContainer.getSlot(slot).onSlotChanged();
    }
}

From source file:org.trnltk.apps.experiments.AmbiguityMatrixApp.java

private List<String> getDistinctWordsWithEnoughOccurrences(ImmutableMultiset<String> wordCountSet) {
    final List<String> uniqueWordsWithEnoughOccurrences = new ArrayList<String>();
    for (String surface : wordCountSet.elementSet()) {
        int count = wordCountSet.count(surface);
        if (count > 5)
            uniqueWordsWithEnoughOccurrences.add(surface);
    }//from w ww  .  j a  v a2  s .c o m
    return uniqueWordsWithEnoughOccurrences;
}

From source file:org.trnltk.apps.experiments.AmbiguityMatrixApp.java

private Map<String, Integer> buildTotalAmbiguityMap(ImmutableMultiset<String> wordCountSet,
        Map<String, List<String>> resultMap) {
    HashMap<String, Integer> totalAmbiguityMap = new HashMap<String, Integer>();

    for (String word : wordCountSet) {
        int occurrenceCount = wordCountSet.count(word);
        List<String> results = resultMap.get(word);
        if (results != null)
            // for now, assume score grows with parse result count's square
            totalAmbiguityMap.put(word, occurrenceCount * results.size() * results.size());
    }// w w  w . j  a v  a  2 s.co m

    return totalAmbiguityMap;
}

From source file:org.trnltk.apps.experiments.AmbiguityMatrixApp.java

private void printFirstNTopAmbiguousEntries(BufferedWriter writer, ImmutableMultiset<String> wordCountSet,
        Map<String, List<String>> resultMap, SortedMap<String, Integer> sortedTotalAmbiguityMap, int N) {
    final Formatter formatter = new Formatter(writer);
    int i = 0;/* w w w .j a va 2  s  . co m*/
    for (Map.Entry<String, Integer> entry : sortedTotalAmbiguityMap.entrySet()) {
        String surface = entry.getKey();
        Integer totalAmbiguity = entry.getValue();
        int occurrenceCount = wordCountSet.count(surface);
        List<String> parseResults = resultMap.get(surface);
        formatter.format(Locale.getDefault(), "%20s : %10d \t O:%10d \t A:%3d \t %s \n", surface,
                totalAmbiguity, occurrenceCount, parseResults.size(), Joiner.on("  ").join(parseResults));
        if (i++ == N)
            break;
    }
}

From source file:com.android.tools.idea.wizard.ModuleListModel.java

private void checkForDuplicateNames() {
    Collection<ModuleToImport> modules = getSelectedModules();
    ImmutableMultiset<String> names = ImmutableMultiset
            .copyOf(Iterables.transform(modules, new Function<ModuleToImport, String>() {
                @Override//from  w w w.ja v a2s  .c o  m
                public String apply(@Nullable ModuleToImport input) {
                    return input == null ? null : getModuleName(input);
                }
            }));
    for (ModuleToImport module : modules) {
        ModuleValidationState state = myModules.get(module);
        if (state == ModuleValidationState.OK) {
            if (names.count(getModuleName(module)) > 1) {
                myModules.put(module, ModuleValidationState.DUPLICATE_MODULE_NAME);
            }
        }
    }
}