List of usage examples for com.google.common.collect Multiset add
@Override
boolean add(E element);
From source file:it.units.malelab.ege.distributed.master.UIRunnable.java
private void inc(String keyName, Object keyValue, JobInfo.Status status, Map<String, Map<Object, Multiset<JobInfo.Status>>> map) { Map<Object, Multiset<JobInfo.Status>> valueCounts = map.get(keyName); if (valueCounts == null) { valueCounts = new TreeMap<>(); map.put(keyName, valueCounts);// w w w . j av a 2s . c o m } Multiset<JobInfo.Status> statuses = valueCounts.get(keyValue); if (statuses == null) { statuses = HashMultiset.create(); valueCounts.put(keyValue, statuses); } statuses.add(status); }
From source file:edu.stanford.nlp.util.JBLEU.java
public void stats(List<String> hyp, List<List<String>> refs, int[] result) { assert result.length == 9; assert refs.size() > 0; // 1) choose reference length int selectedRef = pickReference(hyp, refs, verbosity); int selectedRefLen = refs.get(selectedRef).size(); // TODO: Integer-ify everything inside Ngram? Or is there too much // overhead there? // 2) determine the bag of n-grams we can score against // build a simple tries Multiset<Ngram> clippedRefNgrams = HashMultiset.create(); for (List<String> ref : refs) { Multiset<Ngram> refNgrams = HashMultiset.create(); for (int order = 1; order <= N; order++) { for (int i = 0; i <= ref.size() - order; i++) { List<String> toks = ref.subList(i, i + order); Ngram ngram = new Ngram(toks); refNgrams.add(ngram); }// w w w. j av a2s.c o m } // clip n-grams by taking the maximum number of counts for any given reference for (Ngram ngram : refNgrams) { int clippedCount = Math.max(refNgrams.count(ngram), clippedRefNgrams.count(ngram)); clippedRefNgrams.setCount(ngram, clippedCount); } } // 3) now match n-grams int[] attempts = new int[N]; int[] matches = new int[N]; for (int order = 1; order <= N; order++) { for (int i = 0; i <= hyp.size() - order; i++) { List<String> toks = hyp.subList(i, i + order); Ngram ngram = new Ngram(toks); boolean found = clippedRefNgrams.remove(ngram); ++attempts[order - 1]; if (found) { ++matches[order - 1]; } } } // 4) assign sufficient stats System.arraycopy(attempts, 0, result, 0, N); System.arraycopy(matches, 0, result, N, N); result[N * 2] = selectedRefLen; }
From source file:bacter.model.ACGLikelihood.java
/** * Ensure pattern counts are up to date. *///from w w w . j av a 2s . c o m private void updatePatterns() { List<Region> regionList = acg.getRegions(locus); // Remove stale pattern sets patterns.keySet().retainAll(regionList); patternLogLikelihoods.keySet().retainAll(regionList); rootPartials.keySet().retainAll(regionList); constantPatterns.keySet().retainAll(regionList); for (Region region : regionList) { if (patterns.containsKey(region)) continue; // Add new pattern set Multiset<int[]> patSet = LinkedHashMultiset.create(); for (int j = region.leftBoundary; j < region.rightBoundary; j++) { int[] pat = alignment.getPattern(alignment.getPatternIndex(j)); patSet.add(pat); } patterns.put(region, patSet); // Allocate memory for corresponding log likelihoods and root partials patternLogLikelihoods.put(region, new double[patSet.elementSet().size()]); rootPartials.put(region, new double[patSet.elementSet().size() * nStates]); // Compute corresponding constant pattern list List<Integer> constantPatternList = new ArrayList<>(); int patternIdx = 0; for (int[] pattern : patSet.elementSet()) { boolean isConstant = true; for (int i = 1; i < pattern.length; i++) if (pattern[i] != pattern[0]) { isConstant = false; break; } if (isConstant && !alignment.getDataType().isAmbiguousState(pattern[0])) constantPatternList.add(patternIdx * nStates + pattern[0]); patternIdx += 1; } constantPatterns.put(region, constantPatternList); } }
From source file:edu.uci.ics.sourcerer.tools.java.component.identifier.internal.ComponentRepositoryBuilder.java
private JarSet createSimpleLibraries() { JarSet assignedJars = JarSet.create(); PriorityQueue<Cluster> sortedClusters = new PriorityQueue<>(clusters.size(), Cluster.DESCENDING_SIZE_COMPARATOR); Map<VersionedFqnNode, Cluster> fqnToCluster = new HashMap<>(); for (Cluster cluster : clusters) { sortedClusters.add(cluster);/* w ww.j a va 2 s . co m*/ for (VersionedFqnNode fqn : cluster.getCoreFqns()) { fqnToCluster.put(fqn, cluster); } for (VersionedFqnNode fqn : cluster.getVersionFqns()) { fqnToCluster.put(fqn, cluster); } } int phantomCount = 0; task.start("Creating simple libraries from clusters", "clusters examined", 10_000); while (!sortedClusters.isEmpty()) { // Perhaps this needs to be iterated, too? Cluster biggest = sortedClusters.poll(); Library library = Library.create(biggest); repo.addLibrary(library); { Set<VersionedFqnNode> globalPotentials = new HashSet<>(); Set<VersionedFqnNode> globalPartials = new HashSet<>(); // For each version, find any fqns that always occur for (ClusterVersion version : biggest.getVersions()) { Multiset<FqnVersion> potentials = HashMultiset.create(); for (Jar jar : version.getJars()) { for (FqnVersion fqn : jar.getFqns()) { potentials.add(fqn); } } int max = version.getJars().size(); for (FqnVersion fqn : potentials) { if (potentials.count(fqn) == max) { globalPotentials.add(fqn.getFqn()); } else { globalPartials.add(fqn.getFqn()); } } } globalPotentials.removeAll(globalPartials); globalPotentials.removeAll(biggest.getCoreFqns()); globalPotentials.removeAll(biggest.getVersionFqns()); // Are there any clusters that we match? Set<Cluster> potentialClusters = new HashSet<>(); for (VersionedFqnNode fqn : globalPotentials) { Cluster cluster = fqnToCluster.get(fqn); if (cluster == null) { logger.severe("Missing cluster for FQN: " + fqn.getFqn()); } else { potentialClusters.add(cluster); } } for (Cluster cluster : potentialClusters) { if (globalPotentials.containsAll(cluster.getCoreFqns()) && CollectionUtils.containsNone(globalPartials, cluster.getVersionFqns())) { library.addSecondaryCluster(cluster); } } } // Verify the cluster is novel Set<Cluster> packaging = new HashSet<>(); packaging.add(library.getCoreCluster()); packaging.addAll(library.getSecondaryClusters()); // Add the jars to the library for (Jar jar : biggest.getJars()) { boolean addMe = true; for (Cluster cluster : jarsToClusters.get(jar)) { if (cluster != biggest && !library.getSecondaryClusters().contains(cluster)) { addMe = false; break; } } if (addMe && !assignedJars.contains(jar)) { library.addJar(jar); assignedJars = assignedJars.add(jar); } } if (library.getJars().isEmpty()) { phantomCount++; } // Split the jars into versions splitLibaryIntoVersions(library); } task.progress(); task.finish(); task.report(assignedJars.size() + " jars assigned to " + repo.getLibraries().size() + " libraries, of which " + phantomCount + " are phantom libraries"); return assignedJars; }
From source file:org.eclipse.sirius.diagram.sequence.ui.tool.internal.edit.validator.PositionsChecker.java
/** * Inspect all sequence events and check that there is no conflicts. * //from w w w . j a va 2 s. com * @return a collection of invalid positions. */ public Collection<Integer> getInvalidPositions() { final Multiset<Integer> positions = HashMultiset.create(); // Check conflicts for (ISequenceEvent ise : diagram.getAllOrderedDelimitedSequenceEvents()) { Range futureRange = rangeFunction.apply(ise); int futureLowerBound = futureRange.getLowerBound(); int futureUpperBound = futureRange.getUpperBound(); if (ise instanceof Execution) { Execution exec = (Execution) ise; if (!exec.getStartMessage().some()) { positions.add(futureLowerBound); } if (!exec.getEndMessage().some()) { positions.add(futureUpperBound); } } else if (ise instanceof Operand) { positions.add(futureLowerBound); } else if (ise instanceof Message) { positions.add(futureLowerBound); if (((Message) ise).isReflective()) { positions.add(futureUpperBound); } } else if (ise instanceof State && ise.isLogicallyInstantaneous()) { positions.add(futureRange.middleValue()); } else { positions.add(futureLowerBound); positions.add(futureUpperBound); } } Set<Integer> invalidPositions = Sets.newHashSet(); Iterables.addAll(invalidPositions, Iterables.filter(positions, new Predicate<Integer>() { public boolean apply(Integer input) { int count = positions.count(input); return count != 1; } })); return invalidPositions; }
From source file:com.seajas.search.codex.service.social.SocialProfileService.java
protected void countTwitterEntities(List<Tweet> tweets, Multiset<Long> mentionCounter, Multiset<String> hashTagCounter) { for (Tweet tweet : tweets) { if (tweet.hasMentions()) { List<MentionEntity> mentions = tweet.getEntities().getMentions(); for (MentionEntity entity : mentions) { mentionCounter.add(entity.getId()); }/* w w w .j a va 2 s .c om*/ } if (tweet.hasTags()) { List<HashTagEntity> hashTags = tweet.getEntities().getHashTags(); for (HashTagEntity hashTagEntity : hashTags) { hashTagCounter.add(hashTagEntity.getText()); } } } }
From source file:bacter.model.ACGLikelihoodBeagle.java
/** * Ensure pattern counts are up to date. *///from www. j av a2s . c o m private void updatePatterns() { List<Region> regionList = acg.getRegions(locus); // Remove stale pattern sets patterns.keySet().retainAll(regionList); constantPatterns.keySet().retainAll(regionList); for (Region region : regionList) { if (patterns.containsKey(region)) continue; // Add new pattern set Multiset<int[]> patSet = LinkedHashMultiset.create(); for (int j = region.leftBoundary; j < region.rightBoundary; j++) { int[] pat = alignment.getPattern(alignment.getPatternIndex(j)); patSet.add(pat); } patterns.put(region, patSet); // Compute corresponding constant pattern list List<Integer> constantPatternList = new ArrayList<>(); int patternIdx = 0; for (int[] pattern : patSet.elementSet()) { boolean isConstant = true; for (int i = 1; i < pattern.length; i++) if (pattern[i] != pattern[0]) { isConstant = false; break; } if (isConstant) { if (alignment.getDataType().isAmbiguousCode(pattern[0])) { if (useAmbiguitiesInput.get()) { for (int state : alignment.getDataType().getStatesForCode(pattern[0])) constantPatternList.add(patternIdx * nStates + state); } } else { constantPatternList.add(patternIdx * nStates + pattern[0]); } } patternIdx += 1; } constantPatterns.put(region, constantPatternList); } }
From source file:org.sonar.plugins.core.issue.IssueCountersDecorator.java
public void decorate(Resource resource, DecoratorContext context) { Issuable issuable = perspectives.as(Issuable.class, resource); if (issuable != null) { Collection<Issue> issues = getOpenIssues(issuable.issues()); boolean shouldSaveNewMetrics = shouldSaveNewMetrics(context); Multiset<RulePriority> severitiesBag = HashMultiset.create(); Map<RulePriority, Multiset<Rule>> rulesPerSeverity = Maps.newHashMap(); ListMultimap<RulePriority, Issue> issuesPerSeverities = ArrayListMultimap.create(); int countUnassigned = 0; int falsePositives = 0; for (Issue issue : issues) { severitiesBag.add(RulePriority.valueOf(issue.severity())); Multiset<Rule> rulesBag = initRules(rulesPerSeverity, RulePriority.valueOf(issue.severity())); rulesBag.add(rulefinder.findByKey(issue.ruleKey().repository(), issue.ruleKey().rule())); issuesPerSeverities.put(RulePriority.valueOf(issue.severity()), issue); if (issue.assignee() == null) { countUnassigned++;//from www . j a v a2s .c om } if (Issue.RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) { falsePositives++; } } for (RulePriority ruleSeverity : RulePriority.values()) { saveIssuesForSeverity(context, ruleSeverity, severitiesBag); saveIssuesPerRules(context, ruleSeverity, rulesPerSeverity); saveNewIssuesForSeverity(context, ruleSeverity, issuesPerSeverities, shouldSaveNewMetrics); saveNewIssuesPerRule(context, ruleSeverity, issues, shouldSaveNewMetrics); } saveTotalIssues(context, issues); saveNewIssues(context, issues, shouldSaveNewMetrics); saveMeasure(context, CoreMetrics.UNASSIGNED_ISSUES, countUnassigned); saveMeasure(context, CoreMetrics.FALSE_POSITIVE_ISSUES, falsePositives); } }
From source file:com.google.idea.blaze.java.sync.source.SourceDirectoryCalculator.java
/** Adds the java source directories. */ private void calculateJavaSourceDirectories(BlazeContext context, WorkspaceRoot workspaceRoot, ArtifactLocationDecoder artifactLocationDecoder, WorkspacePath directoryRoot, Collection<SourceArtifact> javaArtifacts, Collection<JavaPackageReader> javaPackageReaders, Collection<BlazeSourceDirectory> result) { List<SourceRoot> sourceRootsPerFile = Lists.newArrayList(); // Get java sources List<ListenableFuture<SourceRoot>> sourceRootFutures = Lists.newArrayList(); for (final SourceArtifact sourceArtifact : javaArtifacts) { ListenableFuture<SourceRoot> future = executorService.submit(() -> sourceRootForJavaSource(context, artifactLocationDecoder, sourceArtifact, javaPackageReaders)); sourceRootFutures.add(future);//from w ww. j av a2 s. c om } try { for (SourceRoot sourceRoot : Futures.allAsList(sourceRootFutures).get()) { if (sourceRoot != null) { sourceRootsPerFile.add(sourceRoot); } } } catch (ExecutionException | InterruptedException e) { LOG.error(e); throw new IllegalStateException("Could not read sources"); } // Sort source roots into their respective directories Multimap<WorkspacePath, SourceRoot> sourceDirectoryToSourceRoots = HashMultimap.create(); for (SourceRoot sourceRoot : sourceRootsPerFile) { sourceDirectoryToSourceRoots.put(sourceRoot.workspacePath, sourceRoot); } // Create a mapping from directory to package prefix Map<WorkspacePath, SourceRoot> workspacePathToSourceRoot = Maps.newHashMap(); for (WorkspacePath workspacePath : sourceDirectoryToSourceRoots.keySet()) { Collection<SourceRoot> sources = sourceDirectoryToSourceRoots.get(workspacePath); Multiset<String> packages = HashMultiset.create(); for (SourceRoot source : sources) { packages.add(source.packagePrefix); } final String directoryPackagePrefix; // Common case -- all source files agree on a single package if (packages.elementSet().size() == 1) { directoryPackagePrefix = packages.elementSet().iterator().next(); } else { String preferredPackagePrefix = PackagePrefixCalculator.packagePrefixOf(workspacePath); directoryPackagePrefix = pickMostFrequentlyOccurring(packages, preferredPackagePrefix); } SourceRoot candidateRoot = new SourceRoot(workspacePath, directoryPackagePrefix); workspacePathToSourceRoot.put(workspacePath, candidateRoot); } // Add content entry base if it doesn't exist if (!workspacePathToSourceRoot.containsKey(directoryRoot)) { SourceRoot candidateRoot = new SourceRoot(directoryRoot, PackagePrefixCalculator.packagePrefixOf(directoryRoot)); workspacePathToSourceRoot.put(directoryRoot, candidateRoot); } // First, create a graph of the directory structure from root to each source file Map<WorkspacePath, SourceRootDirectoryNode> sourceRootDirectoryNodeMap = Maps.newHashMap(); SourceRootDirectoryNode rootNode = new SourceRootDirectoryNode(directoryRoot, null); sourceRootDirectoryNodeMap.put(directoryRoot, rootNode); for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) { final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath); List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of(); SourceRootDirectoryNode previousNode = rootNode; for (int i = 0; i < pathComponents.size(); ++i) { final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, i + 1); SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath); if (node == null) { node = new SourceRootDirectoryNode(workspacePath, pathComponents.get(i)); sourceRootDirectoryNodeMap.put(workspacePath, node); previousNode.children.add(node); } previousNode = node; } } // Add package prefix votes at each directory node for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) { final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath); List<String> packageComponents = PACKAGE_SPLITTER.splitToList(sourceRoot.packagePrefix); List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of(); int packageIndex = packageComponents.size(); int pathIndex = pathComponents.size(); while (pathIndex >= 0 && packageIndex >= 0) { final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, pathIndex); SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath); String packagePrefix = PACKAGE_JOINER.join(packageComponents.subList(0, packageIndex)); // If this is the source root containing Java files, we *have* to pick its package prefix // Otherwise just add a vote if (sourceRoot.workspacePath.equals(workspacePath)) { node.forcedPackagePrefix = packagePrefix; } else { node.packagePrefixVotes.add(packagePrefix); } String pathComponent = pathIndex > 0 ? pathComponents.get(pathIndex - 1) : ""; String packageComponent = packageIndex > 0 ? packageComponents.get(packageIndex - 1) : ""; if (!pathComponent.equals(packageComponent)) { break; } --packageIndex; --pathIndex; } } Map<WorkspacePath, SourceRoot> sourceRoots = Maps.newHashMap(); SourceRootDirectoryNode root = sourceRootDirectoryNodeMap.get(directoryRoot); visitDirectoryNode(sourceRoots, root, null); for (SourceRoot sourceRoot : sourceRoots.values()) { result.add(BlazeSourceDirectory.builder(workspaceRoot.fileForPath(sourceRoot.workspacePath)) .setPackagePrefix(sourceRoot.packagePrefix).setGenerated(false).build()); } }
From source file:org.onebusaway.nyc.vehicle_tracking.impl.simulator.SimulatorTask.java
public VehicleLocationDetails getTransitionParticleDetails(int parentParticleId, int transParticleNumber, int recordIndex) { final VehicleLocationDetails details = new VehicleLocationDetails(); details.setId(_id);//from w w w . j a v a2 s . co m final Collection<Multiset.Entry<Particle>> particles; if (recordIndex < 0) { details.setLastObservation( RecordLibrary.getNycTestInferredLocationRecordAsNycRawLocationRecord(_mostRecentRecord)); particles = _vehicleLocationInferenceService.getCurrentParticlesForVehicleId(_vehicleId).entrySet(); } else { details.setLastObservation(getDetails(recordIndex).getLastObservation()); particles = getDetails(recordIndex).getParticles(); } if (particles != null) { for (final Multiset.Entry<Particle> pEntry : particles) { final Particle p = pEntry.getElement(); if (p.getIndex() == parentParticleId) { final Multiset<Particle> history = HashMultiset.create(); history.add(Iterables.get(p.getTransitions().elementSet(), transParticleNumber)); details.setParticles(history); details.setHistory(true); break; } } } return details; }