/** * Removes all pairs that contain the given single value * @param key * @return */ public boolean removePairsContaining(K key) { Set<Pair<K>> pairs = mapKeyToPairContainingIt.remove(key); boolean ret = pairsSet.removeAll(pairs); // search and remove all the pairs that contain this key and are mapped by other keys. Set<K> keysToRemove = new LinkedHashSet<K>(); for (Entry<K, Set<Pair<K>>> entry : mapKeyToPairContainingIt.entrySet()) { Set<Pair<K>> pairs2 = entry.getValue(); Set<Pair<K>> pairsContainingTheKey = new HashSet<Pair<K>>(); for (Pair<K> pair : pairs2) if (pair.contains(key)) pairsContainingTheKey.add(pair); pairs2.removeAll(pairsContainingTheKey); // if the key to this pair is mapped to no other pairs, remove this key too if (pairs2.isEmpty()) keysToRemove.add(entry.getKey()); } for (K keyToRemove : keysToRemove) mapKeyToPairContainingIt.remove(keyToRemove); return ret; }
/** * Identical to <code>java.util.set.put()</code> method. * @param pair * @param value */ public void put(Pair<K> pair) { pairsSet.add(pair); for (K single : pair.toSet()) { if (!mapKeyToPairContainingIt.containsKey(single)) { mapKeyToPairContainingIt.put(single, new LinkedHashSet<Pair<K>>()); } Set<Pair<K>> itsSetOfPairsContainingIt = mapKeyToPairContainingIt.get(single); itsSetOfPairsContainingIt.add(pair); } }
/** * parse the undirected edges from the CGX doc * * @return * @throws CompilationException */ public PairSet<Long> parseEdges() throws CompilationException { if (nodesMap == null) nodesMap = parseNodes(); PairSet<Long> undirectedEdges = new PairSet<Long>(); NodeList nodeLst = doc.getElementsByTagName("coref"); for (int i = 0; i < nodeLst.getLength(); i++) { // get a coref node Node fstNode = nodeLst.item(i); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; Long id1 = Long.parseLong( fstElmnt.getAttribute("from") ); Long id2 = Long.parseLong( fstElmnt.getAttribute("to") ); if (id1 == null || id2 == null) throw new CompilationException("There is a coref line that doesn't connect two Concepts"); undirectedEdges.put(new Pair<Long>(id1, id2)); } } return undirectedEdges; }
Pair<String> verbPair = new Pair<String>(leftVerb, rightVerb);
/** * @param originId * @param undirectedEdges * @return */ public static List<Long> getNeighboringIds(Long originId, PairSet<Long> undirectedEdges) { List<Long> neighborIds = new Vector<Long>(); ImmutableSet<Pair<Long>> edgesFromOriginId = undirectedEdges.getPairContaining(originId); if (edgesFromOriginId != null) for (Pair<Long> edge : edgesFromOriginId) { Long[] ids = edge.toSet().toArray(new Long[2]); neighborIds.add(ids[0].equals(originId) ? ids[1] : ids[0]); // add the id on the other side of the edge } return neighborIds; }
for (Pair<Long> edge : undirectedEdges.getPairContaining(rootId)) Long[] sides = edge.toSet().toArray(new Long[2]); Long childId = sides[0].equals(rootId) ? sides[1] : sides[0]; childrenIds.add(childId);