Example usage for java.util HashSet isEmpty

List of usage examples for java.util HashSet isEmpty

Introduction

In this page you can find the example usage for java.util HashSet isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this set contains no elements.

Usage

From source file:org.apache.sysml.runtime.compress.CompressedMatrixBlock.java

/**
 * Compress block./*  ww w  . ja  v a2 s .  co  m*/
 * 
 * @param k  number of threads
 * @throws DMLRuntimeException if DMLRuntimeException occurs
 */
public void compress(int k) throws DMLRuntimeException {
    //check for redundant compression
    if (isCompressed()) {
        throw new DMLRuntimeException("Redundant compression, block already compressed.");
    }

    Timing time = new Timing(true);
    _stats = new CompressionStatistics();

    // SAMPLE-BASED DECISIONS:
    // Decisions such as testing if a column is amenable to bitmap
    // compression or evaluating co-coding potentionls are made based on a
    // subset of the rows. For large datasets, sampling might take a
    // significant amount of time. So, we generate only one sample and use
    // it for the entire compression process.

    //prepare basic meta data and deep copy / transpose input
    final int numRows = getNumRows();
    final int numCols = getNumColumns();
    final boolean sparse = isInSparseFormat();
    final double sp = OptimizerUtils.getSparsity(numRows, numCols, getNonZeros());
    MatrixBlock rawblock = !TRANSPOSE_INPUT ? new MatrixBlock(this)
            : LibMatrixReorg.transpose(this, new MatrixBlock(numCols, numRows, sparse), k);

    //construct sample-based size estimator
    CompressedSizeEstimator bitmapSizeEstimator = SizeEstimatorFactory.getSizeEstimator(rawblock, numRows);

    // The current implementation of this method is written for correctness,
    // not for performance or for minimal use of temporary space.

    // We start with a full set of columns.
    HashSet<Integer> remainingCols = new HashSet<Integer>();
    for (int i = 0; i < numCols; i++)
        remainingCols.add(i);

    // PHASE 1: Classify columns by compression type
    // We start by determining which columns are amenable to bitmap compression
    double uncompressedColumnSize = getUncompressedSize(numRows, 1, sp);

    // information about the bitmap amenable columns
    List<Integer> bitmapCols = new ArrayList<Integer>();
    List<Integer> uncompressedCols = new ArrayList<Integer>();
    List<Integer> colsCards = new ArrayList<Integer>();
    List<Long> compressedSizes = new ArrayList<Long>();
    HashMap<Integer, Double> compressionRatios = new HashMap<Integer, Double>();

    // Classify columns according to ration (size uncompressed / size compressed), 
    // where a column is compressible if ratio > 1.
    CompressedSizeInfo[] sizeInfos = (k > 1) ? computeCompressedSizeInfos(bitmapSizeEstimator, numCols, k)
            : computeCompressedSizeInfos(bitmapSizeEstimator, numCols);
    for (int col = 0; col < numCols; col++) {
        long compressedSize = sizeInfos[col].getMinSize();
        double compRatio = uncompressedColumnSize / compressedSize;
        if (compRatio > 1) {
            bitmapCols.add(col);
            compressionRatios.put(col, compRatio);
            colsCards.add(sizeInfos[col].getEstCarinality());
            compressedSizes.add(compressedSize);
        } else
            uncompressedCols.add(col);
    }

    _stats.timePhase1 = time.stop();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Compression statistics:");
        LOG.debug("--compression phase 1: " + _stats.timePhase1);
    }

    // PHASE 2: Grouping columns
    // Divide the bitmap columns into column groups.
    List<int[]> bitmapColGrps = PlanningCoCoder.findCocodesByPartitioning(bitmapSizeEstimator, bitmapCols,
            colsCards, compressedSizes, numRows, isInSparseFormat() ? sp : 1, k);

    _stats.timePhase2 = time.stop();
    if (LOG.isDebugEnabled())
        LOG.debug("--compression phase 2: " + _stats.timePhase2);

    if (INVESTIGATE_ESTIMATES) {
        double est = 0;
        for (int[] groupIndices : bitmapColGrps)
            est += bitmapSizeEstimator.estimateCompressedColGroupSize(groupIndices).getMinSize();
        est += uncompressedCols.size() * uncompressedColumnSize;
        _stats.estSize = est;
    }

    // PHASE 3: Compress and correct sample-based decisions
    ColGroup[] colGroups = (k > 1)
            ? compressColGroups(rawblock, bitmapSizeEstimator, compressionRatios, numRows, sp, bitmapColGrps, k)
            : compressColGroups(rawblock, bitmapSizeEstimator, compressionRatios, numRows, sp, bitmapColGrps);
    allocateColGroupList();
    for (int j = 0; j < colGroups.length; j++) {
        if (colGroups[j] != null) {
            for (int col : colGroups[j].getColIndices())
                remainingCols.remove(col);
            _colGroups.add(colGroups[j]);
        }
    }

    _stats.timePhase3 = time.stop();
    if (LOG.isDebugEnabled())
        LOG.debug("--compression phase 3: " + _stats.timePhase3);

    // Phase 4: Cleanup
    // The remaining columns are stored uncompressed as one big column group
    if (!remainingCols.isEmpty()) {
        ArrayList<Integer> list = new ArrayList<Integer>(remainingCols);
        ColGroupUncompressed ucgroup = new ColGroupUncompressed(list, rawblock);
        _colGroups.add(ucgroup);
    }

    _stats.size = estimateCompressedSizeInMemory();
    _stats.ratio = estimateSizeInMemory() / _stats.size;

    //final cleanup (discard uncompressed block)
    rawblock.cleanupBlock(true, true);
    this.cleanupBlock(true, true);

    _stats.timePhase4 = time.stop();
    if (LOG.isDebugEnabled()) {
        LOG.debug("--compression phase 4: " + _stats.timePhase4);
        LOG.debug("--num col groups: " + _colGroups.size());
        LOG.debug("--compressed size: " + _stats.size);
        LOG.debug("--compression ratio: " + _stats.ratio);
    }
}

From source file:xyz.klinker.blur.launcher3.Launcher.java

/**
 * A package was uninstalled/updated.  We take both the super set of packageNames
 * in addition to specific applications to remove, the reason being that
 * this can be called when a package is updated as well.  In that scenario,
 * we only remove specific components from the workspace and hotseat, where as
 * package-removal should clear all items by package name.
 *///from  w ww.j  a va  2  s .  c o  m
@Override
public void bindWorkspaceComponentsRemoved(final HashSet<String> packageNames,
        final HashSet<ComponentName> components, final UserHandleCompat user) {
    Runnable r = new Runnable() {
        public void run() {
            bindWorkspaceComponentsRemoved(packageNames, components, user);
        }
    };
    if (waitUntilResume(r)) {
        return;
    }
    if (!packageNames.isEmpty()) {
        mWorkspace.removeItemsByPackageName(packageNames, user);
    }
    if (!components.isEmpty()) {
        mWorkspace.removeItemsByComponentName(components, user);
    }
    // Notify the drag controller
    mDragController.onAppsRemoved(packageNames, components);

}

From source file:structuredPredictionNLG.SFX.java

/**
 * This method goes through the ActionSequence one time-step at the time, and creates a feature and cost vector for each one.
 * Meanwhile it tracks the context information that the feature vector requires.
 *///  w ww  .j  a v a2 s. c o  m
