Example usage for org.apache.commons.collections4.map MultiValueMap getCollection

List of usage examples for org.apache.commons.collections4.map MultiValueMap getCollection

Introduction

In this page you can find the example usage for org.apache.commons.collections4.map MultiValueMap getCollection.

Prototype

@SuppressWarnings("unchecked")
public Collection<V> getCollection(final Object key) 

Source Link

Document

Gets the collection mapped to the specified key.

Usage

From source file:info.rmapproject.webapp.service.DataDisplayServiceImpl.java

@Override
public Graph getDiSCOGraph(DiSCODTO discoDTO) throws Exception {
    Graph graph = graphFactory.newGraph();
    String sDiscoUri = discoDTO.getUri().toString();

    graph.addNode(sDiscoUri, sDiscoUri, discoNodeType);

    if (WebappUtils.isUri(discoDTO.getCreator())) {
        graph.addNode(discoDTO.getCreator(), agentNodeType);
        graph.addEdge(sDiscoUri, discoDTO.getCreator(), DCTERMS.CREATOR.toString());
    }/*from  w  ww  .j  ava  2  s .  c  o m*/

    if (WebappUtils.isUri(discoDTO.getProviderId())) {
        List<URI> rdfTypes = rmapService.getResourceRdfTypesInContext(new URI(discoDTO.getProviderId()),
                discoDTO.getUri());
        String targetNodeType = WebappUtils.getNodeType(rdfTypes);
        graph.addNode(discoDTO.getProviderId(), targetNodeType);
        graph.addEdge(sDiscoUri, discoDTO.getProviderId(), RMAP.PROVIDERID.toString());
    }

    for (URI aggregate : discoDTO.getAggregatedResources()) {
        List<URI> rdfTypes = rmapService.getResourceRdfTypesInContext(aggregate, discoDTO.getUri());
        String targetNodeType = WebappUtils.getNodeType(rdfTypes);
        graph.addNode(aggregate.toString(), targetNodeType);
        graph.addEdge(sDiscoUri, aggregate.toString(), Terms.ORE_AGGREGATES_PATH);
    }

    List<RMapTriple> triples = discoDTO.getRelatedStatements();

    List<RMapTriple> filteredTriples = new ArrayList<RMapTriple>();
    List<RMapTriple> labelTriples = new ArrayList<RMapTriple>();
    List<String> labelFlds = Arrays.asList(this.labelTypes.split(","));

    MultiValueMap<String, URI> typeMap = new MultiValueMap<String, URI>();

    //sort in triples into lists that will be used for graph creation
    for (RMapTriple triple : triples) {
        RMapResource subj = triple.getSubject();
        RMapIri pred = triple.getPredicate();
        RMapValue obj = triple.getObject();

        boolean isType = pred.toString().equals(RDF.TYPE.toString());

        if (!isType && !(obj instanceof RMapLiteral)) {
            filteredTriples.add(triple);
        }

        if (isType && obj instanceof RMapIri) {
            typeMap.put(subj.toString(), new URI(obj.toString()));
        }

        if (obj instanceof RMapLiteral && labelFlds.contains(pred.toString())) {
            labelTriples.add(triple);
        }

    }

    triples = null;

    if ((filteredTriples.size() + graph.getEdges().size()) <= maxObjGraphRelationships) {
        for (RMapTriple triple : filteredTriples) {
            String subject = triple.getSubject().toString();
            String predicate = triple.getPredicate().toString();
            String object = triple.getObject().toString();

            List<URI> subjTypes = null;
            List<URI> objTypes = null;
            if (typeMap != null && typeMap.getCollection(subject) != null) {
                subjTypes = new ArrayList<URI>(typeMap.getCollection(subject));
            }
            if (typeMap != null && typeMap.getCollection(object) != null) {
                objTypes = new ArrayList<URI>(typeMap.getCollection(object));
            }

            graph.addNode(subject, WebappUtils.getNodeType(subjTypes));
            graph.addNode(object, WebappUtils.getNodeType(objTypes));
            graph.addEdge(subject, object, predicate);
        }

        //make node labels from label triples within the graph
        for (RMapTriple triple : labelTriples) {
            if (graph.getNodes().containsKey(triple.getSubject().toString())) {
                graph.getNodes().get(triple.getSubject().toString()).setLabel(triple.getObject().toString());
            }
        }

    } else {
        //don't do graph because it's too large and will just be an unbearable mess!!
        graph = null;
    }
    return graph;
}

