@Test public void testBuild() { final ParseTree pt = ParseTree.build(jCas); assertEquals(onetwo, pt.getParent(tokens[0]).getChunk()); assertEquals(onetwo, pt.getParent(tokens[1]).getChunk()); assertEquals(threefour, pt.getParent(tokens[2]).getChunk()); assertEquals(threefour, pt.getParent(tokens[3]).getChunk()); final List<WordToken> words = pt.getChildWords(onetwo, x -> true).collect(Collectors.toList()); assertEquals(2, words.size()); assertEquals(tokens[0], words.get(0)); assertEquals(tokens[1], words.get(1)); assertEquals(0, pt.getChildWords(sentence, x -> false).count()); pt.traverseChildren(l -> childrenCount++); assertEquals(3, childrenCount); }
public static ParseTree build(JCas jCas) { final Map<PhraseChunk, List<PhraseChunk>> index = JCasUtil.indexCovering(jCas, PhraseChunk.class, PhraseChunk.class); final Collection<PhraseChunk> phrases = JCasUtil.select(jCas, PhraseChunk.class); final List<ParseTreeNode> roots = new LinkedList<>(); final Map<PhraseChunk, ParseTreeNode> chunkToNode = new HashMap<>(); for (final PhraseChunk chunk : phrases) { ParseTreeNode treeNode = chunkToNode.computeIfAbsent(chunk, ParseTreeNode::new); final Collection<PhraseChunk> covering = index.get(chunk); if (covering == null || covering.isEmpty()) { roots.add(treeNode); } else { final PhraseChunk parent = findSmallest(covering); ParseTreeNode parentNode = chunkToNode.get(parent); if (parentNode == null) { parentNode = new ParseTreeNode(parent); chunkToNode.put(parent, parentNode); } treeNode.setParent(parentNode); parentNode.addChild(treeNode); } } final Map<PhraseChunk, List<WordToken>> wordIndex = JCasUtil.indexCovered(jCas, PhraseChunk.class, WordToken.class); final Map<WordToken, ParseTreeNode> wordToNode = new HashMap<>(); chunkToNode .values() .forEach( n -> { n.getChildren().sort(SENTENCE_ORDER); final Collection<WordToken> allWords = wordIndex.get(n.getChunk()); if (allWords != null) { final List<WordToken> words = new ArrayList<>(allWords); if (n.hasChildren()) { n.getChildren().stream() .map(t -> wordIndex.get(t.getChunk())) .filter(Objects::nonNull) .forEach(words::removeAll); } n.addWords(words); words.stream().forEach(w -> wordToNode.put(w, n)); } }); roots.sort(SENTENCE_ORDER); return new ParseTree(roots, chunkToNode, wordToNode); }
ParseTree { public static ParseTree build(JCas jCas) { final Map<PhraseChunk, List<PhraseChunk>> index = JCasUtil.indexCovering(jCas, PhraseChunk.class, PhraseChunk.class); final Collection<PhraseChunk> phrases = JCasUtil.select(jCas, PhraseChunk.class); final List<ParseTreeNode> roots = new LinkedList<>(); final Map<PhraseChunk, ParseTreeNode> chunkToNode = new HashMap<>(); for (final PhraseChunk chunk : phrases) { ParseTreeNode treeNode = chunkToNode.computeIfAbsent(chunk, ParseTreeNode::new); final Collection<PhraseChunk> covering = index.get(chunk); if (covering == null || covering.isEmpty()) { roots.add(treeNode); } else { final PhraseChunk parent = findSmallest(covering); ParseTreeNode parentNode = chunkToNode.get(parent); if (parentNode == null) { parentNode = new ParseTreeNode(parent); chunkToNode.put(parent, parentNode); } treeNode.setParent(parentNode); parentNode.addChild(treeNode); } } final Map<PhraseChunk, List<WordToken>> wordIndex = JCasUtil.indexCovered(jCas, PhraseChunk.class, WordToken.class); final Map<WordToken, ParseTreeNode> wordToNode = new HashMap<>(); chunkToNode .values() .forEach( n -> { n.getChildren().sort(SENTENCE_ORDER); final Collection<WordToken> allWords = wordIndex.get(n.getChunk()); if (allWords != null) { final List<WordToken> words = new ArrayList<>(allWords); if (n.hasChildren()) { n.getChildren().stream() .map(t -> wordIndex.get(t.getChunk())) .filter(Objects::nonNull) .forEach(words::removeAll); } n.addWords(words); words.stream().forEach(w -> wordToNode.put(w, n)); } }); roots.sort(SENTENCE_ORDER); return new ParseTree(roots, chunkToNode, wordToNode); } }
ParseTree { public static ParseTree build(JCas jCas) { final Map<PhraseChunk, List<PhraseChunk>> index = JCasUtil.indexCovering(jCas, PhraseChunk.class, PhraseChunk.class); final Collection<PhraseChunk> phrases = JCasUtil.select(jCas, PhraseChunk.class); final List<ParseTreeNode> roots = new LinkedList<>(); final Map<PhraseChunk, ParseTreeNode> chunkToNode = new HashMap<>(); for (final PhraseChunk chunk : phrases) { ParseTreeNode treeNode = chunkToNode.computeIfAbsent(chunk, ParseTreeNode::new); final Collection<PhraseChunk> covering = index.get(chunk); if (covering == null || covering.isEmpty()) { roots.add(treeNode); } else { final PhraseChunk parent = findSmallest(covering); ParseTreeNode parentNode = chunkToNode.get(parent); if (parentNode == null) { parentNode = new ParseTreeNode(parent); chunkToNode.put(parent, parentNode); } treeNode.setParent(parentNode); parentNode.addChild(treeNode); } } final Map<PhraseChunk, List<WordToken>> wordIndex = JCasUtil.indexCovered(jCas, PhraseChunk.class, WordToken.class); final Map<WordToken, ParseTreeNode> wordToNode = new HashMap<>(); chunkToNode .values() .forEach( n -> { n.getChildren().sort(SENTENCE_ORDER); final Collection<WordToken> allWords = wordIndex.get(n.getChunk()); if (allWords != null) { final List<WordToken> words = new ArrayList<>(allWords); if (n.hasChildren()) { n.getChildren().stream() .map(t -> wordIndex.get(t.getChunk())) .filter(Objects::nonNull) .forEach(words::removeAll); } n.addWords(words); words.stream().forEach(w -> wordToNode.put(w, n)); } }); roots.sort(SENTENCE_ORDER); return new ParseTree(roots, chunkToNode, wordToNode); } private  ParseTree(
      List<ParseTreeNode> roots,
      Map<PhraseChunk, ParseTreeNode> chunkToNode,
      Map<WordToken, ParseTreeNode> wordToNode); }
