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

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

Introduction

In this page you can find the example usage for com.google.common.collect Multiset 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.simmetrics.metrics.BlockDistance.java

@Override
public float distance(Multiset<T> a, Multiset<T> b) {

    float distance = 0;

    for (T token : union(a, b).elementSet()) {
        float frequencyInA = a.count(token);
        float frequencyInB = b.count(token);

        distance += abs(frequencyInA - frequencyInB);
    }//  w w  w  .j a v  a 2 s .  co m
    return distance;
}

From source file:codecrafter47.bungeetablistplus.placeholder.RedisBungeePlaceholders.java

@Override
public void setup() {
    bind("server_rplayer_count").to(context -> {
        int sum = 0;
        Optional<ServerGroup> serverGroup = context.getServerGroup();
        if (serverGroup.isPresent()) {
            Multiset<String> serverCount = RedisBungee.getApi().getServerToPlayers().keys();
            for (String server : serverGroup.get().getServerNames()) {
                sum += serverCount.count(server);
            }// w ww  . j ava2 s .c  o  m
        }
        return Integer.toString(sum);
    });
}

From source file:org.simmetrics.metrics.EuclideanDistance.java

@Override
public float distance(Multiset<T> a, Multiset<T> b) {

    float distance = 0.0f;

    for (T token : union(a, b).elementSet()) {
        float frequencyInA = a.count(token);
        float frequencyInB = b.count(token);

        distance += ((frequencyInA - frequencyInB) * (frequencyInA - frequencyInB));
    }//w ww .ja v  a2  s.c om

    return (float) sqrt(distance);
}

From source file:org.caleydo.view.domino.internal.data.Categorical2DDataDomainValues.java

/**
 * @param col//w ww .  ja va  2  s .  c o m
 * @return
 */
private <T> T mostFrequent(Multiset<T> col) {
    T top = null;
    int c = 0;
    for (T elem : col.elementSet()) {
        int elemC = col.count(elem);
        if (elemC > c) {
            top = elem;
            c = elemC;
        }
    }
    return top;
}

From source file:Grep.GrepFiles.java

void printStatistics() {
    try {/*  w w w .jav  a 2s .com*/
        RecursiveFind recursiveFind = new RecursiveFind(directory, packages);
        Multiset<String> stats = PackageStats.getInstance().statistics;

        for (String key : packages) {
            System.out.println(key + " " + stats.count(key));
        }

    } catch (FileNotFoundException ex) {
        System.err.println("File Exceptions");
    }
}

From source file:br.com.caelum.vraptor.observer.upload.CommonsUploadMultipartObserver.java

protected String fixIndexedParameters(String name, Multiset<String> indexes) {
    if (name.contains("[]")) {
        String newName = name.replace("[]", "[" + (indexes.count(name)) + "]");
        indexes.add(name);/* www .java2  s . co m*/
        logger.debug("{} was renamed to {}", name, newName);

        return newName;
    }
    return name;
}

From source file:com.facebook.buck.rules.SymlinkTree.java

/**
 * Because of cross-cell, multiple {@link SourcePath}s can resolve to the same relative path,
 * despite having distinct absolute paths. This presents a challenge for rules that require
 * gathering all of the inputs in one directory.
 *
 * @param sourcePaths set of SourcePaths to process
 * @param resolver resolver/*from w w w  .j  av a  2  s.c om*/
 * @return a map that assigns a unique relative path to each of the SourcePaths.
 */