From source file:org.lockss.plugin.silverchair.PostHttpClientUrlConnection.java

public void storeResponseHeaderInto(Properties props, String prefix) {
    // store all header properties (this is the only way to iterate)
    // first collect all values for any repeated headers.
    MultiValueMap<String, String> map = new MultiValueMap<String, String>();
    Header[] headers = method.getResponseHeaders();
    for (int ix = 0; ix < headers.length; ix++) {
        Header hdr = headers[ix];//from www . j a  v a  2s.  com
        String key = hdr.getName();
        String value = hdr.getValue();
        if (value != null) {
            // only store headers with values
            // qualify header names to avoid conflict with our properties
            if (key == null) {
                key = "header_" + ix;
            }
            String propKey = (prefix == null) ? key : prefix + key;
            if (!singleValuedHdrs.isEmpty() && singleValuedHdrs.contains(key.toLowerCase())) {
                map.remove(propKey);
            }
            map.put(propKey, value);
        }
    }
    // now combine multiple values into comma-separated string
    for (String key : map.keySet()) {
        Collection<String> val = map.getCollection(key);
        props.setProperty(key,
                ((val.size() > 1) ? StringUtil.separatedString(val, ",") : CollectionUtil.getAnElement(val)));
    }
}

From source file:pltag.corpus.ConnectionPathCalculator.java

/**
 * Calculates which prediction trees need to be generated from the set of connectionNodes.
 * And combines prediction trees if possible.
 * /*from  w w  w.ja  v a2  s. c  om*/
 * @param currentLeafNumber
 * @param lexicon 
 * @return
 */