@Override
public void run() {
    String predicate = di.getMeaningRepresentation().getPredicate();
    ArrayList<Action> refSequence = di.getDirectReferenceSequence();

    //Collections to track which attribute/value pairs have already be mentioned in the sequence and which are yet to be mentioned
    HashSet<String> attrValuesAlreadyMentioned = new HashSet<>();
    HashSet<String> attrValuesToBeMentioned = new HashSet<>();
    for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) {
        for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) {
            attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
        }
    }
    if (attrValuesToBeMentioned.isEmpty()) {
        attrValuesToBeMentioned.add("empty=empty");
    }

    // First we create the feature and cost vectors for the content actions
    ArrayList<String> attributeSequence = new ArrayList<>();
    String attrValue = "";
    // For every step of the sequence
    for (int w = 0; w < refSequence.size(); w++) {
        if (!refSequence.get(w).getAttribute().equals(Action.TOKEN_PUNCT)
                && !refSequence.get(w).getAttribute().equals(attrValue)) {
            if (!attrValue.isEmpty()) {
                attrValuesToBeMentioned.remove(attrValue);
            }
            // Create the feature and cost vector
            Instance contentTrainingVector = SFX.createContentInstance(predicate,
                    refSequence.get(w).getAttribute(), attributeSequence, attrValuesAlreadyMentioned,
                    attrValuesToBeMentioned, di.getMeaningRepresentation(), SFX.getAvailableContentActions());
            if (contentTrainingVector != null) {
                predicateContentTrainingData.get(di).get(predicate).add(contentTrainingVector);
            }
            attributeSequence.add(refSequence.get(w).getAttribute());

            attrValue = refSequence.get(w).getAttribute();
            if (!attrValue.isEmpty()) {
                attrValuesAlreadyMentioned.add(attrValue);
                attrValuesToBeMentioned.remove(attrValue);
            }
        }
    }

    // Reset the tracking collections
    attrValuesAlreadyMentioned = new HashSet<>();
    attrValuesToBeMentioned = new HashSet<>();
    for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) {
        for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) {
            attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
        }
    }
    if (attrValuesToBeMentioned.isEmpty()) {
        attrValuesToBeMentioned.add("empty=empty");
    }

    // Then we create the feature and cost vectors for the word actions
    // Each word action corresponds to a content action, so we need to keep track of which content action we are "generating" from at each timestep
    ArrayList<String> attrs = new ArrayList<>();
    boolean isValueMentioned = false;
    // The value that we currently need to mention
    String valueTBM = "";
    // These track the content (attribute/value pairs)
    attrValue = "";
    // Time-step counter
    int a = -1;
    // This tracks the subphrase consisting of the words generated for the current content action
    ArrayList<String> subPhrase = new ArrayList<>();
    // For every step of the sequence
    for (int w = 0; w < refSequence.size(); w++) {
        if (!refSequence.get(w).getAttribute().equals(Action.TOKEN_PUNCT)) {
            // If this action does not belong to the current content, we need to update the trackers and switch to the new content action
            if (!refSequence.get(w).getAttribute().equals(attrValue)) {
                a++;
                if (!attrValue.isEmpty()) {
                    attrValuesToBeMentioned.remove(attrValue);
                }
                attrs.add(refSequence.get(w).getAttribute());

                attrValue = refSequence.get(w).getAttribute();
                subPhrase = new ArrayList<>();
                isValueMentioned = false;
                valueTBM = "";
                if (attrValue.contains("=")) {
                    valueTBM = attrValue.substring(attrValue.indexOf('=') + 1);
                }
                if (valueTBM.isEmpty()) {
                    isValueMentioned = true;
                }
            }

            // If it's not the end of the ActionSequence
            if (!attrValue.equals(Action.TOKEN_END)) {
                // The subsequence of content actions we have generated for so far
                ArrayList<String> predictedAttributesForInstance = new ArrayList<>();
                for (int i = 0; i < attrs.size() - 1; i++) {
                    predictedAttributesForInstance.add(attrs.get(i));
                }
                // ...exclusive of the current content action
                if (!attrs.get(attrs.size() - 1).equals(attrValue)) {
                    predictedAttributesForInstance.add(attrs.get(attrs.size() - 1));
                }
                // The subsequence of content actions we will generated for after the current content action
                ArrayList<String> nextAttributesForInstance = new ArrayList<>(
                        attributeSequence.subList(a + 1, attributeSequence.size()));
                // Create the feature and cost vector
                Instance wordTrainingVector = SFX.createWordInstance(predicate, refSequence.get(w),
                        predictedAttributesForInstance, new ArrayList<>(refSequence.subList(0, w)),
                        nextAttributesForInstance, attrValuesAlreadyMentioned, attrValuesToBeMentioned,
                        isValueMentioned, SFX.getAvailableWordActions().get(predicate));

                if (wordTrainingVector != null) {
                    String attribute = attrValue;
                    if (attribute.contains("=")) {
                        attribute = attrValue.substring(0, attrValue.indexOf('='));
                    }
                    if (!predicateWordTrainingData.get(di).containsKey(predicate)) {
                        predicateWordTrainingData.get(di).put(predicate,
                                new HashMap<String, ArrayList<Instance>>());
                    }
                    if (!predicateWordTrainingData.get(di).get(predicate).containsKey(attribute)) {
                        predicateWordTrainingData.get(di).get(predicate).put(attribute,
                                new ArrayList<Instance>());
                    }
                    predicateWordTrainingData.get(di).get(predicate).get(attribute).add(wordTrainingVector);
                    if (!refSequence.get(w).getWord().equals(Action.TOKEN_START)
                            && !refSequence.get(w).getWord().equals(Action.TOKEN_END)) {
                        subPhrase.add(refSequence.get(w).getWord());
                    }
                }

                // Check if we have mentioned the value of the current content action
                if (!isValueMentioned) {
                    // If the value is a variable, we just check if the word action we just generated is that variable
                    if (refSequence.get(w).getWord().startsWith(Action.TOKEN_X)
                            && (valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"")
                                    || valueTBM.startsWith(Action.TOKEN_X))) {
                        isValueMentioned = true;
                        // Otherwise
                    } else if (!refSequence.get(w).getWord().startsWith(Action.TOKEN_X)
                            && !(valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"")
                                    || valueTBM.startsWith(Action.TOKEN_X))) {
                        // We form the key for the value, as it appears in the valueAlignments collection
                        String valueToCheck = valueTBM;
                        if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                || valueToCheck.equals("yes or no") || valueToCheck.equals("none")
                                || valueToCheck.equals("empty")) {
                            String attribute = attrValue;
                            if (attribute.contains("=")) {
                                attribute = attrValue.substring(0, attrValue.indexOf('='));
                            }
                            valueToCheck = attribute + ":" + valueTBM;
                        }
                        // We look up the value in all the value alignments we have made during the parsing of the data, and see if it is mentioned in the subphrase
                        // Note that the value may be formed by multiple word actions
                        if (!valueToCheck.equals("empty:empty")
                                && SFX.getValueAlignments().containsKey(valueToCheck)) {
                            for (ArrayList<String> alignedStr : SFX.getValueAlignments().get(valueToCheck)
                                    .keySet()) {
                                if (SFX.endsWith(subPhrase, alignedStr)) {
                                    isValueMentioned = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (isValueMentioned) {
                        attrValuesAlreadyMentioned.add(attrValue);
                        attrValuesToBeMentioned.remove(attrValue);
                    }
                }
                // We also check if we have inadvertedly mentioned some other pending value (not the current one)
                String mentionedAttrValue = "";
                if (!refSequence.get(w).getWord().startsWith(Action.TOKEN_X)) {
                    for (String attrValueTBM : attrValuesToBeMentioned) {
                        if (attrValueTBM.contains("=")) {
                            String value = attrValueTBM.substring(attrValueTBM.indexOf('=') + 1);
                            if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                    || value.startsWith(Action.TOKEN_X))) {
                                String valueToCheck = value;
                                if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                        || valueToCheck.equals("yes or no") || valueToCheck.equals("none")
                                        || valueToCheck.equals("empty")) {
                                    valueToCheck = attrValueTBM.replace("=", ":");
                                }
                                if (!valueToCheck.equals("empty:empty")
                                        && SFX.getValueAlignments().containsKey(valueToCheck)) {
                                    for (ArrayList<String> alignedStr : SFX.getValueAlignments()
                                            .get(valueToCheck).keySet()) {
                                        if (SFX.endsWith(subPhrase, alignedStr)) {
                                            mentionedAttrValue = attrValueTBM;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (!mentionedAttrValue.isEmpty()) {
                    attrValuesAlreadyMentioned.add(mentionedAttrValue);
                    attrValuesToBeMentioned.remove(mentionedAttrValue);
                }
            }
        }
    }
}

From source file:imitationNLG.SFX.java

public HashMap<Integer, HashSet<String>> createNaiveAlignments(ArrayList<DatasetInstance> trainingData) {
    punctPatterns = new HashMap<>();
    HashMap<ArrayList<Action>, ArrayList<Action>> calculatedRealizationsCache = new HashMap<>();
    HashMap<Integer, HashSet<String>> nGrams = new HashMap<>();
    for (DatasetInstance di : trainingData) {
        HashSet<ArrayList<Action>> initRealizations = new HashSet<>();
        for (ArrayList<Action> real : di.getEvalRealizations()) {
            if (!calculatedRealizationsCache.containsKey(real)) {
                initRealizations.add(real);
            }/*from   w  ww  . ja v a 2  s  . c o  m*/
        }
        if (!calculatedRealizationsCache.containsKey(di.getTrainRealization())) {
            initRealizations.add(di.getTrainRealization());
        }
        for (ArrayList<Action> realization : initRealizations) {
            HashMap<String, HashSet<String>> values = new HashMap<>();
            for (String attr : di.getMeaningRepresentation().getAttributes().keySet()) {
                values.put(attr, new HashSet<>(di.getMeaningRepresentation().getAttributes().get(attr)));
            }

            ArrayList<Action> randomRealization = new ArrayList<Action>();
            for (Action a : realization) {
                if (a.getAttribute().equals(SFX.TOKEN_PUNCT)) {
                    randomRealization.add(new Action(a.getWord(), a.getAttribute()));
                } else {
                    randomRealization.add(new Action(a.getWord(), ""));
                }
            }

            if (values.keySet().isEmpty()) {
                for (int i = 0; i < randomRealization.size(); i++) {
                    if (randomRealization.get(i).getAttribute().isEmpty()
                            || randomRealization.get(i).getAttribute().equals("[]")) {
                        if (!attributes.get(di.getMeaningRepresentation().getPredicate()).contains("empty")) {
                            attributes.get(di.getMeaningRepresentation().getPredicate()).add("empty");
                        }
                        randomRealization.get(i).setAttribute("empty=empty");
                    }
                }
            } else {
                HashMap<Double, HashMap<String, ArrayList<Integer>>> indexAlignments = new HashMap<>();
                HashSet<String> noValueAttrs = new HashSet<String>();
                for (String attr : values.keySet()) {
                    for (String value : values.get(attr)) {
                        if ((!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                || value.startsWith(SFX.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;
                                noValueAttrs.add(attr + "=" + value);
                            }
                            if (valueToCheck.equals(attr)) {
                                noValueAttrs.add(attr + "=" + value);
                            }
                            if (!valueToCheck.equals("empty:empty")
                                    && valueAlignments.containsKey(valueToCheck)) {
                                for (ArrayList<String> align : valueAlignments.get(valueToCheck).keySet()) {
                                    int n = align.size();
                                    for (int i = 0; i <= randomRealization.size() - n; i++) {
                                        ArrayList<String> compare = new ArrayList<String>();
                                        ArrayList<Integer> indexAlignment = new ArrayList<Integer>();
                                        for (int j = 0; j < n; j++) {
                                            compare.add(randomRealization.get(i + j).getWord());
                                            indexAlignment.add(i + j);
                                        }
                                        if (compare.equals(align)) {
                                            if (!indexAlignments.containsKey(
                                                    valueAlignments.get(valueToCheck).get(align))) {
                                                indexAlignments.put(
                                                        valueAlignments.get(valueToCheck).get(align),
                                                        new HashMap());
                                            }
                                            indexAlignments.get(valueAlignments.get(valueToCheck).get(align))
                                                    .put(attr + "=" + valueToCheck, indexAlignment);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                ArrayList<Double> similarities = new ArrayList<>(indexAlignments.keySet());
                Collections.sort(similarities);
                HashSet<String> assignedAttrValues = new HashSet<String>();
                HashSet<Integer> assignedIntegers = new HashSet<Integer>();
                for (int i = similarities.size() - 1; i >= 0; i--) {
                    for (String attrValue : indexAlignments.get(similarities.get(i)).keySet()) {
                        if (!assignedAttrValues.contains(attrValue)) {
                            boolean isUnassigned = true;
                            for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) {
                                if (assignedIntegers.contains(index)) {
                                    isUnassigned = false;
                                }
                            }
                            if (isUnassigned) {
                                assignedAttrValues.add(attrValue);
                                for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) {
                                    assignedIntegers.add(index);
                                    randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim());
                                }
                            }
                        }
                    }
                }

                HashMap<String, Integer> attrXIndeces = new HashMap<>();
                for (Action a : randomRealization) {
                    if (a.getWord().startsWith(SFX.TOKEN_X)) {
                        String attr = a.getWord().substring(3, a.getWord().lastIndexOf('_')).toLowerCase()
                                .trim();
                        a.setAttribute(attr + "=" + a.getWord());
                    }
                }
                //System.out.println("-1: " + randomRealization);

                HashSet<String> unalignedNoValueAttrs = new HashSet<>();
                for (String noValueAttr : noValueAttrs) {
                    boolean assigned = false;
                    for (Action a : randomRealization) {
                        if (a.getAttribute().equals(noValueAttr)) {
                            assigned = true;
                        }
                    }
                    if (!assigned) {
                        unalignedNoValueAttrs.add(noValueAttr);
                    }
                }

                boolean isAllEmpty = true;
                boolean hasSpace = false;
                for (int i = 0; i < randomRealization.size(); i++) {
                    if (!randomRealization.get(i).getAttribute().isEmpty()
                            && !randomRealization.get(i).getAttribute().equals("[]")
                            && !randomRealization.get(i).getAttribute().equals(SFX.TOKEN_PUNCT)) {
                        isAllEmpty = false;
                    }
                    if (randomRealization.get(i).getAttribute().isEmpty()
                            || randomRealization.get(i).getAttribute().equals("[]")) {
                        hasSpace = true;
                    }
                }
                if (isAllEmpty && hasSpace && !unalignedNoValueAttrs.isEmpty()) {
                    for (String attrValue : unalignedNoValueAttrs) {
                        int index = r.nextInt(randomRealization.size());
                        boolean change = false;
                        while (!change) {
                            if (!randomRealization.get(index).getAttribute().equals(SFX.TOKEN_PUNCT)) {
                                randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim());
                                change = true;
                            } else {
                                index = r.nextInt(randomRealization.size());
                            }
                        }
                    }
                }
                //System.out.println(isAllEmpty + " " + hasSpace + " " + unalignedNoValueAttrs);
                //System.out.println(">> " + noValueAttrs);
                //System.out.println(">> " + values);
                //System.out.println("0: " + randomRealization);

                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(SFX.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(SFX.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(SFX.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(SFX.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(TOKEN_PUNCT)
                        && !randomRealization.get(i).getWord().matches("[,.?!;:']")) {
                    if (!previousAttr.isEmpty()) {
                        randomRealization.get(i).setAttribute(previousAttr);
                    }
                } else if (!randomRealization.get(i).getAttribute().equals(TOKEN_PUNCT)) {
                    previousAttr = randomRealization.get(i).getAttribute();
                }
            }
            ArrayList<Action> cleanRandomRealization = new ArrayList<>();
            for (Action a : randomRealization) {
                if (!a.getAttribute().equals(SFX.TOKEN_PUNCT)) {
                    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()) {
                    if (!a.getAttribute().equals(previousAttr)) {
                        endRandomRealization.add(new Action(SFX.TOKEN_END, previousAttr));
                    }
                }
                endRandomRealization.add(a);
                previousAttr = a.getAttribute();
            }
            endRandomRealization.add(new Action(SFX.TOKEN_END, previousAttr));
            endRandomRealization.add(new Action(SFX.TOKEN_END, SFX.TOKEN_END));
            calculatedRealizationsCache.put(realization, endRandomRealization);
            //System.out.println(di.getMeaningRepresentation().getPredicate() + ": " + endRandomRealization);

            ArrayList<String> attrValues = new ArrayList<String>();
            for (Action a : endRandomRealization) {
                if (attrValues.isEmpty()) {
                    attrValues.add(a.getAttribute());
                } else if (!attrValues.get(attrValues.size() - 1).equals(a.getAttribute())) {
                    attrValues.add(a.getAttribute());
                }
            }
            if (attrValues.size() > maxAttrRealizationSize) {
                maxAttrRealizationSize = attrValues.size();
            }

            for (int i = 0; i < randomRealization.size(); i++) {
                Action a = randomRealization.get(i);
                if (a.getAttribute().equals(SFX.TOKEN_PUNCT) && !a.getWord().equals(".")) {
                    boolean legal = true;
                    ArrayList<Action> surroundingActions = new ArrayList<>();
                    if (i - 2 >= 0) {
                        surroundingActions.add(randomRealization.get(i - 2));
                    } else {
                        surroundingActions.add(null);
                    }
                    if (i - 1 >= 0) {
                        surroundingActions.add(randomRealization.get(i - 1));
                    } else {
                        legal = false;
                    }
                    if (i + 1 < randomRealization.size()) {
                        surroundingActions.add(randomRealization.get(i + 1));
                    } else {
                        legal = false;
                    }
                    if (i + 2 < randomRealization.size()) {
                        surroundingActions.add(randomRealization.get(i + 2));
                    } else {
                        surroundingActions.add(null);
                    }
                    if (legal) {
                        punctPatterns.put(surroundingActions, a);
                    }
                }
            }
            /*for (int i = 2; i <= 6; i++) {
             for (int j = 0; j < cleanRandomRealization.size() - i; j++) {
             String ngram = "";
             for (int r = 0; r < i; r++) {
             ngram += cleanRandomRealization.get(j + r).getWord() + "|";
             }
             ngram = ngram.substring(0, ngram.length() - 1);
             if (!nGrams.containsKey(i)) {
             nGrams.put(i, new HashSet<String>());
             }
             nGrams.get(i).add(ngram);
             }
             }*/
        }

        HashSet<ArrayList<Action>> newRealizations = new HashSet<>();
        for (ArrayList<Action> real : di.getEvalRealizations()) {
            newRealizations.add(calculatedRealizationsCache.get(real));
        }
        di.setEvalRealizations(newRealizations);
        di.setTrainRealization(calculatedRealizationsCache.get(di.getTrainRealization()));
        for (ArrayList<Action> rr : di.getEvalRealizations()) {
            for (Action key : rr) {
                if (key.getWord().trim().isEmpty()) {
                    System.out.println("RR " + di.getMeaningRepresentation().getMRstr());
                    System.out.println("RR " + di.getMeaningRepresentation().getAttributes());
                    System.out.println("RR " + rr);
                    System.out.println("RR " + key);
                    System.exit(0);
                }
                if (key.getAttribute().equals("[]") || key.getAttribute().isEmpty()) {
                    System.out.println("RR " + di.getMeaningRepresentation().getMRstr());
                    System.out.println("RR " + di.getMeaningRepresentation().getAttributes());
                    System.out.println("RR " + rr);
                    System.out.println("RR " + key);
                    System.exit(0);
                }
            }
        }
        for (Action key : di.getTrainRealization()) {
            if (key.getWord().trim().isEmpty()) {
                System.out.println("RR " + di.getMeaningRepresentation().getMRstr());
                System.out.println("RR " + di.getMeaningRepresentation().getAttributes());
                System.out.println("RR " + key);
                System.exit(0);
            }
            if (key.getAttribute().equals("[]") || key.getAttribute().isEmpty()) {
                System.out.println("RR " + di.getMeaningRepresentation().getMRstr());
                System.out.println("RR " + di.getMeaningRepresentation().getAttributes());
                System.out.println("RR " + di.getTrainRealization());
                System.out.println("RR " + key);
                System.exit(0);
            }
        }
        //di.setRealizations(randomRealizations);
    }
    return nGrams;
}

From source file:org.unitime.timetable.model.Solution.java

public void createDivSecNumbers(org.hibernate.Session hibSession, Vector messages) {
    Vector assignments = new Vector(getAssignments());
    assignments.addAll(new SolutionDAO().getSession()
            .createQuery("select distinct c from Class_ c, Solution s inner join s.owner.departments d "
                    + "where s.uniqueId = :solutionId and c.managingDept=d and "
                    + "c.uniqueId not in (select a.clazz.uniqueId from s.assignments a)")
            .setLong("solutionId", getUniqueId().longValue()).list());
    HashSet relatedOfferings = new HashSet();
    for (Enumeration e = assignments.elements(); e.hasMoreElements();) {
        Object o = e.nextElement();
        Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
        Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());
        relatedOfferings.add(clazz.getSchedulingSubpart().getInstrOfferingConfig().getInstructionalOffering());
    }//w  ww . j a  v  a2s. c om
    for (Iterator i = relatedOfferings.iterator(); i.hasNext();) {
        InstructionalOffering io = (InstructionalOffering) i.next();
        for (Iterator j = io.getInstrOfferingConfigs().iterator(); j.hasNext();) {
            InstrOfferingConfig ioc = (InstrOfferingConfig) j.next();
            for (Iterator k = ioc.getSchedulingSubparts().iterator(); k.hasNext();) {
                SchedulingSubpart subpart = (SchedulingSubpart) k.next();
                for (Iterator l = subpart.getClasses().iterator(); l.hasNext();) {
                    Class_ clazz = (Class_) l.next();
                    if (clazz.getClassSuffix() != null
                            && !getOwner().getDepartments().contains(clazz.getManagingDept())) {
                        Assignment assignment = clazz.getCommittedAssignment();
                        assignments.add(assignment == null ? (Object) clazz : (Object) assignment);
                        clazz.setClassSuffix(null);
                    }
                }
            }
        }
    }
    DivSecAssignmentComparator cmp = new DivSecAssignmentComparator(this, true, false);
    Collections.sort(assignments, cmp);

    Assignment lastAssignment = null;
    SchedulingSubpart lastSubpart = null;
    Class_ lastClazz = null;
    int divNum = 1, secNum = 0;
    HashSet takenDivNums = null;

    HashSet recompute = new HashSet();

    for (Enumeration e = assignments.elements(); e.hasMoreElements();) {
        Object o = e.nextElement();
        Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
        Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());

        if (clazz.getParentClass() != null && clazz.getSchedulingSubpart().getItype()
                .equals(clazz.getParentClass().getSchedulingSubpart().getItype()))
            continue;

        if (lastSubpart == null || !lastSubpart.equals(clazz.getSchedulingSubpart())) {
            takenDivNums = takenDivisionNumbers(clazz.getSchedulingSubpart());
            lastAssignment = null;
            lastSubpart = null;
            lastClazz = null;
        }

        int nrClasses = clazz.getSchedulingSubpart().getInstrOfferingConfig().getInstructionalOffering()
                .getNrClasses(clazz.getSchedulingSubpart().getItype());

        if (lastAssignment != null && assignment != null) {
            if (nrClasses >= 100 && cmp.compareTimeLocations(lastAssignment.getClazz(), assignment.getClazz(),
                    lastAssignment.getTimeLocation(), assignment.getTimeLocation()) == 0) {
                if (lastClazz != null && clazz.getParentClass() != null
                        && !clazz.getParentClass().equals(lastClazz.getParentClass())
                        && clazz.getParentClass().getDivSecNumber() != null
                        && lastClazz.getParentClass().getDivSecNumber() != null) {
                    if (cmp.compareTimeLocations(lastAssignment.getClazz(), assignment.getClazz(),
                            lastAssignment.getTimeLocation(), assignment.getTimeLocation()) == 0
                            && clazz.getParentClass().getDivSecNumber().substring(0, 3)
                                    .equals(lastClazz.getParentClass().getDivSecNumber().substring(0, 3))) {
                        secNum++;
                    } else {
                        divNum++;
                        secNum = 1;
                        while (takenDivNums.contains(new Integer(divNum)))
                            divNum++;
                    }
                } else {
                    secNum++;
                }
            } else {
                divNum++;
                secNum = 1;
                while (takenDivNums.contains(new Integer(divNum)))
                    divNum++;
            }
        } else if (lastClazz != null) {
            divNum++;
            secNum = 1;
            while (takenDivNums.contains(new Integer(divNum)))
                divNum++;
        } else {
            divNum = 1;
            secNum = 1;
            while (takenDivNums.contains(new Integer(divNum)))
                divNum++;
        }

        if (divNum == 100 && secNum == 1) {
            sLog.warn("Division number exceeded 99 for scheduling subpart "
                    + clazz.getSchedulingSubpart().getSchedulingSubpartLabel() + ".");
            for (Iterator i = clazz.getSchedulingSubpart().getInstrOfferingConfig().getInstructionalOffering()
                    .getInstrOfferingConfigs().iterator(); i.hasNext();) {
                InstrOfferingConfig cfg = (InstrOfferingConfig) i.next();
                for (Iterator j = cfg.getSchedulingSubparts().iterator(); j.hasNext();) {
                    SchedulingSubpart subpart = (SchedulingSubpart) j.next();
                    if (subpart.getItype().equals(clazz.getSchedulingSubpart().getItype()))
                        recompute.add(subpart);
                }
            }
        }

        clazz.setClassSuffix(sSufixFormat.format(divNum) + sSufixFormat.format(secNum));
        hibSession.update(clazz);

        lastAssignment = assignment;
        lastSubpart = clazz.getSchedulingSubpart();
        lastClazz = clazz;
    }

    if (!recompute.isEmpty()) {
        HashSet recompute2 = new HashSet();
        for (Iterator i = assignments.iterator(); i.hasNext();) {
            Object o = i.next();
            Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
            Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());
            if (recompute.contains(clazz.getSchedulingSubpart())) {
                clazz.setClassSuffix(null);
                hibSession.update(clazz);
            } else {
                i.remove();
            }
        }
        cmp = new DivSecAssignmentComparator(this, false, false);
        Collections.sort(assignments, cmp);
        lastAssignment = null;
        lastSubpart = null;
        lastClazz = null;
        for (Enumeration e = assignments.elements(); e.hasMoreElements();) {
            Object o = e.nextElement();
            Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
            Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());

            if (lastSubpart == null || !lastSubpart.equals(clazz.getSchedulingSubpart())) {
                takenDivNums = takenDivisionNumbers(clazz.getSchedulingSubpart());
                lastAssignment = null;
                lastSubpart = null;
                lastClazz = null;
            }

            if (lastAssignment != null && assignment != null) {
                if (cmp.compareTimeLocations(lastAssignment.getClazz(), assignment.getClazz(),
                        lastAssignment.getTimeLocation(), assignment.getTimeLocation()) == 0) {
                    secNum++;
                } else {
                    divNum++;
                    secNum = 1;
                    while (takenDivNums.contains(new Integer(divNum)))
                        divNum++;
                }
            } else if (lastClazz != null) {
                divNum++;
                secNum = 1;
                while (takenDivNums.contains(new Integer(divNum)))
                    divNum++;
            } else {
                divNum = 1;
                secNum = 1;
                while (takenDivNums.contains(new Integer(divNum)))
                    divNum++;
            }

            if (divNum == 100 && secNum == 1) {
                sLog.warn("Division number still (fallback) exceeded 99 for scheduling subpart "
                        + clazz.getSchedulingSubpart().getSchedulingSubpartLabel() + ".");
                for (Iterator i = clazz.getSchedulingSubpart().getInstrOfferingConfig()
                        .getInstructionalOffering().getInstrOfferingConfigs().iterator(); i.hasNext();) {
                    InstrOfferingConfig cfg = (InstrOfferingConfig) i.next();
                    for (Iterator j = cfg.getSchedulingSubparts().iterator(); j.hasNext();) {
                        SchedulingSubpart subpart = (SchedulingSubpart) j.next();
                        if (subpart.getItype().equals(clazz.getSchedulingSubpart().getItype()))
                            recompute2.add(subpart);
                    }
                }
            }

            clazz.setClassSuffix(sSufixFormat.format(divNum) + sSufixFormat.format(secNum));
            hibSession.update(clazz);

            lastAssignment = assignment;
            lastSubpart = clazz.getSchedulingSubpart();
            lastClazz = clazz;
        }

        if (!recompute2.isEmpty()) {
            for (Iterator i = assignments.iterator(); i.hasNext();) {
                Object o = i.next();
                Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
                Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());
                if (recompute2.contains(clazz.getSchedulingSubpart())) {
                    clazz.setClassSuffix(null);
                    hibSession.update(clazz);
                } else {
                    i.remove();
                }
            }
            cmp = new DivSecAssignmentComparator(this, false, true);
            Collections.sort(assignments, cmp);
            lastAssignment = null;
            lastSubpart = null;
            lastClazz = null;
            for (Enumeration e = assignments.elements(); e.hasMoreElements();) {
                Object o = e.nextElement();
                Assignment assignment = (o instanceof Assignment ? (Assignment) o : null);
                Class_ clazz = (assignment == null ? (Class_) o : assignment.getClazz());

                if (lastSubpart == null
                        || cmp.compareSchedulingSubparts(lastSubpart, clazz.getSchedulingSubpart()) != 0) {
                    takenDivNums = takenDivisionNumbers(clazz.getSchedulingSubpart());
                    lastAssignment = null;
                    lastSubpart = null;
                    lastClazz = null;
                }

                if (lastAssignment != null && assignment != null) {
                    if (cmp.compareTimeLocations(lastAssignment.getClazz(), assignment.getClazz(),
                            lastAssignment.getTimeLocation(), assignment.getTimeLocation()) == 0) {
                        secNum++;
                    } else {
                        divNum++;
                        secNum = 1;
                        while (takenDivNums.contains(new Integer(divNum)))
                            divNum++;
                    }
                } else if (lastClazz != null) {
                    divNum++;
                    secNum = 1;
                    while (takenDivNums.contains(new Integer(divNum)))
                        divNum++;
                } else {
                    divNum = 1;
                    secNum = 1;
                    while (takenDivNums.contains(new Integer(divNum)))
                        divNum++;
                }

                if (divNum == 100 && secNum == 1) {
                    messages.add("Division number exceeded 99 for scheduling subpart "
                            + clazz.getSchedulingSubpart().getSchedulingSubpartLabel() + ".");
                    sLog.warn("Division number still (fallback2) exceeded 99 for scheduling subpart "
                            + clazz.getSchedulingSubpart().getSchedulingSubpartLabel() + ".");
                }

                clazz.setClassSuffix(sSufixFormat.format(divNum) + sSufixFormat.format(secNum));
                hibSession.update(clazz);

                lastAssignment = assignment;
                lastSubpart = clazz.getSchedulingSubpart();
                lastClazz = clazz;
            }
        }
    }

    /*
    lastSubpart = null;
    TreeSet otherClasses = new TreeSet(new ClassComparator(ClassComparator.COMPARE_BY_HIERARCHY));
    otherClasses.addAll(new SolutionDAO().getSession().createQuery(
        "select distinct c from Class_ c, Solution s inner join s.owner.departments d "+
        "where s.uniqueId = :solutionId and c.managingDept=d and "+
        "c.uniqueId not in (select a.clazz.uniqueId from s.assignments a) order by c.schedulingSubpart.uniqueId, c.sectionNumberCache").
        setLong("solutionId", getUniqueId().longValue()).
        list());
            
    for (Iterator i=otherClasses.iterator();i.hasNext();) {
    Class_ clazz = (Class_)i.next();
            
    if (clazz.getParentClass()!=null && clazz.getSchedulingSubpart().getItype().equals(clazz.getParentClass().getSchedulingSubpart().getItype()))
        continue;
            
    if (clazz.getClassSuffix()!=null) {
        sLog.warn("This is odd, class "+clazz.getClassLabel()+" already has a div-sec number "+clazz.getClassSuffix()+".");
        continue;
    }
            
    if (lastSubpart==null || !lastSubpart.equals(clazz.getSchedulingSubpart())) {
        takenDivNums = takenDivisionNumbers(clazz.getSchedulingSubpart());
    }
    divNum = 1;
    while (takenDivNums.contains(new Integer(divNum)))
        divNum++;
    if (divNum==100) {
        messages.add("Division number exceeded 99 for scheduling subpart "+clazz.getSchedulingSubpart().getSchedulingSubpartLabel()+".");
    }
    clazz.setClassSuffix(sSufixFormat.format(divNum)+sSufixFormat.format(1));
    takenDivNums.add(new Integer(divNum));
    lastSubpart = clazz.getSchedulingSubpart();
    hibSession.update(clazz);
    }
    */
}

From source file:g7.bluesky.launcher3.Launcher.java

/**
 * A package was uninstalled.  We take both the super set of packageNames
 * in addition to specific applications to remove, the reason being that
 * this can be called when a package is updated as well.  In that scenario,
 * we only remove specific components from the workspace, where as
 * package-removal should clear all items by package name.
 *
 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
 * Implementation of the method from LauncherModel.Callbacks.
 */// w  w w. j av a 2  s . co m
@Override
public void bindComponentsRemoved(final ArrayList<String> packageNames, final ArrayList<AppInfo> appInfos,
        final UserHandleCompat user, final int reason) {
    Runnable r = new Runnable() {
        public void run() {
            bindComponentsRemoved(packageNames, appInfos, user, reason);
        }
    };
    if (waitUntilResume(r)) {
        return;
    }

    if (reason == 0) {
        HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
        for (AppInfo info : appInfos) {
            removedComponents.add(info.componentName);
        }
        if (!packageNames.isEmpty()) {
            mWorkspace.removeItemsByPackageName(packageNames, user);
        }
        if (!removedComponents.isEmpty()) {
            mWorkspace.removeItemsByComponentName(removedComponents, user);
        }
        // Notify the drag controller
        mDragController.onAppsRemoved(packageNames, removedComponents);

    } else {
        mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
    }

    // Update AllApps
    if (!LauncherAppState.isDisableAllApps() && mAppsCustomizeContent != null) {
        mAppsCustomizeContent.removeApps(appInfos);
    }
}

From source file:structuredPredictionNLG.SFX.java

/**
 *
 * @param classifierAttrs/* w  w  w.  j  ava  2 s  .  c  om*/
 * @param classifierWords
 * @param testingData
 * @param epoch
 * @return
 */
@Override
public Double evaluateGeneration(HashMap<String, JAROW> classifierAttrs,
        HashMap<String, HashMap<String, JAROW>> classifierWords, ArrayList<DatasetInstance> testingData,
        int epoch) {
    System.out.println("Evaluate argument generation ");

    ArrayList<ScoredFeaturizedTranslation<IString, String>> generations = new ArrayList<>();
    HashMap<DatasetInstance, ArrayList<Action>> generationActions = new HashMap<>();
    ArrayList<ArrayList<Sequence<IString>>> finalReferences = new ArrayList<>();
    HashMap<DatasetInstance, ArrayList<String>> finalReferencesWordSequences = new HashMap<>();
    HashMap<DatasetInstance, String> predictedWordSequences_overAllPredicates = new HashMap<>();
    ArrayList<String> allPredictedWordSequences = new ArrayList<>();
    ArrayList<String> allPredictedMRStr = new ArrayList<>();
    ArrayList<ArrayList<String>> allPredictedReferences = new ArrayList<>();
    HashMap<String, Double> attrCoverage = new HashMap<>();

    HashMap<String, HashSet<String>> abstractMRsToMRs = new HashMap<>();

    for (DatasetInstance di : testingData) {
        String predicate = di.getMeaningRepresentation().getPredicate();
        ArrayList<Action> predictedActionList = new ArrayList<>();
        ArrayList<Action> predictedWordList = new ArrayList<>();

        //PHRASE GENERATION EVALUATION
        String predictedAttr = "";
        ArrayList<String> predictedAttrValues = new ArrayList<>();

        HashSet<String> attrValuesToBeMentioned = new HashSet<>();
        HashSet<String> attrValuesAlreadyMentioned = new HashSet<>();
        for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) {
            for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) {
                attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
            }
        }
        if (attrValuesToBeMentioned.isEmpty()) {
            attrValuesToBeMentioned.add("empty=empty");
        }
        while (!predictedAttr.equals(Action.TOKEN_END)
                && predictedAttrValues.size() < getMaxContentSequenceLength()) {
            if (!predictedAttr.isEmpty()) {
                attrValuesToBeMentioned.remove(predictedAttr);
            }
            if (!attrValuesToBeMentioned.isEmpty()) {
                Instance attrTrainingVector = createContentInstance(predicate, "@TOK@", predictedAttrValues,
                        attrValuesAlreadyMentioned, attrValuesToBeMentioned, di.getMeaningRepresentation(),
                        getAvailableContentActions());

                if (attrTrainingVector != null) {
                    Prediction predictAttr = classifierAttrs.get(predicate).predict(attrTrainingVector);
                    if (predictAttr.getLabel() != null) {
                        predictedAttr = predictAttr.getLabel().trim();

                        if (!classifierAttrs.get(predicate).getCurrentWeightVectors().keySet()
                                .containsAll(di.getMeaningRepresentation().getAttributeValues().keySet())) {
                            System.out.println("MR ATTR NOT IN CLASSIFIERS");
                            System.out
                                    .println(classifierAttrs.get(predicate).getCurrentWeightVectors().keySet());
                        }
                        String predictedValue = "";
                        if (!predictedAttr.equals(Action.TOKEN_END)) {
                            predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned);

                            HashSet<String> rejectedAttrs = new HashSet<>();
                            while (predictedValue.isEmpty() && (!predictedAttr.equals(Action.TOKEN_END)
                                    || (predictedAttrValues.isEmpty()
                                            && classifierAttrs.get(predicate).getCurrentWeightVectors().keySet()
                                                    .containsAll(di.getMeaningRepresentation()
                                                            .getAttributeValues().keySet())))) {
                                rejectedAttrs.add(predictedAttr);

                                predictedAttr = Action.TOKEN_END;
                                double maxScore = -Double.MAX_VALUE;
                                for (String attr : predictAttr.getLabel2Score().keySet()) {
                                    if (!rejectedAttrs.contains(attr) && (Double
                                            .compare(predictAttr.getLabel2Score().get(attr), maxScore) > 0)) {
                                        maxScore = predictAttr.getLabel2Score().get(attr);
                                        predictedAttr = attr;
                                    }
                                }
                                if (!predictedAttr.equals(Action.TOKEN_END)) {
                                    predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned);
                                }
                            }
                        }
                        if (!predictedAttr.equals(Action.TOKEN_END)) {
                            predictedAttr += "=" + predictedValue;
                        }
                        predictedAttrValues.add(predictedAttr);
                        if (!predictedAttr.isEmpty()) {
                            attrValuesAlreadyMentioned.add(predictedAttr);
                            attrValuesToBeMentioned.remove(predictedAttr);
                        }
                    } else {
                        predictedAttr = Action.TOKEN_END;
                        predictedAttrValues.add(predictedAttr);
                    }
                } else {
                    predictedAttr = Action.TOKEN_END;
                    predictedAttrValues.add(predictedAttr);
                }
            } else {
                predictedAttr = Action.TOKEN_END;
                predictedAttrValues.add(predictedAttr);
            }
        }

        //WORD SEQUENCE EVALUATION
        predictedAttr = "";
        ArrayList<String> predictedAttributes = new ArrayList<>();

        attrValuesToBeMentioned = new HashSet<>();
        attrValuesAlreadyMentioned = new HashSet<>();
        HashMap<String, ArrayList<String>> valuesToBeMentioned = new HashMap<>();
        for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) {
            for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) {
                attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
            }
            valuesToBeMentioned.put(attribute,
                    new ArrayList<>(di.getMeaningRepresentation().getAttributeValues().get(attribute)));
        }
        if (attrValuesToBeMentioned.isEmpty()) {
            attrValuesToBeMentioned.add("empty=empty");
        }
        HashSet<String> attrValuesToBeMentionedCopy = new HashSet<>(attrValuesToBeMentioned);

        int a = -1;
        for (String attrValue : predictedAttrValues) {
            a++;
            if (!attrValue.equals(Action.TOKEN_END)) {
                String attribute = attrValue.split("=")[0];
                predictedAttributes.add(attrValue);

                //GENERATE PHRASES
                if (!attribute.equals(Action.TOKEN_END)) {
                    if (classifierWords.get(predicate).containsKey(attribute)) {
                        ArrayList<String> nextAttributesForInstance = new ArrayList<>(
                                predictedAttrValues.subList(a + 1, predictedAttrValues.size()));
                        String predictedWord = "";

                        boolean isValueMentioned = false;
                        String valueTBM = "";
                        if (attrValue.contains("=")) {
                            valueTBM = attrValue.substring(attrValue.indexOf('=') + 1);
                        }
                        if (valueTBM.isEmpty()) {
                            isValueMentioned = true;
                        }
                        ArrayList<String> subPhrase = new ArrayList<>();
                        while (!predictedWord.equals(Action.TOKEN_END)
                                && predictedWordList.size() < getMaxWordSequenceLength()) {
                            ArrayList<String> predictedAttributesForInstance = new ArrayList<>();
                            for (int i = 0; i < predictedAttributes.size() - 1; i++) {
                                predictedAttributesForInstance.add(predictedAttributes.get(i));
                            }
                            if (!predictedAttributes.get(predictedAttributes.size() - 1).equals(attrValue)) {
                                predictedAttributesForInstance
                                        .add(predictedAttributes.get(predictedAttributes.size() - 1));
                            }
                            Instance wordTrainingVector = createWordInstance(predicate,
                                    new Action("@TOK@", attrValue), predictedAttributesForInstance,
                                    predictedActionList, nextAttributesForInstance, attrValuesAlreadyMentioned,
                                    attrValuesToBeMentioned, isValueMentioned,
                                    getAvailableWordActions().get(predicate));

                            if (wordTrainingVector != null && classifierWords.get(predicate) != null) {
                                if (classifierWords.get(predicate).get(attribute) != null) {
                                    Prediction predictWord = classifierWords.get(predicate).get(attribute)
                                            .predict(wordTrainingVector);
                                    if (predictWord.getLabel() != null) {
                                        predictedWord = predictWord.getLabel().trim();
                                        while (predictedWord.equals(Action.TOKEN_END)
                                                && !predictedActionList.isEmpty()
                                                && predictedActionList.get(predictedActionList.size() - 1)
                                                        .getWord().equals(Action.TOKEN_END)) {
                                            double maxScore = -Double.MAX_VALUE;
                                            for (String word : predictWord.getLabel2Score().keySet()) {
                                                if (!word.equals(Action.TOKEN_END) && (Double.compare(
                                                        predictWord.getLabel2Score().get(word),
                                                        maxScore) > 0)) {
                                                    maxScore = predictWord.getLabel2Score().get(word);
                                                    predictedWord = word;
                                                }
                                            }
                                        }

                                        predictedActionList.add(new Action(predictedWord, attrValue));
                                        if (!predictedWord.equals(Action.TOKEN_START)
                                                && !predictedWord.equals(Action.TOKEN_END)) {
                                            subPhrase.add(predictedWord);
                                            predictedWordList.add(new Action(predictedWord, attrValue));
                                        }
                                    } else {
                                        predictedWord = Action.TOKEN_END;
                                        predictedActionList.add(new Action(predictedWord, attrValue));
                                    }
                                } else {
                                    predictedWord = Action.TOKEN_END;
                                    predictedActionList.add(new Action(predictedWord, attrValue));
                                }

                            }
                            if (!isValueMentioned) {
                                if (!predictedWord.equals(Action.TOKEN_END)) {
                                    if (predictedWord.startsWith(Action.TOKEN_X)
                                            && (valueTBM.matches("\"[xX][0-9]+\"")
                                                    || valueTBM.matches("[xX][0-9]+")
                                                    || valueTBM.startsWith(Action.TOKEN_X))) {
                                        isValueMentioned = true;
                                    } else if (!predictedWord.startsWith(Action.TOKEN_X)
                                            && !(valueTBM.matches("\"[xX][0-9]+\"")
                                                    || valueTBM.matches("[xX][0-9]+")
                                                    || valueTBM.startsWith(Action.TOKEN_X))) {
                                        String valueToCheck = valueTBM;
                                        if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                                || valueToCheck.equals("yes or no")
                                                || valueToCheck.equals("none")
                                                //|| valueToCheck.equals("dont_care")
                                                || valueToCheck.equals("empty")) {
                                            if (attribute.contains("=")) {
                                                valueToCheck = attribute.replace("=", ":");
                                            } else {
                                                valueToCheck = attribute + ":" + valueTBM;
                                            }
                                        }
                                        if (!valueToCheck.equals("empty:empty")
                                                && getValueAlignments().containsKey(valueToCheck)) {
                                            for (ArrayList<String> alignedStr : getValueAlignments()
                                                    .get(valueToCheck).keySet()) {
                                                if (endsWith(subPhrase, alignedStr)) {
                                                    isValueMentioned = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isValueMentioned) {
                                    attrValuesAlreadyMentioned.add(attrValue);
                                    attrValuesToBeMentioned.remove(attrValue);
                                }
                            }
                            String mentionedAttrValue = "";
                            if (!predictedWord.startsWith(Action.TOKEN_X)) {
                                for (String attrValueTBM : attrValuesToBeMentioned) {
                                    if (attrValueTBM.contains("=")) {
                                        String value = attrValueTBM.substring(attrValueTBM.indexOf('=') + 1);
                                        if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                                || value.startsWith(Action.TOKEN_X))) {
                                            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 = attrValueTBM.replace("=", ":");
                                            }
                                            if (!valueToCheck.equals("empty:empty")
                                                    && getValueAlignments().containsKey(valueToCheck)) {
                                                for (ArrayList<String> alignedStr : getValueAlignments()
                                                        .get(valueToCheck).keySet()) {
                                                    if (endsWith(subPhrase, alignedStr)) {
                                                        mentionedAttrValue = attrValueTBM;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (!mentionedAttrValue.isEmpty()) {
                                attrValuesAlreadyMentioned.add(mentionedAttrValue);
                                attrValuesToBeMentioned.remove(mentionedAttrValue);
                            }
                        }
                        if (predictedWordList.size() >= getMaxWordSequenceLength() && !predictedActionList
                                .get(predictedActionList.size() - 1).getWord().equals(Action.TOKEN_END)) {
                            predictedWord = Action.TOKEN_END;
                            predictedActionList.add(new Action(predictedWord, attrValue));
                        }
                    } else {
                        String predictedWord = Action.TOKEN_END;
                        predictedActionList.add(new Action(predictedWord, attrValue));
                    }
                }
            }
        }
        ArrayList<String> predictedAttrs = new ArrayList<>();
        predictedAttrValues.forEach((attributeValuePair) -> {
            predictedAttrs.add(attributeValuePair.split("=")[0]);
        });

        String predictedWordSequence = postProcessWordSequence(di, predictedActionList);

        ArrayList<String> predictedAttrList = getPredictedAttrList(predictedActionList);
        if (attrValuesToBeMentionedCopy.size() != 0.0) {
            double missingAttrs = 0.0;
            missingAttrs = attrValuesToBeMentionedCopy.stream()
                    .filter((attr) -> (!predictedAttrList.contains(attr))).map((_item) -> 1.0)
                    .reduce(missingAttrs, (accumulator, _item) -> accumulator + _item);
            double attrSize = attrValuesToBeMentionedCopy.size();
            attrCoverage.put(predictedWordSequence, missingAttrs / attrSize);
        }

        allPredictedWordSequences.add(predictedWordSequence);
        allPredictedMRStr.add(di.getMeaningRepresentation().getMRstr());
        predictedWordSequences_overAllPredicates.put(di, predictedWordSequence);

        if (!abstractMRsToMRs.containsKey(di.getMeaningRepresentation().getAbstractMR())) {
            abstractMRsToMRs.put(di.getMeaningRepresentation().getAbstractMR(), new HashSet<String>());
        }
        abstractMRsToMRs.get(di.getMeaningRepresentation().getAbstractMR())
                .add(di.getMeaningRepresentation().getMRstr());

        Sequence<IString> translation = IStrings
                .tokenize(NISTTokenizer.tokenize(predictedWordSequence.toLowerCase()));
        ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(translation, null,
                0);
        generations.add(tran);
        generationActions.put(di, predictedActionList);

        ArrayList<Sequence<IString>> references = new ArrayList<>();
        ArrayList<String> referencesStrings = new ArrayList<>();

        if (getPerformEvaluationOn().equals("valid") || getPerformEvaluationOn().equals("train")) {
            for (String ref : di.getEvaluationReferences()) {
                referencesStrings.add(ref);
                references.add(IStrings.tokenize(NISTTokenizer.tokenize(ref)));
            }
        } else {
            references = wenEvaluationReferenceSequences.get(di.getMeaningRepresentation().getMRstr());
            referencesStrings = wenEvaluationReferences.get(di.getMeaningRepresentation().getMRstr());
            if (references == null) {
                references = new ArrayList<>();
                referencesStrings = new ArrayList<>();
                for (String ref : di.getEvaluationReferences()) {
                    referencesStrings.add(ref);
                    references.add(IStrings.tokenize(NISTTokenizer.tokenize(ref)));
                }
            }
        }
        allPredictedReferences.add(referencesStrings);
        finalReferencesWordSequences.put(di, referencesStrings);
        finalReferences.add(references);
    }

    BLEUMetric BLEU = new BLEUMetric(finalReferences, 4, false);
    Double bleuScore = BLEU.score(generations);

    double finalCoverageError = 0.0;
    finalCoverageError = attrCoverage.values().stream().map((c) -> c).reduce(finalCoverageError,
            (accumulator, _item) -> accumulator + _item);
    finalCoverageError /= attrCoverage.size();
    for (int i = 0; i < allPredictedWordSequences.size(); i++) {
        double maxRouge = 0.0;
        String predictedWordSequence = allPredictedWordSequences.get(i).replaceAll("\\?", " \\? ")
                .replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ")
                .trim();
        for (String ref : allPredictedReferences.get(i)) {
            double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4);
            if (rouge > maxRouge) {
                maxRouge = rouge;
            }
        }
        //System.out.println(allPredictedMRStr.get(i) + "\t" + maxRouge + "\t" + allPredictedWordSequences.get(i) + "\t" + refs);
    }

    double avgRougeScore = 0.0;
    String detailedRes = "";

    avgRougeScore = testingData.stream().map((di) -> {
        double maxRouge = 0.0;
        if (!finalReferencesWordSequences.containsKey(di)) {
            System.out.println(di.getMeaningRepresentation().getAbstractMR());
        }
        String predictedWordSequence = predictedWordSequences_overAllPredicates.get(di)
                .replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ")
                .replaceAll(",", " , ").replaceAll("  ", " ").trim();
        for (String ref : finalReferencesWordSequences.get(di)) {
            double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4);
            if (rouge > maxRouge) {
                maxRouge = rouge;
            }
        }
        return maxRouge;
    }).map((maxRouge) -> maxRouge).reduce(avgRougeScore, (accumulator, _item) -> accumulator + _item);
    System.out.println("BLEU: \t" + bleuScore);
    //System.out.println("g: " + generations);
    //System.out.println("attr: " + predictedAttrLists);
    //System.out.println("BLEU smooth: \t" + bleuSmoothScore);
    //System.out.println("g: " + generations);
    //System.out.println("attr: " + predictedAttrLists);
    //System.out.println("BLEU smooth: \t" + bleuSmoothScore);
    System.out.println("ROUGE: \t" + (avgRougeScore / allPredictedWordSequences.size()));
    System.out.println("COVERAGE ERROR: \t" + finalCoverageError);
    System.out.println("BRC: \t"
            + ((avgRougeScore / allPredictedWordSequences.size()) + bleuScore + (1.0 - finalCoverageError))
                    / 3.0);

    if (isCalculateResultsPerPredicate()) {
        ////////////////////////
        //ArrayList<String> bestPredictedStrings = new ArrayList<>();
        //ArrayList<String> bestPredictedStringsMRs = new ArrayList<>();
        double uniqueMRsInTestAndNotInTrainAllPredWordBLEU = 0.0;
        double uniqueMRsInTestAndNotInTrainAllPredWordROUGE = 0.0;
        double uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR = 0.0;
        double uniqueMRsInTestAndNotInTrainAllPredWordBRC = 0.0;

        detailedRes = "";
        ArrayList<DatasetInstance> abstractMRList = new ArrayList<>();
        HashSet<String> reportedAbstractMRs = new HashSet<>();
        testingData.stream()
                .filter((di) -> (!reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR())))
                .map((di) -> {
                    reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR());
                    return di;
                }).forEachOrdered((di) -> {
                    boolean isInTraining = false;
                    for (DatasetInstance di2 : getTrainingData()) {
                        if (di2.getMeaningRepresentation().getAbstractMR()
                                .equals(di.getMeaningRepresentation().getAbstractMR())) {
                            isInTraining = true;
                        }
                    }
                    if (!isInTraining) {
                        for (DatasetInstance di2 : getValidationData()) {
                            if (di2.getMeaningRepresentation().getAbstractMR()
                                    .equals(di.getMeaningRepresentation().getAbstractMR())) {
                                isInTraining = true;
                            }
                        }
                    }
                    if (!isInTraining) {
                        abstractMRList.add(di);
                    }
                });
        for (DatasetInstance di : abstractMRList) {
            Double bestROUGE = -100.0;
            Double bestBLEU = -100.0;
            Double bestCover = -100.0;
            Double bestHarmonicMean = -100.0;
            String predictedString = predictedWordSequences_overAllPredicates.get(di);
            reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR());
            double maxRouge = 0.0;
            String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                    .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();
            for (String ref : finalReferencesWordSequences.get(di)) {
                double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4);
                if (rouge > maxRouge) {
                    maxRouge = rouge;
                }
            }

            double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence,
                    finalReferencesWordSequences.get(di), 4);
            double cover = 1.0 - attrCoverage.get(predictedString);
            double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover);

            if (harmonicMean > bestHarmonicMean) {
                bestROUGE = maxRouge;
                bestBLEU = BLEUSmooth;
                bestCover = cover;
                bestHarmonicMean = harmonicMean;
            }

            uniqueMRsInTestAndNotInTrainAllPredWordBLEU += bestBLEU;
            uniqueMRsInTestAndNotInTrainAllPredWordROUGE += bestROUGE;
            uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR += bestCover;
            uniqueMRsInTestAndNotInTrainAllPredWordBRC += bestHarmonicMean;
        }
        uniqueMRsInTestAndNotInTrainAllPredWordBLEU /= abstractMRList.size();
        uniqueMRsInTestAndNotInTrainAllPredWordROUGE /= abstractMRList.size();
        uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR /= abstractMRList.size();
        uniqueMRsInTestAndNotInTrainAllPredWordBRC /= abstractMRList.size();
        System.out.println(
                "UNIQUE (NOT IN TRAIN) WORD ALL PRED BLEU: \t" + uniqueMRsInTestAndNotInTrainAllPredWordBLEU);
        System.out.println(
                "UNIQUE (NOT IN TRAIN) WORD ALL PRED ROUGE: \t" + uniqueMRsInTestAndNotInTrainAllPredWordROUGE);
        System.out.println("UNIQUE (NOT IN TRAIN) WORD ALL PRED COVERAGE ERROR: \t"
                + (1.0 - uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR));
        System.out.println(
                "UNIQUE (NOT IN TRAIN) WORD ALL PRED BRC: \t" + uniqueMRsInTestAndNotInTrainAllPredWordBRC);

        abstractMRList.forEach((di) -> {
            System.out.println(di.getMeaningRepresentation().getAbstractMR() + "\t"
                    + predictedWordSequences_overAllPredicates.get(di));
        });
        System.out.println("TOTAL SET SIZE: \t" + abstractMRList.size());
        //System.out.println(abstractMRList);  
        //System.out.println(detailedRes);
    }
    ArrayList<String> bestPredictedStrings = new ArrayList<>();
    ArrayList<String> bestPredictedStringsMRs = new ArrayList<>();
    double uniqueAllPredWordBLEU = 0.0;
    double uniqueAllPredWordROUGE = 0.0;
    double uniqueAllPredWordCOVERAGEERR = 0.0;
    double uniqueAllPredWordBRC = 0.0;

    HashSet<String> reportedAbstractMRs = new HashSet<>();
    for (DatasetInstance di : testingData) {
        if (!reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR())) {
            String bestPredictedString = "";
            Double bestROUGE = -100.0;
            Double bestBLEU = -100.0;
            Double bestCover = -100.0;
            Double bestHarmonicMean = -100.0;
            String predictedString = predictedWordSequences_overAllPredicates.get(di);
            reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR());
            double maxRouge = 0.0;
            String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                    .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();
            for (String ref : finalReferencesWordSequences.get(di)) {
                double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4);
                if (rouge > maxRouge) {
                    maxRouge = rouge;
                }
            }

            double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence,
                    finalReferencesWordSequences.get(di), 4);
            double cover = 1.0 - attrCoverage.get(predictedString);
            double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover);

            if (harmonicMean > bestHarmonicMean) {
                bestPredictedString = predictedString;
                bestROUGE = maxRouge;
                bestBLEU = BLEUSmooth;
                bestCover = cover;
                bestHarmonicMean = harmonicMean;
            }
            bestPredictedStrings.add(bestPredictedString);
            bestPredictedStringsMRs.add(di.getMeaningRepresentation().getMRstr());

            uniqueAllPredWordBLEU += bestBLEU;
            uniqueAllPredWordROUGE += bestROUGE;
            uniqueAllPredWordCOVERAGEERR += bestCover;
            uniqueAllPredWordBRC += bestHarmonicMean;
        }
        //}
    }
    if (isCalculateResultsPerPredicate()) {
        uniqueAllPredWordBLEU /= reportedAbstractMRs.size();
        uniqueAllPredWordROUGE /= reportedAbstractMRs.size();
        uniqueAllPredWordCOVERAGEERR /= reportedAbstractMRs.size();
        uniqueAllPredWordBRC /= reportedAbstractMRs.size();
        System.out.println("UNIQUE WORD ALL PRED BLEU: \t" + uniqueAllPredWordBLEU);
        System.out.println("UNIQUE WORD ALL PRED ROUGE: \t" + uniqueAllPredWordROUGE);
        System.out.println("UNIQUE WORD ALL PRED COVERAGE ERROR: \t" + (1.0 - uniqueAllPredWordCOVERAGEERR));
        System.out.println("UNIQUE WORD ALL PRED BRC: \t" + uniqueAllPredWordBRC);
        System.out.println(detailedRes);
        System.out.println("TOTAL: \t" + reportedAbstractMRs.size());

        ////////////////////////
        for (String predicate : getPredicates()) {
            detailedRes = "";
            bestPredictedStrings = new ArrayList<>();
            bestPredictedStringsMRs = new ArrayList<>();
            double uniquePredWordBLEU = 0.0;
            double uniquePredWordROUGE = 0.0;
            double uniquePredWordCOVERAGEERR = 0.0;
            double uniquePredWordBRC = 0.0;

            reportedAbstractMRs = new HashSet<>();
            for (DatasetInstance di : testingData) {
                if (di.getMeaningRepresentation().getPredicate().equals(predicate)
                        && !reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR())) {
                    String bestPredictedString = "";
                    Double bestROUGE = -100.0;
                    Double bestBLEU = -100.0;
                    Double bestCover = -100.0;
                    Double bestHarmonicMean = -100.0;

                    String predictedString = predictedWordSequences_overAllPredicates.get(di);
                    reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR());
                    double maxRouge = 0.0;
                    String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ")
                            .replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ")
                            .replaceAll("  ", " ").trim();
                    for (String ref : finalReferencesWordSequences.get(di)) {
                        double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4);
                        if (rouge > maxRouge) {
                            maxRouge = rouge;
                        }
                    }

                    double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence,
                            finalReferencesWordSequences.get(di), 4);
                    double cover = 1.0 - attrCoverage.get(predictedString);
                    double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover);

                    if (harmonicMean > bestHarmonicMean) {
                        bestPredictedString = predictedString;
                        bestROUGE = maxRouge;
                        bestBLEU = BLEUSmooth;
                        bestCover = cover;
                        bestHarmonicMean = harmonicMean;
                    }
                    bestPredictedStrings.add(bestPredictedString);
                    bestPredictedStringsMRs.add(di.getMeaningRepresentation().getMRstr());

                    uniquePredWordBLEU += bestBLEU;
                    uniquePredWordROUGE += bestROUGE;
                    uniquePredWordCOVERAGEERR += bestCover;
                    uniquePredWordBRC += bestHarmonicMean;
                }
            }

            uniquePredWordBLEU /= reportedAbstractMRs.size();
            uniquePredWordROUGE /= reportedAbstractMRs.size();
            uniquePredWordCOVERAGEERR /= reportedAbstractMRs.size();
            uniquePredWordBRC /= reportedAbstractMRs.size();
            System.out.println("UNIQUE WORD " + predicate + " BLEU: \t" + uniquePredWordBLEU);
            System.out.println("UNIQUE WORD " + predicate + " ROUGE: \t" + uniquePredWordROUGE);
            System.out.println(
                    "UNIQUE WORD " + predicate + " COVERAGE ERROR: \t" + (1.0 - uniquePredWordCOVERAGEERR));
            System.out.println("UNIQUE WORD " + predicate + " BRC: \t" + uniquePredWordBRC);
            System.out.println(detailedRes);
            System.out.println("TOTAL " + predicate + ": \t" + reportedAbstractMRs.size());
        }
    }

    if (isCalculateResultsPerPredicate()) {
        BufferedWriter bw = null;
        File f = null;
        try {
            f = new File("results/random_SFX" + getDataset() + "TextsAfter" + (epoch) + "_"
                    + JLOLS.sentenceCorrectionFurtherSteps + "_" + JLOLS.p + "epochsTESTINGDATA.txt");
        } catch (NullPointerException e) {
        }

        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
        } catch (FileNotFoundException e) {
        }

        try {
            bw.write("BLEU:" + bleuScore);
            bw.write("\n");
        } catch (IOException e) {
        }
        for (int i = 0; i < bestPredictedStrings.size(); i++) {
            try {
                String mr = bestPredictedStringsMRs.get(i);
                bw.write("MR;" + mr.replaceAll(";", ",") + ";");
                if (getDataset().equals("hotel")) {
                    bw.write("LOLS_SFHOT;");
                } else {
                    bw.write("LOLS_SFRES;");
                }

                bw.write("\n");
            } catch (IOException e) {
            }
        }

        try {
            bw.close();
        } catch (IOException e) {
        }
    }
    return bleuScore;
}

