List of usage examples for java.util HashSet stream
default Stream<E> stream()
From source file:util.AdjacencyList.java
/** * Returns all nodes in this graph.//from w ww. j a v a 2 s. c om * * @return int[] of all nodes */ public int[] getConnectedNodes() { HashSet<Integer> connected = new HashSet<>(); for (int n = 0; n < getNodeCount(); n++) { if (getDegree(n) > 0) { connected.add(n); } } for (int m = 0; m < getEdgeCount(); m++) { connected.add(getToNode(m)); } return connected.stream().mapToInt(Integer::intValue).toArray(); }
From source file:de.steilerdev.myVerein.server.model.Event.java
/** * This function updates the list of invited user of this event * @param divisionRepository An active division repository used to expand the division set *///from w ww. j a va2 s . c o m public void updateInvitedUser(DivisionRepository divisionRepository) { if (invitedDivision == null || (invitedDivision = Division.getExpandedSetOfDivisions(invitedDivision, divisionRepository)) == null) { logger.error("Unable to update invited user, because invited divisions are null!"); } else { logger.info("Updating invited user for event " + this); Set<String> oldInvitedUser = invitedUser == null ? new HashSet<>() : invitedUser.keySet(); HashSet<String> newInvitedUser = new HashSet<>(); invitedDivision.stream().forEach(div -> newInvitedUser.addAll(div.getMemberList())); if (oldInvitedUser.isEmpty() || newInvitedUser.isEmpty()) { logger.debug("Old set of invited user or new set of invited user is empty"); invitedUser = new HashMap<>(); if (oldInvitedUser.isEmpty() && !newInvitedUser.isEmpty()) { logger.debug("Old set of invited user is empty and new set of invited user is not empty"); newInvitedUser.stream().forEach(userID -> invitedUser.put(userID, EventStatus.PENDING)); } else if (newInvitedUser.isEmpty() && !oldInvitedUser.isEmpty()) { logger.debug("New set of invited user is empty and old set of invited user is not empty"); oldInvitedUser.stream().forEach(userID -> invitedUser.put(userID, EventStatus.REMOVED)); lastChanged = LocalDateTime.now(); } } else { logger.debug("Old and new set of invited user is not empty"); oldInvitedUser.removeAll(newInvitedUser); oldInvitedUser.stream().forEach(userID -> invitedUser.put(userID, EventStatus.REMOVED)); newInvitedUser.stream().forEach(userID -> invitedUser.putIfAbsent(userID, EventStatus.PENDING)); lastChanged = LocalDateTime.now(); } } }
From source file:se.trixon.jota.server.Server.java
private void intiListeners() { Runtime.getRuntime().addShutdownHook(new Thread(() -> { notifyClientsShutdown();/*from w ww.ja va2 s . co m*/ })); mOptions.getPreferences().addPreferenceChangeListener((PreferenceChangeEvent evt) -> { HashSet<ClientCallbacks> invalidClientCallbacks = new HashSet<>(); for (ClientCallbacks clientCallback : mClientCallbacks) { switch (evt.getKey()) { case ServerOptions.KEY_CRON_ACTIVE: { try { clientCallback.onServerEvent(ServerEvent.CRON_CHANGED); } catch (RemoteException ex) { //Add invalid reference for removal invalidClientCallbacks.add(clientCallback); } } break; } } invalidClientCallbacks.stream().forEach((invalidClientCallback) -> { //Remove invalid reference mClientCallbacks.remove(invalidClientCallback); }); }); }
From source file:org.apache.sysml.hops.codegen.opt.ReachabilityGraph.java
private ArrayList<Pair<CutSet, Double>> evaluateCutSets(ArrayList<ArrayList<NodeLink>> candCS, ArrayList<ArrayList<NodeLink>> remain) { ArrayList<Pair<CutSet, Double>> cutSets = new ArrayList<>(); for (ArrayList<NodeLink> cand : candCS) { HashSet<NodeLink> probe = new HashSet<>(cand); //determine subproblems for cutset candidates HashSet<NodeLink> part1 = new HashSet<>(); rCollectInputs(_root, probe, part1); HashSet<NodeLink> part2 = new HashSet<>(); for (NodeLink rNode : cand) rCollectInputs(rNode, probe, part2); //select, score and create cutsets if (!CollectionUtils.containsAny(part1, part2) && !part1.isEmpty() && !part2.isEmpty()) { //score cutsets (smaller is better) double base = UtilFunctions.pow(2, _matPoints.size()); double numComb = UtilFunctions.pow(2, cand.size()); double score = (numComb - 1) / numComb * base + 1 / numComb * UtilFunctions.pow(2, part1.size()) + 1 / numComb * UtilFunctions.pow(2, part2.size()); //construct cutset cutSets.add(Pair.of(new CutSet(cand.stream().map(p -> p._p).toArray(InterestingPoint[]::new), part1.stream().map(p -> p._p).toArray(InterestingPoint[]::new), part2.stream().map(p -> p._p).toArray(InterestingPoint[]::new)), score)); } else {/*from www . j av a 2s. c o m*/ remain.add(cand); } } return cutSets; }
From source file:pathwaynet.PathwayCalculator.java
private <T> HashMap<T, Map<T, Number>> getDistancesWithGroup(HashMap<T, Map<T, Number>> distancesMap, Collection<T> componentsInGroup, Collection<T> componentsConsidered, boolean onlyFromSource, boolean insideGroup) { // get the in-group set and out-group set of enzymes HashSet<T> componentsOutsideGroupAvailable = new HashSet<>(); componentsOutsideGroupAvailable.addAll(distancesMap.keySet()); componentsOutsideGroupAvailable.retainAll(componentsConsidered); componentsOutsideGroupAvailable.removeAll(componentsInGroup); HashSet<T> componentsInGroupAvailable = new HashSet<>(); componentsInGroupAvailable.addAll(distancesMap.keySet()); componentsInGroupAvailable.retainAll(componentsConsidered); componentsInGroupAvailable.removeAll(componentsOutsideGroupAvailable); // obtain distance HashMap<T, Map<T, Number>> distancesFromGroup = new HashMap<>(); if (insideGroup && componentsInGroupAvailable.size() < 2) System.err.println("WARNING: Fewer than TWO given components are involved in the pathway."); else if ((!insideGroup) && (componentsInGroupAvailable.isEmpty() || componentsOutsideGroupAvailable.isEmpty())) System.err.println(//from w ww.j av a 2 s . c o m "WARNING: There is either no or full overlap between the given components and the ones involving in the pathway."); else { componentsInGroupAvailable.stream().forEach((component1) -> { distancesFromGroup.put(component1, new HashMap<>()); distancesMap.keySet().stream().forEach((component2) -> { Number minDist = getShortestDistance(distancesMap, component1, component2, onlyFromSource); if (insideGroup && (componentsInGroupAvailable.contains(component2) && (!component1.equals(component2))) && minDist != null) { distancesFromGroup.get(component1).put(component2, minDist); } else if ((!insideGroup) && componentsOutsideGroupAvailable.contains(component2) && minDist != null) { distancesFromGroup.get(component1).put(component2, minDist); } }); //System.err.println(component1 + "\t" + componentsInGroupAvailable.size() +"\t" + componentsOutsideGroupAvailable.size() + "\t" + distancesFromGroup.get(component1).values()); }); } return distancesFromGroup; }
From source file:ai.grakn.graql.internal.reasoner.AtomicTest.java
@Test public void testAlphaEquivalence_DifferentRelationInequivalentVariants() { GraknGraph graph = unificationTestSet.graph(); HashSet<String> patternStrings = Sets.newHashSet("{$x isa relation1;}", "{($y) isa relation1;}", "{($x, $y);}", "{($x, $y) isa relation1;}", "{(role1: $x, role2: $y) isa relation1;}", "{(role: $y, role2: $z) isa relation1;}", "{$x ($y, $z) isa relation1;}", "{$x (role1: $y, role2: $z) isa relation1;}"); Set<Atom> atoms = patternStrings.stream().map(s -> conjunction(s, graph)) .map(p -> ReasonerQueries.atomic(p, graph).getAtom()).collect(toSet()); atoms.forEach(at -> {// w w w .ja va2 s. c om atoms.stream().filter(a -> a != at).forEach(a -> atomicEquivalence(a, at, false)); }); }
From source file:org.cgiar.ccafs.marlo.action.projects.DeliverableListAction.java
public List<Deliverable> getDeliverables(boolean open, boolean closed) { try {/* w ww.java2 s . co m*/ if (open) { if (this.isPlanningActive()) { List<Deliverable> openA = deliverables.stream() .filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == null || a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Ongoing.getStatusId()) || (a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Extended.getStatusId()) || a.getDeliverableInfo(this.getActualPhase()).getStatus() .intValue() == 0 || a.getDeliverableInfo(this.getActualPhase()).getStatus() .intValue() == -1)))) .collect(Collectors.toList()); return openA; } else { List<Deliverable> openA = deliverables.stream() .filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == null || a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Ongoing.getStatusId()) || (a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Extended.getStatusId()) || a.getDeliverableInfo(this.getActualPhase()).getStatus() .intValue() == 0)))) .collect(Collectors.toList()); openA.addAll(deliverables.stream().filter(d -> d.isActive() && d.getDeliverableInfo(this.getActualPhase()) != null && d.getDeliverableInfo(this.getActualPhase()).getYear() == this.getCurrentCycleYear() && d.getDeliverableInfo(this.getActualPhase()).getStatus() != null && d.getDeliverableInfo(this.getActualPhase()).getStatus().intValue() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())) .collect(Collectors.toList())); openA.addAll(deliverables.stream() .filter(d -> d.isActive() && d.getDeliverableInfo(this.getActualPhase()) != null && d.getDeliverableInfo(this.getActualPhase()).getNewExpectedYear() != null && d.getDeliverableInfo(this.getActualPhase()).getNewExpectedYear() .intValue() == this.getCurrentCycleYear() && d.getDeliverableInfo(this.getActualPhase()).getStatus() != null && d.getDeliverableInfo(this.getActualPhase()).getStatus().intValue() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())) .collect(Collectors.toList())); openA.sort((p1, p2) -> p1.getDeliverableInfo(this.getActualPhase()) .isRequieriedReporting(this.getCurrentCycleYear()) .compareTo(p2.getDeliverableInfo(this.getActualPhase()) .isRequieriedReporting(this.getCurrentCycleYear()))); HashSet<Deliverable> deliverables = new HashSet<>(); deliverables.addAll(openA); openA.clear(); openA.addAll(deliverables); return openA; } } else { if (this.isPlanningActive()) { List<Deliverable> openA = new ArrayList<>(); if (closed) { openA = deliverables.stream().filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Cancelled.getStatusId())))))) .collect(Collectors.toList()); } else { openA = deliverables.stream().filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())))))) .collect(Collectors.toList()); } return openA; } else { List<Deliverable> openA = new ArrayList<>(); if (closed) { openA = deliverables.stream().filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Cancelled.getStatusId())))))) .collect(Collectors.toList()); } else { openA = deliverables.stream().filter(a -> a.isActive() && a.getDeliverableInfo(this.getActualPhase()) != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() != null && ((a.getDeliverableInfo(this.getActualPhase()).getStatus() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())))))) .collect(Collectors.toList()); } openA.removeAll(deliverables.stream().filter(d -> d.isActive() && d.getDeliverableInfo(this.getActualPhase()) != null && d.getDeliverableInfo(this.getActualPhase()).getYear() == this.getCurrentCycleYear() && d.getDeliverableInfo(this.getActualPhase()).getStatus() != null && d.getDeliverableInfo(this.getActualPhase()).getStatus().intValue() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())) .collect(Collectors.toList())); openA.removeAll(deliverables.stream() .filter(d -> d.isActive() && d.getDeliverableInfo(this.getActualPhase()) != null && d.getDeliverableInfo(this.getActualPhase()).getNewExpectedYear() != null && d.getDeliverableInfo(this.getActualPhase()).getNewExpectedYear() .intValue() == this.getCurrentCycleYear() && d.getDeliverableInfo(this.getActualPhase()).getStatus() != null && d.getDeliverableInfo(this.getActualPhase()).getStatus().intValue() == Integer .parseInt(ProjectStatusEnum.Complete.getStatusId())) .collect(Collectors.toList())); openA.sort((p1, p2) -> p1.getDeliverableInfo(this.getActualPhase()) .isRequieriedReporting(this.getCurrentCycleYear()) .compareTo(p2.getDeliverableInfo(this.getActualPhase()) .isRequieriedReporting(this.getCurrentCycleYear()))); HashSet<Deliverable> deliverables = new HashSet<>(); deliverables.addAll(openA); openA.clear(); openA.addAll(deliverables); return openA; } } } catch (Exception e) { e.printStackTrace(); return new ArrayList<>(); } }
From source file:com.sonicle.webtop.contacts.ContactsManager.java
public void syncRemoteCategory(int categoryId, boolean full) throws WTException { CoreManager coreMgr = WT.getCoreManager(getTargetProfileId()); CategoryDAO catDao = CategoryDAO.getInstance(); final String PENDING_KEY = String.valueOf(categoryId); final VCardInput icalInput = new VCardInput(); Connection con = null;//from w w w . j a v a 2 s . c om if (pendingRemoteCategorySyncs.putIfAbsent(PENDING_KEY, RunContext.getRunProfileId()) != null) { throw new ConcurrentSyncException("Sync activity is already running [{}, {}]", categoryId, RunContext.getRunProfileId()); } try { //checkRightsOnCategoryFolder(categoryId, "READ"); con = WT.getConnection(SERVICE_ID, false); Category cat = ManagerUtils.createCategory(catDao.selectById(con, categoryId)); if (cat == null) throw new WTException("Category not found [{}]", categoryId); if (!Category.Provider.CARDDAV.equals(cat.getProvider())) { throw new WTException("Specified category is not remote (CardDAV) [{}]", categoryId); } // Force a full update if last-sync date is null if (cat.getRemoteSyncTimestamp() == null) full = true; if (Category.Provider.CARDDAV.equals(cat.getProvider())) { CategoryRemoteParameters params = LangUtils.deserialize(cat.getParameters(), CategoryRemoteParameters.class); if (params == null) throw new WTException("Unable to deserialize remote parameters"); if (params.url == null) throw new WTException("Remote URL is undefined"); CardDav dav = getCardDav(params.username, params.password); try { DavAddressbook dbook = dav.getAddressbookSyncToken(params.url.toString()); if (dbook == null) throw new WTException("DAV addressbook not found"); final boolean syncIsSupported = !StringUtils.isBlank(dbook.getSyncToken()); final DateTime newLastSync = DateTimeUtils.now(); if (!full && (syncIsSupported && !StringUtils.isBlank(cat.getRemoteSyncTag()))) { // Partial update using SYNC mode String newSyncToken = dbook.getSyncToken(); logger.debug("Querying CardDAV endpoint for changes [{}, {}]", params.url.toString(), cat.getRemoteSyncTag()); List<DavSyncStatus> changes = dav.getAddressbookChanges(params.url.toString(), cat.getRemoteSyncTag()); logger.debug("Returned {} items", changes.size()); try { if (!changes.isEmpty()) { ContactDAO contDao = ContactDAO.getInstance(); Map<String, List<Integer>> contactIdsByHref = contDao.selectHrefsByByCategory(con, categoryId); // Process changes... logger.debug("Processing changes..."); HashSet<String> hrefs = new HashSet<>(); for (DavSyncStatus change : changes) { String href = FilenameUtils.getName(change.getPath()); //String href = change.getPath(); if (DavUtil.HTTP_SC_TEXT_OK.equals(change.getResponseStatus())) { hrefs.add(href); } else { // Card deleted List<Integer> contactIds = contactIdsByHref.get(href); Integer contactId = (contactIds != null) ? contactIds.get(contactIds.size() - 1) : null; if (contactId == null) { logger.warn("Deletion not possible. Card path not found [{}]", PathUtils .concatPaths(dbook.getPath(), FilenameUtils.getName(href))); continue; } doContactDelete(con, contactId, false); } } // Retrieves events list from DAV endpoint (using multiget) logger.debug("Retrieving inserted/updated cards [{}]", hrefs.size()); Collection<String> paths = hrefs.stream().map( href -> PathUtils.concatPaths(dbook.getPath(), FilenameUtils.getName(href))) .collect(Collectors.toList()); List<DavAddressbookCard> dcards = dav.listAddressbookCards(params.url.toString(), paths); //List<DavAddressbookCard> dcards = dav.listAddressbookCards(params.url.toString(), hrefs); // Inserts/Updates data... logger.debug("Inserting/Updating cards..."); for (DavAddressbookCard dcard : dcards) { String href = FilenameUtils.getName(dcard.getPath()); //String href = dcard.getPath(); if (logger.isTraceEnabled()) logger.trace("{}", VCardUtils.print(dcard.getCard())); List<Integer> contactIds = contactIdsByHref.get(href); Integer contactId = (contactIds != null) ? contactIds.get(contactIds.size() - 1) : null; if (contactId != null) { doContactDelete(con, contactId, false); } final ContactInput ci = icalInput.fromVCardFile(dcard.getCard(), null); ci.contact.setCategoryId(categoryId); ci.contact.setHref(href); ci.contact.setEtag(dcard.geteTag()); doContactInsert(coreMgr, con, false, ci.contact, null, true, false); } } catDao.updateRemoteSyncById(con, categoryId, newLastSync, newSyncToken); DbUtils.commitQuietly(con); } catch (Exception ex) { DbUtils.rollbackQuietly(con); throw new WTException(ex, "Error importing vCard"); } } else { // Full update or partial computing hashes String newSyncToken = null; if (syncIsSupported) { // If supported, saves last sync-token issued by the server newSyncToken = dbook.getSyncToken(); } // Retrieves cards from DAV endpoint logger.debug("Retrieving whole list [{}]", params.url.toString()); List<DavAddressbookCard> dcards = dav.listAddressbookCards(params.url.toString()); logger.debug("Endpoint returns {} items", dcards.size()); // Handles data... try { Map<String, VContactHrefSync> syncByHref = null; if (full) { doContactsDeleteByCategory(con, categoryId, false); } else if (!full && !syncIsSupported) { // This hash-map is only needed when syncing using hashes ContactDAO contDao = ContactDAO.getInstance(); syncByHref = contDao.viewHrefSyncDataByCategory(con, categoryId); } logger.debug("Processing results..."); // Define a simple map in order to check duplicates. // eg. SOGo passes same card twice :( HashSet<String> hrefs = new HashSet<>(); for (DavAddressbookCard dcard : dcards) { String href = FilenameUtils.getName(dcard.getPath()); //String href = dcard.getPath(); String etag = dcard.geteTag(); if (logger.isTraceEnabled()) logger.trace("{}", VCardUtils.print(dcard.getCard())); if (hrefs.contains(href)) { logger.trace("Card duplicated. Skipped! [{}]", href); continue; } boolean skip = false; Integer matchingContactId = null; if (syncByHref != null) { // Only if... (!full && !syncIsSupported) see above! String prodId = VCardUtils.buildProdId(ManagerUtils.getProductName()); String hash = DigestUtils .md5Hex(new VCardOutput(prodId).write(dcard.getCard(), true)); VContactHrefSync hrefSync = syncByHref.remove(href); if (hrefSync != null) { // Href found -> maybe updated item if (!StringUtils.equals(hrefSync.getEtag(), hash)) { matchingContactId = hrefSync.getContactId(); etag = hash; logger.trace("Card updated [{}, {}]", href, hash); } else { skip = true; logger.trace("Card not modified [{}, {}]", href, hash); } } else { // Href not found -> added item logger.trace("Card newly added [{}, {}]", href, hash); etag = hash; } } if (!skip) { final ContactInput ci = icalInput.fromVCardFile(dcard.getCard(), null); ci.contact.setCategoryId(categoryId); ci.contact.setHref(href); ci.contact.setEtag(etag); if (matchingContactId == null) { doContactInsert(coreMgr, con, false, ci.contact, null, true, false); } else { ci.contact.setContactId(matchingContactId); boolean updated = doContactUpdate(coreMgr, con, false, ci.contact, true, false); if (!updated) throw new WTException("Contact not found [{}]", ci.contact.getContactId()); } } hrefs.add(href); // Marks as processed! } if (syncByHref != null) { // Only if... (!full && !syncIsSupported) see above! // Remaining hrefs -> deleted items for (VContactHrefSync hrefSync : syncByHref.values()) { logger.trace("Card deleted [{}]", hrefSync.getHref()); doContactDelete(con, hrefSync.getContactId(), false); } } catDao.updateRemoteSyncById(con, categoryId, newLastSync, newSyncToken); DbUtils.commitQuietly(con); } catch (Exception ex) { DbUtils.rollbackQuietly(con); throw new WTException(ex, "Error importing vCard"); } } } catch (DavException ex) { throw new WTException(ex, "CardDAV error"); } } else { throw new WTException("Unsupported provider [{0}]", cat.getProvider()); } } catch (SQLException | DAOException ex) { throw wrapException(ex); } finally { DbUtils.closeQuietly(con); pendingRemoteCategorySyncs.remove(PENDING_KEY); } }
From source file:structuredPredictionNLG.SFX.java
/** * * @param predicate//from ww w . j av a 2 s .c o m * @param bestAction * @param previousGeneratedAttrs * @param attrValuesAlreadyMentioned * @param attrValuesToBeMentioned * @param MR * @param availableAttributeActions * @return */ @Override public Instance createContentInstance(String predicate, String bestAction, ArrayList<String> previousGeneratedAttrs, HashSet<String> attrValuesAlreadyMentioned, HashSet<String> attrValuesToBeMentioned, MeaningRepresentation MR, HashMap<String, HashSet<String>> availableAttributeActions) { TObjectDoubleHashMap<String> costs = new TObjectDoubleHashMap<>(); HashSet<String> attrsToBeMentioned = new HashSet<>(); attrValuesToBeMentioned.stream().map((attrValue) -> { String attr = attrValue; if (attr.contains("=")) { attr = attrValue.substring(0, attrValue.indexOf('=')); } return attr; }).forEachOrdered((attr) -> { attrsToBeMentioned.add(attr); }); if (!bestAction.isEmpty()) { //COSTS if (bestAction.equals(Action.TOKEN_END)) { costs.put(Action.TOKEN_END, 0.0); availableAttributeActions.get(predicate).forEach((action) -> { costs.put(action, 1.0); }); } else if (!bestAction.equals("@TOK@")) { costs.put(Action.TOKEN_END, 1.0); availableAttributeActions.get(predicate).forEach((action) -> { String attr = bestAction; if (bestAction.contains("=")) { attr = bestAction.substring(0, bestAction.indexOf('=')).toLowerCase().trim(); } if (action.equals(attr)) { costs.put(action, 0.0); } else { costs.put(action, 1.0); } }); } } return createContentInstanceWithCosts(predicate, costs, previousGeneratedAttrs, attrValuesAlreadyMentioned, attrValuesToBeMentioned, availableAttributeActions, MR); }
From source file:structuredPredictionNLG.SFX.java
/** * * @param trainingData/*from www . jav a 2 s .com*/ */ public void createRandomAlignments(ArrayList<DatasetInstance> trainingData) { HashMap<String, HashMap<ArrayList<Action>, HashMap<Action, Integer>>> punctPatterns = new HashMap<>(); getPredicates().forEach((predicate) -> { punctPatterns.put(predicate, new HashMap<ArrayList<Action>, HashMap<Action, Integer>>()); }); HashMap<DatasetInstance, ArrayList<Action>> punctRealizations = new HashMap<DatasetInstance, ArrayList<Action>>(); HashMap<ArrayList<Action>, ArrayList<Action>> calculatedRealizationsCache = new HashMap<>(); trainingData.stream().map((di) -> { HashSet<ArrayList<Action>> initRealizations = new HashSet<>(); if (!calculatedRealizationsCache.containsKey(di.getDirectReferenceSequence())) { initRealizations.add(di.getDirectReferenceSequence()); } initRealizations.stream().map((realization) -> { HashMap<String, HashSet<String>> values = new HashMap<>(); di.getMeaningRepresentation().getAttributeValues().keySet().forEach((attr) -> { values.put(attr, new HashSet<>(di.getMeaningRepresentation().getAttributeValues().get(attr))); }); ArrayList<Action> randomRealization = new ArrayList<Action>(); realization.forEach((a) -> { if (a.getAttribute().equals(Action.TOKEN_PUNCT)) { randomRealization.add(new Action(a.getWord(), a.getAttribute())); } else { randomRealization.add(new Action(a.getWord(), "")); } }); HashSet<String> unalignedAttrs = new HashSet<>(); if (values.keySet().isEmpty()) { for (int i = 0; i < randomRealization.size(); i++) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!getAttributes().get(di.getMeaningRepresentation().getPredicate()) .contains("empty")) { getAttributes().get(di.getMeaningRepresentation().getPredicate()).add("empty"); } randomRealization.get(i).setAttribute("empty=empty"); } } } else { values.keySet().forEach((attr) -> { values.get(attr).forEach((value) -> { if ((!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+") || value.startsWith(Action.TOKEN_X))) && !value.isEmpty()) { String valueToCheck = value; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") //|| valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) { valueToCheck = attr + ":" + value; unalignedAttrs.add(attr + "=" + value); } if (valueToCheck.equals(attr)) { unalignedAttrs.add(attr + "=" + value); } if (!valueToCheck.equals("empty:empty") && getValueAlignments().containsKey(valueToCheck)) { unalignedAttrs.add(attr + "=" + valueToCheck); } } else { unalignedAttrs.add(attr + "=" + value); } }); }); unalignedAttrs.forEach((attrValue) -> { int index = getRandomGen().nextInt(randomRealization.size()); boolean change = false; while (!change) { if (!randomRealization.get(index).getAttribute().equals(Action.TOKEN_PUNCT)) { randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim()); change = true; } else { index = getRandomGen().nextInt(randomRealization.size()); } } }); String previousAttr = ""; for (int i = 0; i < randomRealization.size(); i++) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } else { previousAttr = ""; } } //System.out.println("1: " + randomRealization); previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } else { previousAttr = ""; } } //System.out.println("2: " + randomRealization); previousAttr = ""; for (int i = 0; i < randomRealization.size(); i++) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } //System.out.println("3: " + randomRealization); previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } //System.out.println("4: " + randomRealization); } //FIX WRONG @PUNCT@ String previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT) && !randomRealization.get(i).getWord().matches("[,.?!;:']")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } ArrayList<Action> cleanRandomRealization = new ArrayList<>(); randomRealization.stream().filter((a) -> (!a.getAttribute().equals(Action.TOKEN_PUNCT))) .forEachOrdered((a) -> { cleanRandomRealization.add(a); }); //ADD END TOKENS ArrayList<Action> endRandomRealization = new ArrayList<>(); previousAttr = ""; for (int i = 0; i < cleanRandomRealization.size(); i++) { Action a = cleanRandomRealization.get(i); if (!previousAttr.isEmpty() && !a.getAttribute().equals(previousAttr)) { endRandomRealization.add(new Action(Action.TOKEN_END, previousAttr)); } endRandomRealization.add(a); previousAttr = a.getAttribute(); } endRandomRealization.add(new Action(Action.TOKEN_END, previousAttr)); endRandomRealization.add(new Action(Action.TOKEN_END, Action.TOKEN_END)); calculatedRealizationsCache.put(realization, endRandomRealization); //System.out.println(di.getMeaningRepresentation().getPredicate() + ": " + endRandomRealization); ArrayList<String> attrValues = new ArrayList<String>(); endRandomRealization.forEach((a) -> { if (attrValues.isEmpty()) { attrValues.add(a.getAttribute()); } else if (!attrValues.get(attrValues.size() - 1).equals(a.getAttribute())) { attrValues.add(a.getAttribute()); } }); if (attrValues.size() > getMaxContentSequenceLength()) { setMaxContentSequenceLength(attrValues.size()); } ArrayList<Action> punctRealization = new ArrayList<>(); punctRealization.addAll(randomRealization); previousAttr = ""; for (int i = 0; i < punctRealization.size(); i++) { if (!punctRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { if (!punctRealization.get(i).getAttribute().equals(previousAttr) && !previousAttr.isEmpty()) { punctRealization.add(i, new Action(Action.TOKEN_END, previousAttr)); i++; } previousAttr = punctRealization.get(i).getAttribute(); } } if (!punctRealization.get(punctRealization.size() - 1).getWord().equals(Action.TOKEN_END)) { punctRealization.add(new Action(Action.TOKEN_END, previousAttr)); } return punctRealization; }).map((punctRealization) -> { punctRealizations.put(di, punctRealization); return punctRealization; }).forEachOrdered((punctRealization) -> { for (int i = 0; i < punctRealization.size(); i++) { Action a = punctRealization.get(i); if (a.getAttribute().equals(Action.TOKEN_PUNCT)) { boolean legal = true; ArrayList<Action> surroundingActions = new ArrayList<>(); /*if (i - 3 >= 0) { surroundingActions.add(punctRealization.get(i - 3)); } else { surroundingActions.add(null); }*/ if (i - 2 >= 0) { surroundingActions.add(punctRealization.get(i - 2)); } else { surroundingActions.add(null); } if (i - 1 >= 0) { surroundingActions.add(punctRealization.get(i - 1)); } else { legal = false; } boolean oneMore = false; if (i + 1 < punctRealization.size()) { surroundingActions.add(punctRealization.get(i + 1)); if (!punctRealization.get(i + 1).getAttribute().equals(Action.TOKEN_END)) { oneMore = true; } } else { legal = false; } if (oneMore && i + 2 < punctRealization.size()) { surroundingActions.add(punctRealization.get(i + 2)); } else { surroundingActions.add(null); } if (legal) { if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .containsKey(surroundingActions)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .put(surroundingActions, new HashMap<Action, Integer>()); } if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).containsKey(a)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).put(a, 1); } else { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions) .put(a, punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).get(a) + 1); } } } } }); return di; }).map((di) -> { di.setDirectReferenceSequence(calculatedRealizationsCache.get(di.getDirectReferenceSequence())); return di; }); punctRealizations.keySet().forEach((di) -> { ArrayList<Action> punctRealization = punctRealizations.get(di); punctPatterns.get(di.getMeaningRepresentation().getPredicate()).keySet().forEach((surrounds) -> { int beforeNulls = 0; if (surrounds.get(0) == null) { beforeNulls++; } if (surrounds.get(1) == null) { beforeNulls++; } for (int i = 0 - beforeNulls; i < punctRealization.size(); i++) { boolean matches = true; int m = 0; for (int s = 0; s < surrounds.size(); s++) { if (surrounds.get(s) != null) { if (i + s < punctRealization.size()) { if (!punctRealization.get(i + s).getWord().equals(surrounds.get(s) .getWord()) /*|| !cleanActionList.get(i).getAttribute().equals(surrounds.get(s).getAttribute())*/) { matches = false; s = surrounds.size(); } else { m++; } } else { matches = false; s = surrounds.size(); } } else if (s < 2 && i + s >= 0) { matches = false; s = surrounds.size(); } else if (s >= 2 && i + s < punctRealization.size()) { matches = false; s = surrounds.size(); } } if (matches && m > 0) { Action a = new Action("", ""); if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds) .containsKey(a)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds).put(a, 1); } else { punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds).put(a, punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds) .get(a) + 1); } } } }); }); punctPatterns.keySet().forEach((predicate) -> { punctPatterns.get(predicate).keySet().forEach((punct) -> { Action bestAction = null; int bestCount = 0; for (Action a : punctPatterns.get(predicate).get(punct).keySet()) { if (punctPatterns.get(predicate).get(punct).get(a) > bestCount) { bestAction = a; bestCount = punctPatterns.get(predicate).get(punct).get(a); } else if (punctPatterns.get(predicate).get(punct).get(a) == bestCount && bestAction.getWord().isEmpty()) { bestAction = a; } } if (!getPunctuationPatterns().containsKey(predicate)) { getPunctuationPatterns().put(predicate, new HashMap<ArrayList<Action>, Action>()); } if (!bestAction.getWord().isEmpty()) { getPunctuationPatterns().get(predicate).put(punct, bestAction); } }); }); }