public static ImmutableBiMap<SourcePath, Path> resolveDuplicateRelativePaths(
        ImmutableSortedSet<SourcePath> sourcePaths, SourcePathResolver resolver) {
    // This serves a dual purpose - it keeps track of whether a particular relative path had been
    // assigned to a SourcePath and how many times a particular relative path had been seen.
    Multiset<Path> assignedPaths = HashMultiset.create();
    ImmutableBiMap.Builder<SourcePath, Path> builder = ImmutableBiMap.builder();
    List<SourcePath> conflicts = new ArrayList<>();

    for (SourcePath sourcePath : sourcePaths) {
        Path relativePath = resolver.getRelativePath(sourcePath);
        if (!assignedPaths.contains(relativePath)) {
            builder.put(sourcePath, relativePath);
            assignedPaths.add(relativePath);
        } else {
            conflicts.add(sourcePath);
        }
    }

    for (SourcePath conflict : conflicts) {
        Path relativePath = resolver.getRelativePath(conflict);
        Path parent = MorePaths.getParentOrEmpty(relativePath);
        String extension = MorePaths.getFileExtension(relativePath);
        String name = MorePaths.getNameWithoutExtension(relativePath);

        while (true) {
            StringBuilder candidateName = new StringBuilder(name);
            candidateName.append('-');
            int suffix = assignedPaths.count(relativePath);
            candidateName.append(suffix);
            if (!extension.isEmpty()) {
                candidateName.append('.');
                candidateName.append(extension);
            }
            Path candidate = parent.resolve(candidateName.toString());

            if (!assignedPaths.contains(candidate)) {
                assignedPaths.add(candidate);
                builder.put(conflict, candidate);
                break;
            } else {
                assignedPaths.add(relativePath);
            }
        }
    }

    return builder.build();
}

From source file:org.datacleaner.components.machinelearning.impl.MLFeatureUtils.java

public static Set<String> sanitizeFeatureVectorSet(Multiset<String> values, MLTrainingConstraints constraints) {
    final Set<String> resultSet;

    final int maxFeatures = constraints.getMaxFeatures();
    if (maxFeatures > 0) {
        resultSet = new TreeSet<>();
        final Iterator<String> highestCountFirst = Multisets.copyHighestCountFirst(values).elementSet()
                .iterator();/*  ww w.  j a  v  a2 s.c om*/
        // populate "resultSet" using "highestCountFirst"
        for (int i = 0; i < maxFeatures; i++) {
            if (highestCountFirst.hasNext()) {
                final String value = highestCountFirst.next();
                resultSet.add(value);
            }
        }
    } else {
        resultSet = new TreeSet<>(values.elementSet());
    }

    final boolean includeFeaturesForUniqueValues = constraints.isIncludeFeaturesForUniqueValues();
    if (!includeFeaturesForUniqueValues) {
        // remove uniques in "values" from "resultSet".
        for (Iterator<String> it = resultSet.iterator(); it.hasNext();) {
            final String value = it.next();
            if (values.count(value) == 1) {
                it.remove();
            }
        }
    }
    return resultSet;
}

From source file:com.cloudera.science.ml.client.cmd.KMeansOutlierCommand.java

private void validate(List<Centers> centers, List<Integer> centerIds,
        Map<ClusterKey, MahalanobisDistance> distances) {
    ImmutableMultiset.Builder<Integer> imb = ImmutableMultiset.builder();
    for (ClusterKey ck : distances.keySet()) {
        imb.add(ck.getClusterId());//w  w  w  . j a v  a 2  s  . c o m
    }
    Multiset<Integer> counts = imb.build();
    for (int i = 0; i < centers.size(); i++) {
        int idx = (centerIds == null || centerIds.isEmpty()) ? i : centerIds.get(i);
        if (counts.count(idx) != centers.get(i).size()) {
            throw new IllegalArgumentException("Covariance/cluster mismatch for cluster ID: " + idx);
        }
    }
}

From source file:com.synflow.cx.internal.compiler.helpers.FsmBeautifier.java

/**
 * Renames the states of the FSM.// w  w  w . j a  v a  2 s .  co  m
 * 
 * @param actorName
 *            base name of the states (simple name of the actor)
 */
private void renameStates(String actorName) {
    // the "currentName" is set by a state with a name
    String currentName = null;
    for (State state : fsm.getStates()) {
        String stateName = state.getName();
        if (stateName == null) {
            if (currentName == null) {
                // this is the case for an actor whose first state has no name
                currentName = "FSM_" + actorName;
            }
            state.setName(currentName);
        } else {
            currentName = stateName;
        }
    }

    // rename consecutive states
    Multiset<String> visited = HashMultiset.create();
    for (State state : fsm.getStates()) {
        String name = state.getName();
        int n = visited.count(name);
        if (n > 0) {
            state.setName(name + "_" + n);
        }

        visited.add(name);
    }
}