List of usage examples for org.apache.commons.collections4 Bag add
boolean add(E object);
From source file:edu.gslis.ts.RunQuery.java
public static void main(String[] args) { try {/*w w w. ja v a 2 s .com*/ // 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(); }//from ww w . j a v a 2s . c om 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:uniol.apt.analysis.cycles.lts.AllSmallCyclesHavePVOne.java
/** * Check if a cycle exists whose Parikh vector is incomparable to (1, ..., 1). * @param ts The transition system to examine * @return true if an incomparable cycle was found, else false. *//* ww w . j ava2 s . c o m*/ static private boolean checkPhase2(TransitionSystem ts) { /* * Proof that the Parikh vector of generalised cycles is a linear combination of the Parikh vectors of * chords: * (Preconditions: total reachability, but nothing else) * * Assume a fixed spanning tree that assigns to each state s a (directed) path from the initial state to * s and also the Parikh vector P(s) of this path. Define the Parikh vector of a chord to be * P(s[t>s')=P(s)+1_t-P_{s'}. * * We will proof that the Parikh vector of a directed cycles is a linear combination of the Parikh * vectors of the chords. This is shown by proving the more general claim that this also holds for * generalised (undirected; allowing arcs to be followed backwards) cycles. Since every directed cycle * is also a generalised cycle, the claim follows. * * We proof this by induction on the number of chords contained in a cycle. * * If a cycle contains no chords, it can only follow arcs inside the spanning tree and so its Parikh * vector must be zero. * * Assume a cycle pi containing a chord so that we can write pi as pi = s [t> s' [sigma> s where sigma * is the remaining cycle. By total reachability, we can use the paths from the initial state to s and * s' to construct a new cycle that does not use this chord, but instead goes from s' backwards to the * initial state and from there goes forwards to s, both by following the arcs of the spanning tree. * This cycle is pi' = s' [sigma> s [-P_s> i [P_{s'}> s'. Note that this is a generalised cycle, * because the path leading to state s is followed backwards. Since this cycle contains one chord less, * by the induction hypothesis it can be written as a linear combination of chords. * * The Parikh vector of pi' satisfies: P(pi')=P(sigma)-P(s)+P(s'). By using P(s[t>s')=P(s)+1_t-P_{s'}, * this can be rewritten to P(pi')=P(sigma)-P(s[t>s')+1_t. * * We can now write the Parikh vector of pi as P(pi)=1_t+P(sigma)=P(pi')+P(s[t>s') which shows that * P(pi) is also a linear combination of the Parikh vectors of chords. */ /* * Proof that for any chord s[t>s' we have P(s)+1_t >= P(s'): * (Preconditions: determinism, persistence, reversibility, total reachability) * * Assume P1 holds. * * In the paper "Characterisation of the State Spaces of Marked Graph Petri Nets" (which was already * mentioned above), short paths are defined. Because it does a breath-first-search, our spanning tree * computes short paths from the initial state to all other states. Thus, the path sigma(s') to s' with * Parikh vector P(s') is a short path. The path sigma(st) that first goes to s via P(s) and then to s' * via our chord is either also a short path, or is longer than the length of a short path. * * Lemma 25 in this paper says that a path is short iff there is some event x which does not appear in * it. * * Lemma 27 in this paper says that for any path from s to s', its Parikh vector is the Parikh vector of * a short path from s to s' plus a number m added to each component of the Parikh vector uniformly. * * Thus, since sigma(s) is a short path, sigma(st) can either be a short path (if one of its entries is * null) or contains a cycle (if none of its entries is null). In the first case we have P(s)+1_t=P(s') * and in the second case we have P(s)+1_t >= P(s'). * * Thus, if we find a chord for which P(s)+1_t >= P(s') does not hold, then the transition system does * not satisfy P1, because we already checked for determinism and persistence. */ /* * Proof that for any chord, all entries must be the same: * (Preconditions: determinism, persistence, reversibility, total reachability) * * Assume P1 holds. * * Lemma 27 in the paper says that for any path from s to s', its Parikh vector is the Parikh vector of * a short path from s to s' plus a number m added to each component of the Parikh vector uniformly. * * We already know that the path to s' via the spanning tree is a short path. Lemma 27 says that the * path going to s and then following s[t>s' must have this Parikh vector plus a constant number. * Thus, their difference must be the same number in each component. * * Again, because determinism and persistence were already checked, if this does not hold, then P1 must * be violated. */ SpanningTree<TransitionSystem, Arc, State> tree = SpanningTree.get(ts, ts.getInitialState()); for (Arc chord : tree.getChords()) { State source = chord.getSource(); State target = chord.getTarget(); Bag<String> pSource = getParikhVectorReaching(tree, source); Bag<String> pTarget = getParikhVectorReaching(tree, target); pSource.add(chord.getLabel()); int expected = -1; for (String event : ts.getAlphabet()) { int val = pSource.getCount(event) - pTarget.getCount(event); if (expected == -1) // First arc: We will expect this value for other arcs expected = val; if (val < 0 || val != expected) { debugFormat( "Chord %s shows that P1 is not satisfied, because %s+%s-%s has " + "either a negative entry or not all entries have the same " + " number of occurrences", chord, tree.getEdgePathFromStart(source), chord.getLabel(), tree.getEdgePathFromStart(target)); return true; } } } /* * If we get here, then we know: * * - The Parikh vector of any cycle is a linear combination of the Parikh vectors of chords * - All chords have constant (all entries the same) Parikh vectors * - All chords have Parikh vectors >= 0 * => All cycles must have Parikh vectors of the form (m, ..., m) * => There can be no cycle with a Parikh vector incomparable to (1, ..., 1) */ return false; }
From source file:uniol.apt.analysis.cycles.lts.AllSmallCyclesHavePVOne.java
static private Bag<String> getParikhVectorReaching(SpanningTree<TransitionSystem, Arc, State> tree, State state) {//from www .j a v 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; }