From source file:org.hyperledger.fabric.sdk.Channel.java

/**
 * Send transaction to one of a specified set of orderers with the specified user context.
 * IF there are no event hubs or eventing peers this future returns immediately completed
 * indicating that orderer has accepted the transaction only.
 *
 * @param proposalResponses/*from  w  ww. ja  v a  2s . c o m*/
 * @param transactionOptions
 * @return Future allowing access to the result of the transaction invocation.
 */

public CompletableFuture<TransactionEvent> sendTransaction(Collection<ProposalResponse> proposalResponses,
        TransactionOptions transactionOptions) {
    try {

        if (null == transactionOptions) {
            throw new InvalidArgumentException("Parameter transactionOptions can't be null");
        }
        checkChannelState();
        User userContext = transactionOptions.userContext != null ? transactionOptions.userContext
                : client.getUserContext();
        userContextCheck(userContext);
        if (null == proposalResponses) {
            throw new InvalidArgumentException("sendTransaction proposalResponses was null");
        }

        List<Orderer> orderers = transactionOptions.orderers != null ? transactionOptions.orderers
                : new ArrayList<>(getOrderers());

        // make certain we have our own copy
        final List<Orderer> shuffeledOrderers = new ArrayList<>(orderers);

        if (transactionOptions.shuffleOrders) {
            Collections.shuffle(shuffeledOrderers);
        }

        if (config.getProposalConsistencyValidation()) {
            HashSet<ProposalResponse> invalid = new HashSet<>();
            int consistencyGroups = SDKUtils.getProposalConsistencySets(proposalResponses, invalid).size();

            if (consistencyGroups != 1 || !invalid.isEmpty()) {
                throw new IllegalArgumentException(format(
                        "The proposal responses have %d inconsistent groups with %d that are invalid."
                                + " Expected all to be consistent and none to be invalid.",
                        consistencyGroups, invalid.size()));

            }

        }

        List<FabricProposalResponse.Endorsement> ed = new LinkedList<>();
        FabricProposal.Proposal proposal = null;
        ByteString proposalResponsePayload = null;
        String proposalTransactionID = null;
        TransactionContext transactionContext = null;

        for (ProposalResponse sdkProposalResponse : proposalResponses) {
            ed.add(sdkProposalResponse.getProposalResponse().getEndorsement());
            if (proposal == null) {
                proposal = sdkProposalResponse.getProposal();
                proposalTransactionID = sdkProposalResponse.getTransactionID();
                if (proposalTransactionID == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction ID");
                }
                proposalResponsePayload = sdkProposalResponse.getProposalResponse().getPayload();
                if (proposalResponsePayload == null) {
                    throw new InvalidArgumentException("Proposals with missing payload.");
                }
                transactionContext = sdkProposalResponse.getTransactionContext();
                if (transactionContext == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction context.");
                }
            } else {
                final String transactionID = sdkProposalResponse.getTransactionID();
                if (transactionID == null) {
                    throw new InvalidArgumentException("Proposals with missing transaction id.");
                }
                if (!proposalTransactionID.equals(transactionID)) {
                    throw new InvalidArgumentException(
                            format("Proposals with different transaction IDs %s,  and %s",
                                    proposalTransactionID, transactionID));
                }
            }
        }

        TransactionBuilder transactionBuilder = TransactionBuilder.newBuilder();

        Payload transactionPayload = transactionBuilder.chaincodeProposal(proposal).endorsements(ed)
                .proposalResponsePayload(proposalResponsePayload).build();

        Envelope transactionEnvelope = createTransactionEnvelope(transactionPayload, transactionContext);

        NOfEvents nOfEvents = transactionOptions.nOfEvents;

        if (nOfEvents == null) {
            nOfEvents = NOfEvents.createNofEvents();
            Collection<Peer> eventingPeers = getEventingPeers();
            boolean anyAdded = false;
            if (!eventingPeers.isEmpty()) {
                anyAdded = true;
                nOfEvents.addPeers(eventingPeers);
            }
            Collection<EventHub> eventHubs = getEventHubs();
            if (!eventHubs.isEmpty()) {
                anyAdded = true;
                nOfEvents.addEventHubs(getEventHubs());
            }

            if (!anyAdded) {
                nOfEvents = NOfEvents.createNoEvents();
            }

        } else if (nOfEvents != NOfEvents.nofNoEvents) {
            StringBuilder issues = new StringBuilder(100);
            Collection<Peer> eventingPeers = getEventingPeers();
            nOfEvents.unSeenPeers().forEach(peer -> {
                if (peer.getChannel() != this) {
                    issues.append(format("Peer %s added to NOFEvents does not belong this channel. ",
                            peer.getName()));

                } else if (!eventingPeers.contains(peer)) {
                    issues.append(format("Peer %s added to NOFEvents is not a eventing Peer in this channel. ",
                            peer.getName()));
                }

            });
            nOfEvents.unSeenEventHubs().forEach(eventHub -> {
                if (!eventHubs.contains(eventHub)) {
                    issues.append(format("Eventhub %s added to NOFEvents does not belong this channel. ",
                            eventHub.getName()));
                }

            });

            if (nOfEvents.unSeenEventHubs().isEmpty() && nOfEvents.unSeenPeers().isEmpty()) {
                issues.append("NofEvents had no Eventhubs added or Peer eventing services.");
            }
            String foundIssues = issues.toString();
            if (!foundIssues.isEmpty()) {
                throw new InvalidArgumentException(foundIssues);
            }
        }

        final boolean replyonly = nOfEvents == NOfEvents.nofNoEvents
                || (getEventHubs().isEmpty() && getEventingPeers().isEmpty());

        CompletableFuture<TransactionEvent> sret;
        if (replyonly) { //If there are no eventhubs to complete the future, complete it
            // immediately but give no transaction event
            logger.debug(format(
                    "Completing transaction id %s immediately no event hubs or peer eventing services found in channel %s.",
                    proposalTransactionID, name));
            sret = new CompletableFuture<>();
        } else {
            sret = registerTxListener(proposalTransactionID, nOfEvents, transactionOptions.failFast);
        }

        logger.debug(format("Channel %s sending transaction to orderer(s) with TxID %s ", name,
                proposalTransactionID));
        boolean success = false;
        Exception lException = null; // Save last exception to report to user .. others are just logged.

        BroadcastResponse resp = null;
        Orderer failed = null;
        for (Orderer orderer : shuffeledOrderers) {
            if (failed != null) {
                logger.warn(format("Channel %s  %s failed. Now trying %s.", name, failed, orderer));
            }
            failed = orderer;
            try {

                if (null != diagnosticFileDumper) {
                    logger.trace(format("Sending to channel %s, orderer: %s, transaction: %s", name,
                            orderer.getName(), diagnosticFileDumper
                                    .createDiagnosticProtobufFile(transactionEnvelope.toByteArray())));
                }

                resp = orderer.sendTransaction(transactionEnvelope);
                lException = null; // no longer last exception .. maybe just failed.
                if (resp.getStatus() == Status.SUCCESS) {
                    success = true;
                    break;
                } else {
                    logger.warn(format("Channel %s %s failed. Status returned %s", name, orderer,
                            getRespData(resp)));
                }
            } catch (Exception e) {
                String emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s)", name,
                        orderer.getName(), orderer.getUrl());
                if (resp != null) {

                    emsg = format("Channel %s unsuccessful sendTransaction to orderer %s (%s).  %s", name,
                            orderer.getName(), orderer.getUrl(), getRespData(resp));
                }

                logger.error(emsg);
                lException = new Exception(emsg, e);

            }

        }

        if (success) {
            logger.debug(format("Channel %s successful sent to Orderer transaction id: %s", name,
                    proposalTransactionID));
            if (replyonly) {
                sret.complete(null); // just say we're done.
            }
            return sret;
        } else {

            String emsg = format(
                    "Channel %s failed to place transaction %s on Orderer. Cause: UNSUCCESSFUL. %s", name,
                    proposalTransactionID, getRespData(resp));

            unregisterTxListener(proposalTransactionID);

            CompletableFuture<TransactionEvent> ret = new CompletableFuture<>();
            ret.completeExceptionally(
                    lException != null ? new Exception(emsg, lException) : new Exception(emsg));
            return ret;
        }
    } catch (Exception e) {

        CompletableFuture<TransactionEvent> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;

    }

}

