List of usage examples for org.apache.commons.collections4.bag HashBag HashBag
public HashBag()
From source file:edu.gslis.ts.RunQuery.java
public static void main(String[] args) { try {// w ww . j a v a2s. co m // Get the commandline options Options options = createOptions(); CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse(options, args); String inputPath = cmd.getOptionValue("input"); String eventsPath = cmd.getOptionValue("events"); String stopPath = cmd.getOptionValue("stop"); int queryId = Integer.valueOf(cmd.getOptionValue("query")); List<String> ids = FileUtils.readLines(new File(inputPath + File.separator + "ids.txt")); Stopper stopper = new Stopper(stopPath); Map<Integer, FeatureVector> queries = readEvents(eventsPath, stopper); FeatureVector query = queries.get(queryId); Pairtree ptree = new Pairtree(); Bag<String> words = new HashBag<String>(); for (String streamId : ids) { String ppath = ptree.mapToPPath(streamId.replace("-", "")); String inpath = inputPath + File.separator + ppath + File.separator + streamId + ".xz"; // System.out.println(inpath); File infile = new File(inpath); InputStream in = new XZInputStream(new FileInputStream(infile)); TTransport inTransport = new TIOStreamTransport(new BufferedInputStream(in)); TBinaryProtocol inProtocol = new TBinaryProtocol(inTransport); inTransport.open(); final StreamItem item = new StreamItem(); while (true) { try { item.read(inProtocol); // System.out.println("Read " + item.stream_id); } catch (TTransportException tte) { // END_OF_FILE is used to indicate EOF and is not an exception. if (tte.getType() != TTransportException.END_OF_FILE) tte.printStackTrace(); break; } } // Do something with this document... String docText = item.getBody().getClean_visible(); StringTokenizer itr = new StringTokenizer(docText); while (itr.hasMoreTokens()) { words.add(itr.nextToken()); } inTransport.close(); } for (String term : words.uniqueSet()) { System.out.println(term + ":" + words.getCount(term)); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.mikenimer.familydam.services.photos.MetadataKeywordsListServlet.java
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws SlingServletException, IOException { String path = "/content/dam"; if (request.getRequestParameter("path") != null) { path = request.getRequestParameter("path").getString(); }/* w ww . j a va 2s. c o m*/ try { Bag bag = new HashBag(); String stmt = "select * from [fd:image] WHERE ISDESCENDANTNODE([" + path + "])"; Session session = request.getResourceResolver().adaptTo(Session.class); Query query = session.getWorkspace().getQueryManager().createQuery(stmt, Query.JCR_SQL2); //query.setLimit(limit); //query.setOffset(offset); QueryResult results = query.execute(); // Execute the query and get the results ... // (This is the same as before.) //javax.jcr.QueryResult result = query.execute(); NodeIterator nodeIterator = results.getNodes(); while (nodeIterator.hasNext()) { Node n = nodeIterator.nextNode(); if (n.hasNode("metadata")) { Node metaNode = n.getNode("metadata"); if (metaNode.hasProperty("keywords")) { String keywords = metaNode.getProperty("keywords").getString(); String[] keys = keywords.split(","); for (String key : keys) { bag.add(key); } } } } Set set = bag.uniqueSet(); // find scale ratio, we need a 1-12 range final JSONWriter w = new JSONWriter(response.getWriter()); w.setTidy(true); w.array(); for (Object word : set) { w.object(); w.key("word").value(word.toString().toLowerCase()); w.key("count").value(bag.getCount(word)); w.key("size").value(Math.max(1.5, Math.min(5, bag.getCount(word) * .05)) + "rem"); // todo try this. // $size = min(max(round(( $size_max*($count-$count_min))/($count_max-$count_min),2), $size_min),$size_max); w.endObject(); } w.endArray(); //writeJsonObject(w, tree); } catch (Exception re) { re.printStackTrace(); throw new SlingServletException(new javax.servlet.ServletException(re)); } }
From source file:de.tudarmstadt.ukp.dkpro.core.frequency.phrasedetection.FrequencyCounter.java
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); if (sortByAlphabet && sortByCount) { throw new ResourceInitializationException( new IllegalArgumentException("Can only sort either by count or alphabetically.")); }/*from ww w . j a v a 2s . c o m*/ unigrams = new HashBag<>(); bigrams = new HashBag<>(); /* set feature path to default */ if (featurePath == null) { featurePath = DEFAULT_FEATURE_PATH; } /* init sequence generator */ try { sequenceGenerator = new PhraseSequenceGenerator.Builder().featurePath(featurePath) .coveringType(coveringType).lowercase(lowercase).stopwordsFile(stopwordsFile) .stopwordsReplacement(stopwordsReplacement).filterRegex(filterRegex) .filterRegexReplacement(regexReplacement).buildStringSequenceGenerator(); } catch (IOException e) { throw new ResourceInitializationException(e); } }
From source file:de.tudarmstadt.ukp.dkpro.core.io.ditop.DiTopWriter.java
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); try {//w w w . j a v a2 s.co m model = ParallelTopicModel.read(modelLocation); collectionDir = new File(targetLocation, corpusName + "_" + model.getNumTopics()); if (collectionDir.exists()) { getLogger().warn(String.format("%s' already exists, overwriting content.", collectionDir)); } collectionDir.mkdirs(); initializeTopicFile(); } catch (Exception e) { throw new ResourceInitializationException(e); } collectionValuesSet = collectionValues == null ? Collections.<String>emptySet() : new HashSet<>(Arrays.asList(collectionValues)); collectionCounter = new HashBag<>(); }
From source file:org.michaelevans.colorart.library.ColorArt.java
private int findEdgeColor() { int height = mBitmap.getHeight(); int width = mBitmap.getWidth(); mImageColors = new HashBag(); HashBag leftImageColors = new HashBag(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (x == 0) { leftImageColors.add(Integer.valueOf(mBitmap.getPixel(x, y))); }//from w w w .j a v a 2 s . c om mImageColors.add(Integer.valueOf(mBitmap.getPixel(x, y))); } } ArrayList<CountedColor> sortedColors = new ArrayList<CountedColor>(); int randomColorThreshold = (int) (height * COLOR_THRESHOLD_MINIMUM_PERCENTAGE); Iterator<Integer> iterator = leftImageColors.iterator(); while (iterator.hasNext()) { Integer color = iterator.next(); int colorCount = leftImageColors.getCount(color); if (colorCount < randomColorThreshold) { continue; } CountedColor container = new CountedColor(color, colorCount); sortedColors.add(container); } Collections.sort(sortedColors); CountedColor proposedEdgeColor = null; Iterator<CountedColor> sortedColorIterator = sortedColors.iterator(); if (!sortedColorIterator.hasNext()) { return Color.BLACK; } proposedEdgeColor = sortedColorIterator.next(); if (!proposedEdgeColor.isBlackOrWhite()) { return proposedEdgeColor.getColor(); } while (sortedColorIterator.hasNext()) { CountedColor nextProposedColor = sortedColorIterator.next(); double edgeColorRatio = (double) nextProposedColor.getCount() / proposedEdgeColor.getCount(); if (edgeColorRatio <= EDGE_COLOR_DISCARD_THRESHOLD) { break; } if (!nextProposedColor.isBlackOrWhite()) { proposedEdgeColor = nextProposedColor; break; } } return proposedEdgeColor.getColor(); }
From source file:pltag.parser.semantics.classifier.IncrementalSemanticsWidget.java
private IncrementalSemanticsWidget(Map<Predicate, Proposition> propositions, int timestamp, boolean fullSentence) { this.propositions = propositions; predicatesWithSense = new HashSet<Predicate>(); predicates = new HashSet<String>(); argWords = new HashBag<String>(); argRoles = new HashSet<Pair<Integer, Argument>>(); incompleteArcs = new HashBag<String>(); this.timestamp = timestamp; this.fullSentence = fullSentence; }
From source file:uniol.apt.analysis.cycles.lts.AllSmallCyclesHavePVOne.java
static private Bag<String> getParikhVectorReaching(SpanningTree<TransitionSystem, Arc, State> tree, State state) {// w w w . j av a 2s . c om Bag<String> result = new HashBag<>(); while (!state.equals(tree.getStartNode())) { Arc arc = tree.getPredecessorEdge(state); result.add(arc.getLabel()); state = arc.getSource(); } return result; }
From source file:uniol.apt.analysis.synthesize.separation.KBoundedSeparation.java
private Bag<State> expand(Bag<State> input, Event event, int g, boolean forward) { TransitionSystem ts = utility.getTransitionSystem(); Bag<State> result = new HashBag<State>(); for (State state : ts.getNodes()) { int increment = 0; for (Arc arc : forward ? state.getPostsetEdges() : state.getPresetEdges()) { if (arc.getEvent().equals(event)) { int value = getGradient(input, arc) - g; if (!forward) value = -value;/*from www . jav a2s. c o m*/ if (value > increment) increment = value; } } result.add(state, input.getCount(state) + increment); } return result; }