@SuppressWarnings("unchecked") // cast to Collection<String>
private List<PredictionStringTree> generatePredictionTrees(int currentLeafNumber, List<StringTree> lexicon) {//, String leaf) {
                                                                                                             //if (currentLeafNumber.equals("6"))
                                                                                                             //System.out.print("");
    MultiValueMap predictionTreeNodeMap = findNodesWithGreaterLeafnumbers(currentLeafNumber);
    ArrayList<PredictionStringTree> localPredictedTrees = new ArrayList<PredictionStringTree>();
    int sourcetreeno = 0;
    ArrayList<PredictionStringTree> unhelpfulpredtrees = new ArrayList<PredictionStringTree>();
    HashMap<Integer, Integer> translations = new HashMap<Integer, Integer>();
    //System.out.println("prediction tree to connect leaf " + currentLeafNumber + " number of pred trees needed: "+ predictionTreeNodeMap.size());
    //System.out.println("nodes needed: "+predictionTreeNodeMap);
    for (Integer predictionTreeOrigin : (Collection<Integer>) predictionTreeNodeMap.keySet()) {
        ElementaryStringTree originalStringTree = (ElementaryStringTree) sentenceWordLex[predictionTreeOrigin];
        //            ElementaryStringTree originalStringTree = (ElementaryStringTree) sentenceWordLex[Integer.parseInt(predictionTreeOrigin)];
        if (originalStringTree == null) {
            continue;
        }
        originalStringTree.makeLexiconEntry();
        if (originalStringTree.categories[originalStringTree.root] != null)
        //            if (originalStringTree.categories[Integer.parseInt(originalStringTree.root)] != null)
        {
            translations.putAll(originalStringTree.removeUnaryNodes(originalStringTree.root));
        } else {
            translations.putAll(originalStringTree.removeUnaryNodes(originalStringTree.coordanchor));
        }
        if (originalStringTree.getAnchor() == Integer.MIN_VALUE
                && originalStringTree.treeString.startsWith("*"))
        //            if (originalStringTree.getAnchor().equals("") && originalStringTree.treeString.startsWith("*"))
        {
            continue;
        }
        Collection<Integer> cn = predictionTreeNodeMap.getCollection(predictionTreeOrigin);
        PredictionStringTree predictionTree = buildPredictionTree(originalStringTree, cn, currentLeafNumber);
        //            PredictionStringTree predictionTree = buildPredictionTree(originalStringTree, cn, Integer.parseInt(currentLeafNumber));
        predictionTree.cutTail(cn);
        if (predictionTree.hasUsefulNodes(cn, translations)) {
            predictionTree = buildPredictionTree(originalStringTree, cn, currentLeafNumber);
            //                predictionTree = buildPredictionTree(originalStringTree, cn, Integer.parseInt(currentLeafNumber));
            sourcetreeno++;
            //System.out.println(predictionTree.print());
            ArrayList<PredictionStringTree> newlist = new ArrayList<PredictionStringTree>();
            ArrayList<PredictionStringTree> removelist = new ArrayList<PredictionStringTree>();
            // combine prediction trees (trees can always be combined! I think.)
            for (PredictionStringTree otherTree : localPredictedTrees) {
                PredictionStringTree ct = combinePredTrees(predictionTree, otherTree,
                        predictionTreeNodeMap.values(), translations);//.copyPred();
                if (ct != null) {
                    removelist.add(otherTree);
                    removelist.add(predictionTree);
                    newlist.remove(predictionTree);
                    newlist.add(ct);
                    predictionTree = ct;
                }
            }
            if (predictionTree.isAuxtree()) {
                localPredictedTrees.add(predictionTree);
            } else {
                localPredictedTrees.add(0, predictionTree);
            }
            localPredictedTrees.removeAll(removelist);
            //            might add too much here.
            for (PredictionStringTree npt : newlist) {
                if (predictionTree.isAuxtree()) {
                    localPredictedTrees.add(npt);
                } else {
                    localPredictedTrees.add(0, npt);
                }
            }
        } else {
            predictionTree = buildPredictionTree(originalStringTree, cn, currentLeafNumber);
            //                predictionTree = buildPredictionTree(originalStringTree, cn, Integer.parseInt(currentLeafNumber));
            unhelpfulpredtrees.add(predictionTree);
        }
    }
    if (localPredictedTrees.isEmpty() & unhelpfulpredtrees.size() > 0) {
        PredictionStringTree first = null;
        int min = Integer.MAX_VALUE;
        //         String others = "";
        for (PredictionStringTree pt : unhelpfulpredtrees) {
            if (pt.isAuxtree()) {
                continue;
            }
            int anchorpos = pt.getAnchorList().get(0);
            //                int anchorpos = Integer.parseInt(pt.getAnchorList().get(0));
            if (anchorpos < min) {
                min = anchorpos;
                if (first != null)
                //            others += " @ "+first.toString();
                {
                    first = pt;
                }
            }
            //      else{ 
            //         if (first !=null)
            //         others += " @ "+first.toString();
            //      }
        }
        if (first != null) {
            //      System.out.println(first+"\t"+others);
            localPredictedTrees.add(first);
        }
    } //*/
    if (localPredictedTrees.size() > 1) {
        PredictionStringTree predictionTree = localPredictedTrees.get(0);
        ArrayList<PredictionStringTree> newlist = new ArrayList<PredictionStringTree>();
        ArrayList<PredictionStringTree> removelist = new ArrayList<PredictionStringTree>();
        for (int i = 1; i < localPredictedTrees.size(); i++) {
            PredictionStringTree otherTree = localPredictedTrees.get(i);
            PredictionStringTree ct = combinePredTrees(predictionTree, otherTree,
                    predictionTreeNodeMap.values(), translations);//.copyPred();
            if (ct != null) {
                removelist.add(otherTree);
                removelist.add(predictionTree);
                newlist.remove(predictionTree);
                newlist.add(ct);
                predictionTree = ct;
            }
        }
        if (predictionTree.isAuxtree()) {
            localPredictedTrees.add(predictionTree);
        } else {
            localPredictedTrees.add(0, predictionTree);
        }
        localPredictedTrees.removeAll(removelist);
        //            might add too much here.
        for (PredictionStringTree npt : newlist) {
            if (predictionTree.isAuxtree()) {
                localPredictedTrees.add(npt);
            } else {
                localPredictedTrees.add(0, npt);
            }
        }
    }
    for (PredictionStringTree pst : localPredictedTrees) {
        pst.cutTail(predictionTreeNodeMap.values());
    }

    if (localPredictedTrees.size() > 1) {
        LogInfo.error("unaccounted case! combination of prediction trees; number of trees: " + sourcetreeno
                + " to connect leaf " + currentLeafNumber);
    }
    //        noOfSources.put(sourcetreeno + "", noOfSources.get(sourcetreeno + "").intValue() + 1);
    noOfSources.put(sourcetreeno, noOfSources.get(sourcetreeno) + 1);
    return localPredictedTrees;
}

