/** * <B>NOTE:</B> Must be called only after one of the conversion methods was called. * @return a mapping between any Token annotation and all its tree nodes. If the last conversion * had several sentences, they are all return without any division to sentences. If you wish * to have a division by sentence, use {@link #getTokenToNodesBySentence()}.<BR> * for each token, the first node is always the (single) non-deep node. Any subsequent nodes, * if any, are deep nodes. * @throws CasTreeConverterException */ public OneToManyBidiMultiHashMap<Token,BasicNode> getAllTokensToNodes() throws CasTreeConverterException { if (lastTokenToNodeBySentence == null) { throw new CasTreeConverterException("getAllTokensToNodes() called before a conversion method was called."); } OneToManyBidiMultiHashMap<Token,BasicNode> result = new OneToManyBidiMultiHashMap<Token,BasicNode>(); for (OneToManyBidiMultiHashMap<Token,BasicNode> map : lastTokenToNodeBySentence.values()) { OneToManyBidiMultiHashMap<Token,BasicNode> mapCopy = new OneToManyBidiMultiHashMap<Token,BasicNode>(map); result.putAll(mapCopy); } return result; }
private BasicNode convertSentenceToTree(JCas jcas, Sentence sentenceAnno) throws CasTreeConverterException, UnsupportedPosTagStringException { tokenAnnotations = JCasUtil.selectCovered(Token.class, sentenceAnno); tokenToNode = new OneToManyBidiMultiHashMap<Token, BasicNode>(); nodes = new LinkedHashSet<BasicNode>(); childrenByParent = new LinkedHashMap<Token, Set<BasicNode>>(tokenAnnotations.size()); else if (tokenToNode.containsKey(dependent)) { throw new CasTreeConverterException(String.format( "Got dependent in more than one non-deep dependency. Dependent:%s, current governor:%s, all dependencies: %s", Token dependent = depAnno.getDependent(); Collection<BasicNode> dependentNodes = tokenToNode.get(dependent); Collection<BasicNode> parentNodes = tokenToNode.get(entry.getKey()); if (parentNodes != null && parentNodes.size()!=0) {
/** * * @param jcas a JCas created by an EOP LAP * @param sentenceAnno one sentence in the CAS * @return a root of a BIU dependency parse tree of the sentence * @throws CasTreeConverterException * @throws UnsupportedPosTagStringException */ public BasicNode convertSingleSentenceToTree(JCas jcas, Sentence sentenceAnno) throws CasTreeConverterException, UnsupportedPosTagStringException { BasicNode root = convertSentenceToTree(jcas, sentenceAnno); lastRootList = Arrays.asList(new BasicNode[] {root}); lastSentenceList = Arrays.asList(new Sentence[] {sentenceAnno}); lastTokenToNodeBySentence = new LinkedHashMap<Sentence, OneToManyBidiMultiHashMap<Token,BasicNode>>(); OneToManyBidiMultiHashMap<Token,BasicNode> tokenToNodeCopy = new OneToManyBidiMultiHashMap<Token,BasicNode>(tokenToNode); lastTokenToNodeBySentence.put(sentenceAnno, tokenToNodeCopy); return root; }
private BasicNode buildNode(JCas jcas, int serial, int id, Token token, String depType, Token parent) throws CasTreeConverterException, UnsupportedPosTagStringException { String idString = Integer.toString(id); NodeInfo nodeInfo = buildNodeInfo(jcas, token, serial); EdgeInfo edgeInfo = buildEdgeInfo(jcas, depType); BasicNode node = new BasicNode(new DefaultInfo(idString, nodeInfo, edgeInfo)); nodes.add(node); if (parent != null) { registerChild(parent, token, node); } // Note that the first this method is called for each token, we build its non-deep node. // So for every token, the first node is always the non-deep one. All subsequent nodes // may be deep (if any). Note that the value collection in this MultiMap is a List. tokenToNode.put(token, node); return node; }
/** * * @param jcas a JCas created by an EOP LAP * @return A list of roots, each belonging to a BIU dependency parse tree of a sentence in the CAS. Trees are ordered by sentence order. * @throws CasTreeConverterException * @throws UnsupportedPosTagStringException */ public List<BasicNode> convertCasToTrees(JCas jcas) throws CasTreeConverterException, UnsupportedPosTagStringException { Collection<Sentence> sentenceAnnotations = JCasUtil.select(jcas, Sentence.class); lastSentenceList = new ArrayList<Sentence>(sentenceAnnotations); lastTokenToNodeBySentence = new LinkedHashMap<Sentence, OneToManyBidiMultiHashMap<Token,BasicNode>>(); lastRootList = new ArrayList<BasicNode>(sentenceAnnotations.size()); lastSentenceToRoot = new DualHashBidiMap<>(); for (Sentence sentenceAnno : lastSentenceList) { BasicNode root = convertSentenceToTree(jcas, sentenceAnno); lastSentenceToRoot.put(sentenceAnno, root); lastRootList.add(root); OneToManyBidiMultiHashMap<Token,BasicNode> tokenToNodeCopy = new OneToManyBidiMultiHashMap<Token,BasicNode>(tokenToNode); lastTokenToNodeBySentence.put(sentenceAnno, tokenToNodeCopy); } return lastRootList; }