/** * Generate the covered paths, reducing to the lowest depth. * * @param structureHierarchy the structure hierarchy * @param coveredStructures the covered structures * @param depth the maximum depth * @return list of paths for the covered structures */ private List<String> generateCoveredPaths( ItemHierarchy<Structure> structureHierarchy, List<Structure> coveredStructures, int depth) { LinkedHashSet<String> collect = coveredStructures .stream() .map(s -> structureHierarchy.getSelectorPath(s).toDepth(depth).toString()) .collect(Collectors.toCollection(LinkedHashSet::new)); Builder<String> builder = ImmutableList.<String>builder(); String parent = collect.iterator().next(); builder.add(parent); for (String path : collect) { if (!path.startsWith(parent)) { builder.add(path); parent = path; } } return builder.build(); }
@Test public void testGenerateNestedToDepth1() { Section section = new Section(jCas); section.setBegin(0); section.setDepth(1); section.setEnd(TEXT.length()); section.addToIndexes(); Paragraph paragraph1 = new Paragraph(jCas); paragraph1.setBegin(0); paragraph1.setDepth(2); paragraph1.setEnd(20); paragraph1.addToIndexes(); Paragraph paragraph2 = new Paragraph(jCas); paragraph2.setBegin(20); paragraph2.setDepth(2); paragraph2.setEnd(TEXT.length()); paragraph2.addToIndexes(); ItemHierarchy<Structure> structureHierarchy = StructureHierarchy.build(jCas, structuralClasses); SelectorPath path1 = structureHierarchy.getSelectorPath(paragraph1); SelectorPath path2 = structureHierarchy.getSelectorPath(paragraph1); assertEquals("Section:nth-of-type(1)", path1.toDepth(1).toString()); assertEquals("Section:nth-of-type(1)", path2.toDepth(1).toString()); }