From source file:pltag.corpus.PredictionStringTree.java

protected String printOrigin(Integer nodeId, MultiValueMap<Integer, Integer> origin) {
    if (mapping == null) {
        makeMapping(origin);/* www  .j a v a2s. co m*/
    }
    if (origin.containsKey(nodeId)) {
        Integer index = origin.getCollection(nodeId).iterator().next();
        //         String index = (String) origin.getCollection(nodeIDstring).iterator().next();
        if (index >= mapping.length) {
            makeMapping(origin);
        }
        return String.valueOf(mapping[index]);
        //         return mapping[Integer.parseInt(index)];
    }
    return null;
}

From source file:pltag.corpus.StringTree.java

@SuppressWarnings("unchecked")
protected Collection<Integer> getOrigin(Integer nodeID, MultiValueMap<Integer, Integer> origin) {
    if (origin == null) {
        return null;
    }//w  w  w  .jav  a 2  s  . c  om
    if (origin.containsKey(nodeID)) {
        return origin.getCollection(nodeID);
    }
    return null;
}

From source file:pltag.corpus.StringTree.java

@SuppressWarnings("unchecked")
protected Integer getLowestOrigin(Integer nodeID, MultiValueMap<Integer, Integer> origin) {
    Integer lowest = 9999999;//from w  ww  .  ja  v  a 2 s .  co  m
    if (origin.containsKey(nodeID)) {
        Collection<Integer> nodeIds = origin.getCollection(nodeID);
        if (nodeIds.isEmpty())
            return -1;
        for (Integer id : nodeIds) {
            if (id < lowest) {
                lowest = id;
            }
        }
        //            Iterator<Integer> nodeids = origin.getCollection(nodeID).iterator();
        //            if (origin.getCollection(nodeID).isEmpty())
        //            {
        //                return -1;
        ////                return "-1";
        //                //return null;
        //            }
        //            while (nodeids.hasNext())
        //            {
        //                Integer id = nodeids.next();
        ////                int id = Integer.parseInt((String) nodeids.next());
        //                if (id < lowest)
        //                {
        //                    lowest = id;
        //                }
        //            }
    } else {
        return null;
    }
    return lowest;
    //        return Integer.toString(lowest);
}

From source file:pltag.corpus.TagCorpus.java

/**
 * /*w ww  .j av  a2s . c  om*/
 * @param elementaryTrees
 * @param sentenceTree 
 * @param sentenceWordLex2 
 * @param traces
 * @param fillers
 * @return
 */