ParseTree { public static ParseTree build(JCas jCas) { final Map<PhraseChunk, List<PhraseChunk>> index = JCasUtil.indexCovering(jCas, PhraseChunk.class, PhraseChunk.class); final Collection<PhraseChunk> phrases = JCasUtil.select(jCas, PhraseChunk.class); final List<ParseTreeNode> roots = new LinkedList<>(); final Map<PhraseChunk, ParseTreeNode> chunkToNode = new HashMap<>(); for (final PhraseChunk chunk : phrases) { ParseTreeNode treeNode = chunkToNode.computeIfAbsent(chunk, ParseTreeNode::new); final Collection<PhraseChunk> covering = index.get(chunk); if (covering == null || covering.isEmpty()) { roots.add(treeNode); } else { final PhraseChunk parent = findSmallest(covering); ParseTreeNode parentNode = chunkToNode.get(parent); if (parentNode == null) { parentNode = new ParseTreeNode(parent); chunkToNode.put(parent, parentNode); } treeNode.setParent(parentNode); parentNode.addChild(treeNode); } } final Map<PhraseChunk, List<WordToken>> wordIndex = JCasUtil.indexCovered(jCas, PhraseChunk.class, WordToken.class); final Map<WordToken, ParseTreeNode> wordToNode = new HashMap<>(); chunkToNode .values() .forEach( n -> { n.getChildren().sort(SENTENCE_ORDER); final Collection<WordToken> allWords = wordIndex.get(n.getChunk()); if (allWords != null) { final List<WordToken> words = new ArrayList<>(allWords); if (n.hasChildren()) { n.getChildren().stream() .map(t -> wordIndex.get(t.getChunk())) .filter(Objects::nonNull) .forEach(words::removeAll); } n.addWords(words); words.stream().forEach(w -> wordToNode.put(w, n)); } }); roots.sort(SENTENCE_ORDER); return new ParseTree(roots, chunkToNode, wordToNode); } private  ParseTree(
      List<ParseTreeNode> roots,
      Map<PhraseChunk, ParseTreeNode> chunkToNode,
      Map<WordToken, ParseTreeNode> wordToNode); Stream<WordToken> getChildWords(PhraseChunk chunk, Predicate<String> chunkFilter); void traverseChildren(Consumer<List<ParseTreeNode>> consumer); static ParseTree build(JCas jCas); ParseTreeNode getParent(WordToken token); void log(); }