From source file:com.fairphone.fplauncher3.Workspace.java

public void updatePackageState(ArrayList<PackageInstallInfo> installInfos) {
    HashSet<String> completedPackages = new HashSet<String>();

    for (final PackageInstallInfo installInfo : installInfos) {
        mapOverItems(MAP_RECURSE, new ItemOperator() {
            @Override/*from   ww  w  .  ja v  a 2  s . c  o  m*/
            public boolean evaluate(ItemInfo info, View v, View parent) {
                if (info instanceof ShortcutInfo && v instanceof BubbleTextView) {
                    ShortcutInfo si = (ShortcutInfo) info;
                    ComponentName cn = si.getTargetComponent();
                    if (si.isPromise() && (cn != null) && installInfo.packageName.equals(cn.getPackageName())) {
                        si.setInstallProgress(installInfo.progress);
                        if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
                            // Mark this info as broken.
                            si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
                        }
                        ((BubbleTextView) v).applyState(false);
                    }
                } else if (v instanceof PendingAppWidgetHostView && info instanceof LauncherAppWidgetInfo
                        && ((LauncherAppWidgetInfo) info).providerName.getPackageName()
                                .equals(installInfo.packageName)) {
                    ((LauncherAppWidgetInfo) info).installProgress = installInfo.progress;
                    ((PendingAppWidgetHostView) v).applyState();
                }

                // process all the shortcuts
                return false;
            }
        });

        if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
            completedPackages.add(installInfo.packageName);
        }
    }

    // Note that package states are sent only for myUser
    if (!completedPackages.isEmpty()) {
        restorePendingWidgets(completedPackages);
    }
}

