public void testHighestCountFirst() { Multiset<String> multiset = HashMultiset.create(Arrays.asList("a", "a", "a", "b", "c", "c")); ImmutableMultiset<String> sortedMultiset = Multisets.copyHighestCountFirst(multiset); assertThat(sortedMultiset.entrySet()) .containsExactly( Multisets.immutableEntry("a", 3), Multisets.immutableEntry("c", 2), Multisets.immutableEntry("b", 1)) .inOrder(); assertThat(sortedMultiset).containsExactly("a", "a", "a", "c", "c", "b").inOrder(); assertThat(Multisets.copyHighestCountFirst(ImmutableMultiset.of())).isEmpty(); }
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); }
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); }
public MultisetAtom<K> getCopyHighestCountFirst () { return MultisetAtom.getAtom(Multisets.copyHighestCountFirst(set)); }
public String getMostUsedArticleCasing() { HashMultiset<String> articleNames = HashMultiset.create(); String result; for (Writable writable: super.get()) { LinkWritable link = (LinkWritable)writable; articleNames.add(link.getArticle().toString()); } ImmutableMultiset<String> sorted = Multisets.copyHighestCountFirst(articleNames); result = (String)sorted.elementSet().toArray()[0]; return result; } }
/** * Return the elements that have been seen at least nSeen times. * * @param nSeen * @param baseMultiset * @return */ public static <T> Set<T> getElementsUpToCount(final int nSeen, final Multiset<T> baseMultiset) { checkArgument(nSeen > 0); final Set<T> toKeep = Sets.newHashSet(); for (final Entry<T> entry : Multisets.copyHighestCountFirst( checkNotNull(baseMultiset)).entrySet()) { if (entry.getCount() < nSeen) { break; } toKeep.add(entry.getElement()); } return toKeep; }
/** @return Tokens with their count, sorted by frequency decreasing, then lexicographically ascending */ private ImmutableMultiset<String> filterAndSort(final Multiset<String> counts) { // This isn't terribly efficient, but it is deterministic // Unfortunately, Guava's multiset doesn't give us a clean way to order both by count and element return Multisets.copyHighestCountFirst( ImmutableSortedMultiset.copyOf( Multisets.filter( counts, new Predicate<String>() { @Override public boolean apply(String s) { return counts.count(s) >= minFrequency; } } ) ) ); }
/** @return Tokens with their count, sorted by frequency decreasing, then lexicographically ascending */ private ImmutableMultiset<String> filterAndSort(final Multiset<String> counts) { // This isn't terribly efficient, but it is deterministic // Unfortunately, Guava's multiset doesn't give us a clean way to order both by count and element return Multisets.copyHighestCountFirst( ImmutableSortedMultiset.copyOf( Multisets.filter( counts, new Predicate<String>() { @Override public boolean apply(String s) { return counts.count(s) >= minFrequency; } } ) ) ); }
private static Set<String> collectRepeatedNames(Collection<? extends Described> describedCollection) { if (CollectionUtils.isEmpty(describedCollection)) { return ImmutableSet.of(); } Multiset<String> names = LinkedHashMultiset.create(); for (Described described : describedCollection) { if (described == null) { throw new IllegalArgumentException("A null described was provided"); } names.add(described.getName()); } names = Multisets.copyHighestCountFirst(names); Set<String> repeatedNames = new HashSet<>(); for (String name : names) { if (names.count(name) == 1) { break; } repeatedNames.add(name); } return repeatedNames; }
public void printStats(PrintOpts opts) { if (opts != PrintOpts.SIMPLE) { if (collectExamples) { printExamples(); } log.info("Phone edit distance histo: "); int total = 0; for (Multiset.Entry<Integer> entry : phoneEditHisto.entrySet()) { total += entry.getCount(); log.info(" " + entry.getElement() + " = " + entry.getCount() + " - " + Percent.print(total, totalWords)); } log.info("No phones words that were skipped " + noCodes); log.info("Answer found in top-k answer?"); total = 0; for (Multiset.Entry<Integer> entry : copyHighestCountFirst(rightAnswerInTop).entrySet()) { total += entry.getCount(); log.info( " In top " + entry.getElement() + " - " + entry.getCount() + " - " + Percent.print(total, totalWords)); } } log.info("Total words " + totalWords + ", total right " + totalRightWords + " - " + Percent .print(totalRightWords, totalWords)); log.info("Total phones " + totalPhones + ", total right " + totalRightPhones + " - " + Percent.print(totalRightPhones, totalPhones)); }
public void testHighestCountFirst() { Multiset<String> multiset = HashMultiset.create( Arrays.asList("a", "a", "a", "b", "c", "c")); ImmutableMultiset<String> sortedMultiset = Multisets.copyHighestCountFirst(multiset); assertThat(sortedMultiset.entrySet()).containsExactly( Multisets.immutableEntry("a", 3), Multisets.immutableEntry("c", 2), Multisets.immutableEntry("b", 1)).inOrder(); assertThat(sortedMultiset).containsExactly( "a", "a", "a", "c", "c", "b").inOrder(); assertThat(Multisets.copyHighestCountFirst(ImmutableMultiset.of())).isEmpty(); }
private static void processNames(final FulltextIndexDocument indexDocument, final List<IdentityIndexDocument> identities) { List<IdentityDesignation> designations = new ArrayList<IdentityDesignation>(); for (IdentityIndexDocument identity : identities) { if (StringUtils.isNotBlank(identity.getName()) || StringUtils.isNotBlank(identity.getSurname())) { designations.add(new IdentityDesignation(identity.getName(), identity.getSurname())); } } final Multiset<IdentityDesignation> multiSet = Multisets.copyHighestCountFirst(HashMultiset.create(designations)); if (!multiSet.isEmpty()) { final IdentityDesignation designation = multiSet.iterator().next(); indexDocument.addField(PeopleIndexFieldConstants.FIELD_MAIN_NAME, UserProfileUtils.createFullName(designation.getForenames(), designation.getSurname())); indexDocument.addField(PeopleIndexFieldConstants.FIELD_SURNAME, designation.getSurname()); indexDocument.addField(PeopleIndexFieldConstants.FIELD_FORENAMES, designation.getForenames()); indexDocument.addField(IndexFieldConstants.FOR_SORT_PREFIX + IndexFieldConstants.SEP + PeopleIndexFieldConstants.FIELD_NAME, UserProfileUtils.createNameForSorting(designation.getForenames(), designation.getSurname())); } for (IdentityDesignation designation : multiSet) { final String name = UserProfileUtils.createFullName(designation.getForenames(), designation.getSurname()); storeInAllMetadataField(indexDocument, name); indexDocument.addField(PeopleIndexFieldConstants.FIELD_SUGESTION, name); indexDocument.addField(PeopleIndexFieldConstants.FIELD_NAME, name); } }
private void reconnectToNearestProducer(DisqueConnection<K, V> disqueConnection, boolean forcedReconnect) { log.debug("reconnectToNearestProducer()"); Set<Multiset.Entry<String>> stats = Multisets.copyHighestCountFirst(nodePrefixes).entrySet(); nodePrefixes.clear(); if (!isNodeSwitchNecessary(stats) && !forcedReconnect) { return; } String nodeIdPrefix = getNodeIdPrefix(stats); if (nodeIdPrefix != null) { log.debug("Set preferred node prefix to {}", nodeIdPrefix); socketAddressSupplier.setPreferredNodeIdPrefix(nodeIdPrefix); } if (disqueConnection.isOpen()) { if (nodeIdPrefix == null) { log.info("Initiating reconnect"); } else { log.info("Initiating reconnect to preferred node with prefix {}", nodeIdPrefix); } disconnect((RedisChannelHandler<?, ?>) disqueConnection); } }