ParseTree { public static ParseTree build(JCas jCas) { final Map<PhraseChunk, List<PhraseChunk>> index = JCasUtil.indexCovering(jCas, PhraseChunk.class, PhraseChunk.class); final Collection<PhraseChunk> phrases = JCasUtil.select(jCas, PhraseChunk.class); final List<ParseTreeNode> roots = new LinkedList<>(); final Map<PhraseChunk, ParseTreeNode> chunkToNode = new HashMap<>(); for (final PhraseChunk chunk : phrases) { ParseTreeNode treeNode = chunkToNode.computeIfAbsent(chunk, ParseTreeNode::new); final Collection<PhraseChunk> covering = index.get(chunk); if (covering == null || covering.isEmpty()) { roots.add(treeNode); } else { final PhraseChunk parent = findSmallest(covering); ParseTreeNode parentNode = chunkToNode.get(parent); if (parentNode == null) { parentNode = new ParseTreeNode(parent); chunkToNode.put(parent, parentNode); } treeNode.setParent(parentNode); parentNode.addChild(treeNode); } } final Map<PhraseChunk, List<WordToken>> wordIndex = JCasUtil.indexCovered(jCas, PhraseChunk.class, WordToken.class); final Map<WordToken, ParseTreeNode> wordToNode = new HashMap<>(); chunkToNode .values() .forEach( n -> { n.getChildren().sort(SENTENCE_ORDER); final Collection<WordToken> allWords = wordIndex.get(n.getChunk()); if (allWords != null) { final List<WordToken> words = new ArrayList<>(allWords); if (n.hasChildren()) { n.getChildren().stream() .map(t -> wordIndex.get(t.getChunk())) .filter(Objects::nonNull) .forEach(words::removeAll); } n.addWords(words); words.stream().forEach(w -> wordToNode.put(w, n)); } }); roots.sort(SENTENCE_ORDER); return new ParseTree(roots, chunkToNode, wordToNode); } private  ParseTree(
      List<ParseTreeNode> roots,
      Map<PhraseChunk, ParseTreeNode> chunkToNode,
      Map<WordToken, ParseTreeNode> wordToNode); Stream<WordToken> getChildWords(PhraseChunk chunk, Predicate<String> chunkFilter); void traverseChildren(Consumer<List<ParseTreeNode>> consumer); static ParseTree build(JCas jCas); ParseTreeNode getParent(WordToken token); void log();  }