Example usage for com.google.common.collect Multisets copyHighestCountFirst

List of usage examples for com.google.common.collect Multisets copyHighestCountFirst

Introduction

In this page you can find the example usage for com.google.common.collect Multisets copyHighestCountFirst.

Prototype

@Beta
public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) 

Source Link

Document

Returns a copy of multiset as an ImmutableMultiset whose iteration order is highest count first, with ties broken by the iteration order of the original multiset.

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();//  ww w .  j a  v  a  2  s  .  c o  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:org.carcv.impl.core.util.CollectionUtils.java

public static <T> T highestCountElement(final List<T> list) throws NoSuchElementException {
    final Multiset<T> plateSet = HashMultiset.create();
    plateSet.addAll(list);/* w w w  . j  a  v  a  2 s  .  c  o m*/
    return Multisets.copyHighestCountFirst(plateSet).iterator().next();
}

From source file:com.monpie.histogram.OneThread.java

@Override
public void run() {
    histogram = HashMultiset.create();//  w  w  w .  ja  v a 2s.c  o m

    String randomArray[][] = RandomizeArray.array;

    for (int i = 0; i < RandomizeArray.ROWS; i++) {
        for (int j = 0; j < RandomizeArray.COLUMNS; j++) {
            histogram.add(randomArray[i][j]);
        }
    }

    for (String asciiCharacter : Multisets.copyHighestCountFirst(histogram).elementSet()) {
        System.out.println(asciiCharacter + " : " + histogram.count(asciiCharacter));
    }
    System.out.println("Sum of all elements: " + histogram.size());
}

From source file:com.github.fhirschmann.clozegen.lib.util.MultisetUtils.java

/**
 * Sorts a multiset by its counts and returns a new {@link LinkedHashMultiset}.
 *
 * @param <E> the type of the multiset elements
 * @param multiset to multiset to sort/*w w w.j  a  v a 2  s.c  om*/
 * @return a new mutable sorted multiset
 */
public static <E> LinkedHashMultiset<E> sortMultiSet(final Multiset<E> multiset) {
    final ImmutableMultiset<E> immutableSet = Multisets.copyHighestCountFirst(multiset);
    return LinkedHashMultiset.create(immutableSet);
}

From source file:edu.washington.cs.cupid.standard.MostFrequent.java

@Override
public LinearJob<List<V>, V> getJob(final List<V> input) {
    return new LinearJob<List<V>, V>(this, input) {
        @Override// w  ww.  j  a  v  a2  s . com
        protected LinearStatus<V> run(final IProgressMonitor monitor) {
            try {
                monitor.beginTask(getName(), 100);

                Multiset<V> set = HashMultiset.create();
                set.addAll(input);
                for (V val : Multisets.copyHighestCountFirst(set)) {
                    return LinearStatus.makeOk(getCapability(), val);
                }

                return LinearStatus.<V>makeError(
                        new IllegalArgumentException("Cannot get most frequent element of empty collection"));
            } catch (Exception ex) {
                return LinearStatus.<V>makeError(ex);
            } finally {
                monitor.done();
            }
        }
    };
}

From source file:com.google.caliper.worker.AllocationStats.java

private AllocationStats(int allocationCount, long allocationSize, int reps, Multiset<Allocation> allocations) {
    checkArgument(allocationCount >= 0, "allocationCount (%s) was negative", allocationCount);
    this.allocationCount = allocationCount;
    checkArgument(allocationSize >= 0, "allocationSize (%s) was negative", allocationSize);
    this.allocationSize = allocationSize;
    checkArgument(reps >= 0, "reps (%s) was negative", reps);
    this.reps = reps;
    this.allocations = Multisets.copyHighestCountFirst(allocations);
}

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 www .  j  a  v  a2s.  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:org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.NaiveOptimizationAlgorithm.java

/**
 * Get the scheduling column order by arrows
 *
 * @param arrows/*w w w. j ava2  s  .co  m*/
 *            the list of visible links
 * @return the list of weights, by thread ID
 */
@Override
public Map<Integer, Long> apply(Collection<ILinkEvent> arrows) {
    /*
     * "transitions" contains the count of every arrows between two tids
     * (Pair<Integer, Integer>). For constructing the Pair, we always put
     * the smallest tid first
     */
    Multiset<Pair<Integer, Integer>> transitions = HashMultiset.<Pair<Integer, Integer>>create();

    /*
     * We iterate in arrows to count the number of transitions between every
     * pair (tid,tid) in the current view
     */
    for (ILinkEvent arrow : arrows) {
        ITimeGraphEntry from = arrow.getEntry();
        ITimeGraphEntry to = arrow.getDestinationEntry();
        if (!(from instanceof ControlFlowEntry) || !(to instanceof ControlFlowEntry)) {
            continue;
        }
        int fromTid = ((ControlFlowEntry) from).getThreadId();
        int toTid = ((ControlFlowEntry) to).getThreadId();
        if (fromTid != toTid) {
            Pair<Integer, Integer> key = new Pair<>(Math.min(fromTid, toTid), Math.max(fromTid, toTid));
            transitions.add(key);
        }
    }

    /*
     * We now have a transition count for every pair (tid,tid). The next
     * step is to sort every pair according to its count in decreasing order
     */
    List<Pair<Integer, Integer>> sortedTransitionsByCount = Multisets.copyHighestCountFirst(transitions)
            .asList();

    /*
     * Next, we find the order in which we want to display our threads. We
     * simply iterate in every pair (tid,tid) in orderedTidList. Each time
     * we see a new tid, we add it at the end of orderedTidList. This way,
     * threads with lots of transitions will be grouped in the top. While
     * very naive, this algorithm is fast, simple and gives decent results.
     */
    Map<Integer, Long> orderedTidMap = new LinkedHashMap<>();
    long pos = 0;
    for (Pair<Integer, Integer> threadPair : sortedTransitionsByCount) {
        if (orderedTidMap.get(threadPair.getFirst()) == null) {
            orderedTidMap.put(threadPair.getFirst(), pos);
            pos++;
        }
        if (orderedTidMap.get(threadPair.getSecond()) == null) {
            orderedTidMap.put(threadPair.getSecond(), pos);
            pos++;
        }
    }

    return orderedTidMap;
}

From source file:com.github.fhirschmann.clozegen.lib.multiset.MapMultiset.java

/**
 * Returns a copy of the {@link Multiset} identified by {@code key} as an
 * {@link ImmutableMultiset} whose order is highest count first, with ties broken by
 * the iteration order of the original multiset.
 *
 * @param key the key to identify the multiset by
 * @return a new {@link ImmutableMultiset} whose order is highest count first
 *//* www  . ja  v a  2s . c om*/
public ImmutableMultiset<V> getSorted(final K key) {
    return Multisets.copyHighestCountFirst(checkNotNull(get(key)));
}

From source file:com.github.fhirschmann.clozegen.lib.multiset.WriteMultisets.java

/**
 * Sorts a Multiset by its counts before writing it to a file using
 * {@link WriteMultisets#writeMultiSet(Multiset, File)}.
 *
 * @param multiset the multiset to write
 * @param minFrequency the minimum frequency a word must have in order to be written
 * @param file the file to write to//  w  ww  .  j a  v  a  2 s  .c  om
 * @throws IOException on errors opening/writing to the file
 */
public static void writeSortedMultiSet(final Multiset<String> multiset, final int minFrequency, final File file)
        throws IOException {
    final ImmutableMultiset<String> im = Multisets.copyHighestCountFirst(multiset);
    writeMultiSet(im, minFrequency, file);
}