private List<ElementaryStringTree> traceAndFillerTreatment(List<ElementaryStringTree> elementaryTrees,
        CompositeStringTree sentenceTree, ElementaryStringTree[] sentenceWordLex2,
        MultiValueMap<String, ElementaryStringTree> traces, HashMap<String, ElementaryStringTree> fillers) {
    for (String traceId : traces.keySet()) {
        for (ElementaryStringTree traceTree : traces.getCollection(traceId)) {
            //                ElementaryStringTree traceTree = traces.get(traceId);
            ElementaryStringTree tracefather = null;
            if (traceTree.getTraceFather() != null) {
                //String rootNodeId = tracetree.getTraceFather().getRoot().getNodeID();
                //String leafOrigin = sentenceTree.getLowestOrigin(rootNodeId, sentenceTree.originDown);
                Integer leafOrigin = traceTree.getTraceFather().getMainLeafNode().getLeafNo();
                Integer leafOrigincoord = sentenceTree.getLowestOrigin(traceTree.getRoot(),
                        sentenceTree.originUp);
                tracefather = sentenceWordLex2[leafOrigin];
                if (leafOrigincoord != null) {
                    ElementaryStringTree tracefathercoord = sentenceWordLex2[leafOrigincoord];
                    if (!leafOrigin.equals(leafOrigincoord)) {
                        if (tracefather.getRoot() == traceTree.getTraceFather().getRoot().getNodeID())
                        //                        if (tracefather.getRoot().equals(tracetree.getTraceFather().getRoot().getNodeID()))
                        {
                        } else if (tracefathercoord.getRoot() == traceTree.getTraceFather().getRoot()
                                .getNodeID())
                        //                        else if (tracefathercoord.getRoot().equals(tracetree.getTraceFather().getRoot().getNodeID()))
                        {
                            tracefather = tracefathercoord;
                        } else {
                            LogInfo.error(
                                    "stuck for ideas for where to stick trace TagCorpus:traceAndFillerTreatment.");
                        }
                    }
                }
            }
            ElementaryStringTree fillertree = fillers.get(traceId);
            if (fillertree == null) {
                return elementaryTrees;
            }
            if (fillertree.isAuxtree() && traceTree.isAuxtree() && tracefather != null
                    && tracefather.categories[traceTree.getRoot()] != null
                    && tracefather.categories[fillertree.getRoot()] != null)
            //            if (fillertree.isAuxtree() && tracetree.isAuxtree() && tracefather != null
            //                    && tracefather.categories[Integer.parseInt(tracetree.getRoot())] != null
            //                    && tracefather.categories[Integer.parseInt(fillertree.getRoot())] != null)
            {
                fillertree.setMCTAG(traceTree);
                traceTree.setMCTAG(fillertree);
            }
            //else if (fillertree.getSubstNodes().contains(tracefather) && !tracetree.isAuxtree()){
            //MCTAG
            //}
            else if (tracefather != null) {
                if (traceTree.isAuxtree()) {
                    Integer rootid = traceTree.getRoot();
                    Integer traceFatherOldRootDownIndex = tracefather.getLowestOrigin(rootid,
                            tracefather.originDown);
                    Integer footid = traceTree.getFoot();
                    Integer traceOldFootUpIndex = traceTree.getLowestOrigin(footid, traceTree.originUp);

                    tracefather.integrate(traceTree);
                    while (tracefather.originDown.containsValue(rootid, traceFatherOldRootDownIndex)) {
                        tracefather.originDown.remove(rootid, traceFatherOldRootDownIndex);
                    }
                    while (tracefather.originUp.containsValue(footid, traceFatherOldRootDownIndex)) {
                        tracefather.originUp.remove(footid, traceOldFootUpIndex);
                    }
                } else {
                    tracefather.integrate(traceTree);
                }
                elementaryTrees.remove(traceTree);
            } else if (traceTree.getNodeType(fillertree.getRoot()) == TagNodeType.subst) {
                fillertree.integrate(traceTree);
                //tracetree.integrate(fillertree);
                elementaryTrees.remove(traceTree);
                traceTree = fillertree;
            } else if (fillertree.isAuxtree()) {
                fillertree.integrate(traceTree);
                elementaryTrees.remove(traceTree);
                sentenceWordLex2[traceTree.getLowestOrigin(traceTree.root, traceTree.originDown)] = fillertree;
                //                sentenceWordLex2[Integer.parseInt(tracetree.getLowestOrigin(tracetree.root, tracetree.originDown))] = fillertree;
            }
        }
    } //for each trace id
    return elementaryTrees;
}

From source file:pltag.parser.Lexicon.java

/**
 * Converts a MultiValueMap with String values to one with StringTree values.
 * @param treetype // w w w. j  a  v  a2s  . co m
 * 
 * @param MultiValueMap lexString
 * @return MultiValueMap lexTree
 */