From source file:structuredPredictionNLG.SFX.java

/**
 *
 * @param trainingData/*from w ww  . j ava2s .com*/
 */
@Override
public void createNaiveAlignments(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>>();

    trainingData.stream().map((di) -> {
        HashMap<ArrayList<Action>, ArrayList<Action>> calculatedRealizationsCache = new HashMap<>();
        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<>();
            for (int i = 0; i < realization.size(); i++) {
                Action a = realization.get(i);
                if (a.getAttribute().equals(Action.TOKEN_PUNCT)) {
                    randomRealization.add(new Action(a.getWord(), a.getAttribute()));
                } else {
                    randomRealization.add(new Action(a.getWord(), ""));
                }
            }
            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 {
                HashMap<Double, HashMap<String, ArrayList<Integer>>> indexAlignments = new HashMap<>();
                HashSet<String> noValueAttrs = new HashSet<String>();
                values.keySet().forEach((attr) -> {
                    values.get(attr).stream().filter(
                            (value) -> ((!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                    || value.startsWith(Action.TOKEN_X))) && !value.isEmpty()))
                            .map((value) -> {
                                String valueToCheck = value;
                                if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                        || valueToCheck.equals("yes or no") || valueToCheck.equals("none")
                                //|| attr.equals("dont_care")
                                        || valueToCheck.equals("empty")) {
                                    valueToCheck = attr + ":" + value;
                                    noValueAttrs.add(attr + "=" + value);
                                }
                                if (valueToCheck.equals(attr)) {
                                    noValueAttrs.add(attr + "=" + value);
                                }
                                return valueToCheck;
                            })
                            .filter((valueToCheck) -> (!valueToCheck.equals("empty:empty")
                                    && getValueAlignments().containsKey(valueToCheck)))
                            .forEachOrdered((valueToCheck) -> {
                                for (ArrayList<String> align : getValueAlignments().get(valueToCheck)
                                        .keySet()) {
                                    int n = align.size();
                                    for (int i = 0; i <= randomRealization.size() - n; i++) {
                                        ArrayList<String> compare = new ArrayList<String>();
                                        ArrayList<Integer> indexAlignment = new ArrayList<Integer>();
                                        for (int j = 0; j < n; j++) {
                                            compare.add(randomRealization.get(i + j).getWord());
                                            indexAlignment.add(i + j);
                                        }
                                        if (compare.equals(align)) {
                                            if (!indexAlignments.containsKey(
                                                    getValueAlignments().get(valueToCheck).get(align))) {
                                                indexAlignments.put(
                                                        getValueAlignments().get(valueToCheck).get(align),
                                                        new HashMap());
                                            }
                                            indexAlignments
                                                    .get(getValueAlignments().get(valueToCheck).get(align))
                                                    .put(attr + "=" + valueToCheck, indexAlignment);
                                        }
                                    }
                                }
                            });
                });
                ArrayList<Double> similarities = new ArrayList<>(indexAlignments.keySet());
                Collections.sort(similarities);
                HashSet<String> assignedAttrValues = new HashSet<String>();
                HashSet<Integer> assignedIntegers = new HashSet<Integer>();
                for (int i = similarities.size() - 1; i >= 0; i--) {
                    for (String attrValue : indexAlignments.get(similarities.get(i)).keySet()) {
                        if (!assignedAttrValues.contains(attrValue)) {
                            boolean isUnassigned = true;
                            for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) {
                                if (assignedIntegers.contains(index)) {
                                    isUnassigned = false;
                                }
                            }
                            if (isUnassigned) {
                                assignedAttrValues.add(attrValue);
                                for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) {
                                    assignedIntegers.add(index);
                                    randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim());
                                }
                            }
                        }
                    }
                }
                //System.out.println("-1: " + randomRealization);
                randomRealization.stream().filter((a) -> (a.getWord().startsWith(Action.TOKEN_X)))
                        .forEachOrdered((a) -> {
                            String attr = a.getWord().substring(3, a.getWord().lastIndexOf('_')).toLowerCase()
                                    .trim();
                            a.setAttribute(attr + "=" + a.getWord());
                        });
                HashSet<String> unalignedNoValueAttrs = new HashSet<>();
                noValueAttrs.forEach((noValueAttr) -> {
                    boolean assigned = false;
                    for (Action a : randomRealization) {
                        if (a.getAttribute().equals(noValueAttr)) {
                            assigned = true;
                        }
                    }
                    if (!assigned) {
                        unalignedNoValueAttrs.add(noValueAttr);
                    }
                });
                boolean isAllEmpty = true;
                boolean hasSpace = false;
                for (int i = 0; i < randomRealization.size(); i++) {
                    if (!randomRealization.get(i).getAttribute().isEmpty()
                            && !randomRealization.get(i).getAttribute().equals("[]")
                            && !randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) {
                        isAllEmpty = false;
                    }
                    if (randomRealization.get(i).getAttribute().isEmpty()
                            || randomRealization.get(i).getAttribute().equals("[]")) {
                        hasSpace = true;
                    }
                }
                if (isAllEmpty && hasSpace && !unalignedNoValueAttrs.isEmpty()) {
                    unalignedNoValueAttrs.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());
                            }
                        }
                    });
                }
                //System.out.println(isAllEmpty + " " + hasSpace + " " + unalignedNoValueAttrs);
                //System.out.println(">> " + noValueAttrs);
                //System.out.println(">> " + values);
                //System.out.println("0: " + randomRealization);
                String previousAttr = "";
                int start = -1;
                for (int i = 0; i < randomRealization.size(); i++) {
                    if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)
                            && !randomRealization.get(i).getAttribute().isEmpty()
                            && !randomRealization.get(i).getAttribute().equals("[]")) {
                        if (start != -1) {
                            int middle = (start + i - 1) / 2 + 1;
                            for (int j = start; j < middle; j++) {
                                if (randomRealization.get(j).getAttribute().isEmpty()
                                        || randomRealization.get(j).getAttribute().equals("[]")) {
                                    randomRealization.get(j).setAttribute(previousAttr);
                                }
                            }
                            for (int j = middle; j < i; j++) {
                                if (randomRealization.get(j).getAttribute().isEmpty()
                                        || randomRealization.get(j).getAttribute().equals("[]")) {
                                    randomRealization.get(j)
                                            .setAttribute(randomRealization.get(i).getAttribute());
                                }
                            }
                        }
                        start = i;
                        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 - 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);
                        }
                    }
                }
            }
        });
        di.setDirectReferenceSequence(calculatedRealizationsCache.get(di.getDirectReferenceSequence()));
        return di;
    }).forEachOrdered((di) -> {
        HashSet<String> attrValuesToBeMentioned = new HashSet<>();
        di.getMeaningRepresentation().getAttributeValues().keySet().forEach((attribute) -> {
            int a = 0;
            for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) {
                if (value.startsWith("\"x")) {
                    value = "x" + a;
                    a++;
                } else if (value.startsWith("\"")) {
                    value = value.substring(1, value.length() - 1).replaceAll(" ", "_");
                }
                attrValuesToBeMentioned.add(attribute + "=" + value);
            }
        });
        di.getDirectReferenceSequence().stream().map((key) -> {
            attrValuesToBeMentioned.remove(key.getAttribute());
            return key;
        });
    });
    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);
            }
        });
    });
}