/** * Get the selector path of the structure annotation covering the given annotation * * @param annotation the annotation * @return the selector path in this hierarchy covering the given annotation */ public SelectorPath generatePath(Annotation annotation) { Optional<Structure> covering = getCoveringStructure(annotation); Node<Structure> node = getRoot(); if (covering.isPresent()) { node = getIndex().get(covering.get()); } return new SelectorPath(node.toPath()); }
/** * Build the covering structure hierarchy for the given jCas, using only the structural classes * provided. * * <p>The structure is built by first using the offset of the Structure annotation and then using * the depth. * * @param jCas the jCas * @param structuralClasses the structural classes * @return the StructureHierachy */ public static CoveringStructureHierarchy build( JCas jCas, Set<Class<? extends Structure>> structuralClasses) { Node<Structure> root = StructureHierarchy.build(jCas, structuralClasses).getRoot(); Map<Annotation, Collection<Structure>> covering = buildCovering(jCas, structuralClasses); return new CoveringStructureHierarchy(root, covering); }
.max(Comparator.comparingInt(Structure::getDepth)); Optional<Structure> coveringStructure = structureHierarchy.getCoveringStructure(recordDefinition); Optional<Structure> endStructure = JCasUtil.selectCovering( Optional<Structure> preceding = structureHierarchy.getPrevious(startStructure.get()); Optional<Structure> following = structureHierarchy.getNext(endStructure.get()); preceding.isPresent() ? structureHierarchy.getSelectorPath(preceding.get()).toString() : ""; following.isPresent() ? structureHierarchy.getSelectorPath(following.get()).toString() : ""; if (coveringStructure.isPresent() && coveredStructures.contains(coveringStructure.get())) { Structure repeatingUnit = coveringStructure.get(); minimalRepeat = structureHierarchy.getSelectorPath(repeatingUnit).toString();
/** * Make fields from definitions and look up the location in the structure * * @param structureHierarchy the structure hierarchy * @param fields the fields * @return the list of configurations */ private List<TemplateFieldConfiguration> makeFields( final CoveringStructureHierarchy structureHierarchy, Collection<TemplateFieldDefinition> definitions) { List<TemplateFieldConfiguration> fields = new ArrayList<>(); for (TemplateFieldDefinition templateFieldDefinition : definitions) { String fieldPath = structureHierarchy.generatePath(templateFieldDefinition).toString(); TemplateFieldConfiguration field = makeField(templateFieldDefinition, fieldPath); fields.add(field); } return fields; }
@Override protected void doProcess(final JCas jCas) throws AnalysisEngineProcessException { CoveringStructureHierarchy structureHierarchy = CoveringStructureHierarchy.build(jCas, structuralClasses); Collection<TemplateRecordDefinition> recordDefinitions = JCasUtil.select(jCas, TemplateRecordDefinition.class); Collection<TemplateFieldDefinition> fieldDefinitions = new HashSet<>(JCasUtil.select(jCas, TemplateFieldDefinition.class)); Map<String, TemplateRecordConfiguration> recordConfigurations = new HashMap<>(); for (TemplateRecordDefinition recordDefinition : recordDefinitions) { processRecordDefinition( jCas, structureHierarchy, fieldDefinitions, recordConfigurations, recordDefinition); } List<TemplateRecordConfiguration> configurations = new ArrayList<>(recordConfigurations.values()); if (!fieldDefinitions.isEmpty()) { for (TemplateFieldDefinition field : fieldDefinitions) { configurations.add( new TemplateRecordConfiguration( makeFields(structureHierarchy, ImmutableList.of(field)), field.getBegin())); } } String documentSourceName = SourceUtils.getDocumentSourceBaseName(jCas); try (Writer w = createOutputWriter(documentSourceName)) { Collections.sort(configurations, Comparator.comparing(TemplateRecordConfiguration::getOrder)); objectMapper.writeValue(w, configurations); } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }
/** * Get the selector path of the structure annotation covering the given annotation * * @param annotation the annotation * @return the selector path in this hierarchy covering the given annotation */ public SelectorPath generatePath(Annotation annotation) { Optional<Structure> covering = getCoveringStructure(annotation); Node<Structure> node = getRoot(); if (covering.isPresent()) { node = getIndex().get(covering.get()); } return new SelectorPath(node.toPath()); }
/** * Build the covering structure hierarchy for the given jCas, using only the structural classes * provided. * * <p>The structure is built by first using the offset of the Structure annotation and then using * the depth. * * @param jCas the jCas * @param structuralClasses the structural classes * @return the StructureHierachy */ public static CoveringStructureHierarchy build( JCas jCas, Set<Class<? extends Structure>> structuralClasses) { Node<Structure> root = StructureHierarchy.build(jCas, structuralClasses).getRoot(); Map<Annotation, Collection<Structure>> covering = buildCovering(jCas, structuralClasses); return new CoveringStructureHierarchy(root, covering); }