@SuppressWarnings("unchecked")
private MultiValueMap makeLexTrees(MultiValueMap lexString, String treetype) {
    MultiValueMap<String, ElementaryStringTree> lexTree = new MultiValueMap();
    Set<String> keys = lexString.keySet();
    for (String key : keys) {
        Collection<String> values = lexString.getCollection(key);
        HashMap<String, ElementaryStringTree> treelist = new HashMap<String, ElementaryStringTree>();
        for (String treeString : values) {
            ElementaryStringTree tree = makeToStringTreeOld(treeString, treetype);
            if (tree == null) {
                continue;
            }
            String POSword = tree.getPOStagged(tree.getRoot()).trim();
            if (key.equals("prediction: ")) {
                POSword = key;
            }
            // for lexentries for "put up" etc, add three times into Map: as "put up", "put" and "up".
            String[] words = POSword.split("\t");
            ElementaryStringTree sametree = null;
            if (!treelist.containsKey(POSword + "@@" + tree.toString())) {
                lexTree.put(POSword, tree);
            }
            treelist.put(POSword + "@@" + tree.toString(), tree);
            if (words.length > 1) {
                for (String word : words) {
                    if (sametree == null) {
                        lexTree.put(word, tree);
                    }
                }
            }
        }
    }
    return lexTree;
}

From source file:pltag.parser.Lexicon.java

/**
 * Converts a MultiValueMap with String values to one with StringTree values.
 * @param lexString/*from  w w w .j av a  2  s .c  o m*/
 * @param treetype
 * @return MultiValueMap lexTree
 */
@SuppressWarnings("unchecked")
protected MultiValueMap makeLexStrings(MultiValueMap lexString, String treetype) {
    MultiValueMap lexTree = new MultiValueMap();
    Set<String> keys = lexString.keySet();
    HashSet<String> treelist = new HashSet<String>();
    for (String key : keys) {
        Collection<String> values = lexString.getCollection(key);
        for (String treeString : values) {
            //if (tree == null) continue; //need to deal with errors at different point.
            //need to extract POS tag from treestring and unlexicalize tree.
            String posWord = key;
            String tree = treeString;
            if (!key.equals("prediction: ")) {
                if (opts.goldPosTags || opts.treeFamilies) {//pos and word given
                    //                        if (this.getClass() == UnkLexicon.class) // FIX: Unnecessary check
                    //                        {
                    //                            posWord = UnkLexicon.getPosFromTreeString(treeString, key);
                    //                        }
                    //                        else
                    //                        {
                    //                            posWord = getPosFromTreeString(treeString, key);
                    //                        }                        
                    posWord = getPosFromTreeString(treeString, key);
                    if (opts.posOnly) {//only pos tag given
                        String[] words = posWord.split("\t");
                        posWord = "";
                        for (String w : words) {
                            if (w.contains("*") || w.equals("0")) {
                                continue;
                            } else {
                                posWord += w.substring(0, w.indexOf(" ")) + "\t";
                            }
                        }
                        posWord = posWord.trim();
                    }
                } else {// only word
                    posWord = Utils.getCutOffCorrectedMainLex(key.toLowerCase(), listOfFreqWords, opts.train,
                            opts.fullLex);
                    if (key.contains(" ")) {
                        posWord = posWord.replace(" ", "\t");
                    }
                }
                tree = makeUnlex(treeString, key);
            }
            if (noOfTrees.containsKey(treeString) && posWord.contains(" ")
                    && (opts.goldPosTags || opts.treeFamilies) && !opts.posOnly) {
                String pos = posWord.substring(0, posWord.indexOf(" "));
                String puretree = pos + "\t" + tree.substring(2);
                if (noOfTrees.containsKey(puretree)) {
                    noOfTrees.put(puretree, noOfTrees.get(puretree) + noOfTrees.get(treeString));
                } else {
                    noOfTrees.put(puretree, noOfTrees.get(treeString));
                }
                noOfTrees.remove(treeString);

            }
            // for lexentries for "put up" etc, add three times into Map: as "put up", "put" and "up".
            String[] words = posWord.split("\t");
            ElementaryStringTree sametree = null;

            if (!treelist.contains(posWord + "@@" + tree) && words.length == 1) {
                String lc = posWord.toLowerCase();
                // 
                String wlc = lc.substring(lc.indexOf(" ") + 1);
                if (!opts.goldPosTags && opts.treeFamilies && !lc.equals("prediction: ")) {
                    lexTree.put(wlc, tree);
                } else {
                    lexTree.put(lc, tree);
                }
                if (!wordPosMap.containsValue(wlc, lc)) {
                    wordPosMap.put(wlc, lc);
                }
                trees.put(tree.substring(tree.indexOf("\t") + 1), lc);
            }
            treelist.add(posWord + "@@" + tree);
            if (words.length > 1) {
                for (String word : words) {
                    if (sametree == null && !word.startsWith(" *T*") && !word.startsWith(" *?*")
                            && !word.startsWith(" *-") && !word.equals(" *") && !word.equals(" 0")) {
                        String lc = word.toLowerCase();
                        String wlc = lc.substring(lc.indexOf(" ") + 1);
                        if (!opts.goldPosTags && opts.treeFamilies) {
                            lexTree.put(wlc, tree);
                        } else {
                            lexTree.put(lc, tree);
                        }
                        if (!wordPosMap.containsValue(wlc, lc)) {
                            wordPosMap.put(wlc, lc);
                        }
                        trees.put(tree.substring(tree.indexOf("\t") + 1), lc);
                    } // if
                } // for
            } // if
        } // for (values)
    } // for (keys)
    return lexTree;
}

From source file:pltag.parser.ParsingTask.java

/**
 * Combines two trees by first checking whether they can be combined (no two shadow trees, or unused shadow tree)
 * and then calling the integration function and inserting results into the chart.
 * @param trees/*ww w . java  2s  . c o  m*/
 * @param chart
 * @param chartindex
 * @param posOfNextWord 
 * @return
 */
@SuppressWarnings("unchecked") //
private ArrayList<ChartEntry> combineTrees(Collection<ElementaryStringTree> trees, Chart chart, // Collection<ChartEntry> lastSliceCopy, 
        short chartindex, String[] origPosTags, double startTime, String posOfNextWord, SuperTagger superTagger,
        int beamWidth, boolean endfOfSent, int timestamp) {
    ArrayList<ChartEntry> newEntries = new ArrayList<ChartEntry>();
    // try to integrate each tree (some may have two alternative fringes) 
    // with prefix trees from prev slice.
    // add successful combinations to chart.
    ArrayList<ChartEntry> lastSliceCopy = getLastSlice(chartindex, chart);
    HashMap<ElementaryStringTree, ArrayList<TreeState>> treeStateMap = new HashMap<ElementaryStringTree, ArrayList<TreeState>>();
    double totalTimeSuperTag = 0.0;
    int a = 0;
    int b = 0;
    double bestprob = Double.NEGATIVE_INFINITY;
    for (ChartEntry chartEntry : lastSliceCopy) {
        a++;
        Collection<ElementaryStringTree> selectedTrees;
        double start = System.currentTimeMillis();
        //System.out.print(trees.size()+" ");
        // call the supertagger only on prediction trees (when trees are elementary trees, posOfNextWord is always  "")
        boolean shadowTree = !posOfNextWord.equals("");
        //            selectedTrees = opts.train || shadowTree ? superTagger.superTag(trees, chartEntry, posOfNextWord) : trees ;
        selectedTrees = opts.train || !shadowTree ? trees
                : superTagger.superTag(trees, chartEntry, posOfNextWord);
        //  selectedTrees = trees;
        //System.out.println(selectedTrees.size());
        totalTimeSuperTag += (System.currentTimeMillis() - start);
        for (ElementaryStringTree tree : selectedTrees) {
            b++;
            ArrayList<TreeState> elemtreeStates;
            if (treeStateMap.containsKey(tree)) {
                elemtreeStates = treeStateMap.get(tree);
            } else {
                elemtreeStates = getTreeStateForTree(tree, chartindex);
                treeStateMap.put(tree, elemtreeStates);
            }
            for (TreeState elemtreetreeState : elemtreeStates) {
                MultiValueMap<String, ParserOperation> combinations = new MultiValueMap();
                //      if (System.currentTimeMillis() - startTime > 60000) return newEntries;
                if (tree.hasShadowInd() && chartEntry.getTreeState().getShadowTrees().size() > 2) {
                    continue;
                }
                TreeState treeState = chartEntry.getTreeState();
                if (opts.verbose && treeState.getUnaccessibles().isEmpty()
                        && treeState.getFutureFringe().getNext() != null) {
                    LogInfo.error("why not expanded???");
                }
                String fringeString = treeState.getFringe().toString();
                List<ChartEntry> ces = new ArrayList<ChartEntry>();
                if (combinations.containsKey(fringeString)) {
                    Collection<ParserOperation> operations = combinations.getCollection(fringeString);
                    if (!elemtreetreeState.getRootNode().isShadow()) {
                        ces.addAll(match(tree, elemtreetreeState, chart, chartEntry, origPosTags, chartindex));
                    }
                    if (operations.size() == 1 && operations.contains(null)) {
                        //don't need to do anything because this prefix tree does not integrate with prefix tree
                        //need to do this at tree level.
                    } else {
                        for (ParserOperation operation : operations) {
                            if (operation == null || operation == ParserOperation.verify) {
                                continue;
                            } else {
                                ces.addAll(operation.combine(model, opts, words, origPosTags, treeState,
                                        elemtreetreeState, tree, chartindex));
                            }
                        }
                    }
                } else {//*/
                    combinations.put(fringeString, null);
                    if (combineTwoShadowTreesInARow(chartEntry, elemtreetreeState)) {
                        continue;
                    }
                    //*****************
                    ces = integrate(treeState, elemtreetreeState, tree, chart, chartEntry, origPosTags,
                            chartindex);
                    //*****************
                }
                // clean up results
                for (ChartEntry cefirst : ces) {
                    ArrayList<ChartEntry> expandedCEs = makeExpansions(cefirst);
                    for (ChartEntry ce : expandedCEs) {
                        for (BuildBlock bb : ce.getBuildBlocks()) {
                            //System.out.println(bb.toString()+ bb.getIpi()+"ELEM:" + tree);
                            if (didNotUseShadow(chartEntry, bb, bb.getIpi(), chartindex, tree)) {
                                LinkedList<BuildBlock> list = new LinkedList<BuildBlock>();
                                list.add(bb);
                                //                                    ce.getTreeState().getFutureFringe().getBBHist().remove(bb); // TODO: FIX
                                ce.getTreeState().getFutureFringe().getBBHist().remove(list); // TODO: FIX
                                continue;
                            }
                            bb.setPrevChartEntry(chartEntry);
                            if (!opts.train || opts.useProbabilityModel) {
                                bb.retrieveProbability(model.getParams(), opts.freqBaseline);
                                bb.removeElemTree();
                                //                        bb.removeFreqCounter();
                            }
                            if (!elemtreetreeState.getShadowTrees().isEmpty()) {
                                ShadowStringTree shadowt = ce.getTreeState().getShadowTrees()
                                        .get(ce.getTreeState().getShadowTrees().size() - 1);
                                shadowt.setPredictProb(bb.getProbability());
                            }
                            if (!combinations.containsValue(fringeString, bb.getOperation())) {
                                combinations.put(fringeString, bb.getOperation());
                            }
                            //System.out.print("\n"+ce.getTreeState().getFringe());
                            if (!opts.train || opts.useProbabilityModel) {
                                //                                    double vlap = computeLAP(tree);
                                ce.updateProbs(ce.getTreeState().getFutureFringe(), 0);
                            }
                            //System.out.println(ce.getBestProbability()+"\t"+ce+"\n"+elemtreetreeState+"\n"+tree+a+" "+b+"\n---\n");
                            if (opts.pruneUsingScores) {
                                if (ce.getBestScore() > bestprob) {
                                    bestprob = ce.getBestScore();
                                }
                                if (ce.getBestScore() > bestprob - beamWidth) {
                                    newEntries.add(ce);
                                }
                            } else {
                                double bestPropWithVlap = ce.getBestProbability()
                                        + (opts.train || !shadowTree ? 0.0d : computeLAP(tree));
                                if (bestPropWithVlap > bestprob) {
                                    bestprob = bestPropWithVlap;
                                }
                                if (opts.train || bestPropWithVlap > bestprob - beamWidth) {
                                    newEntries.add(ce);
                                }
                            }
                        } // for each build block
                    } // for each expanded chart entry
                } // for each new chart entry
            } // for each elementary tree state (fringes) ALWAYS ONE
        } // for each elementary tree
    } // for each chart entry
    //        if (opts.timeProfile && totalTimeSuperTag > 100)
    //        {
    //            Utils.log("supertagtime: " + totalTimeSuperTag / 1000 + "\t");
    //        }
    return newEntries;
}