Search in sources :

Example 36 with SNode

use of org.corpus_tools.salt.core.SNode in project ANNIS by korpling.

the class TimelineReconstructor method createTokenFromSOrder.

private void createTokenFromSOrder() {
    nodesToDelete.add(graph.getTextualDSs().get(0));
    Map<String, SSpan> rootNodes = new HashMap<>();
    // also add nodes that are are marked as start by ANNIS even if they don't have an outgoing order rel
    for (SSpan n : graph.getSpans()) {
        SFeature feat = n.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
        if (feat != null && feat.getValue_STEXT() != null) {
            rootNodes.put(feat.getValue_STEXT(), n);
        } else {
            // check if there is no incoming SOrderRelation but an outgoing
            boolean isRoot = true;
            for (SRelation<?, ?> inRel : n.getInRelations()) {
                if (inRel instanceof SOrderRelation) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                for (SRelation<?, ?> outRel : n.getOutRelations()) {
                    if (outRel instanceof SOrderRelation) {
                        rootNodes.put(((SOrderRelation) outRel).getType(), n);
                        break;
                    }
                }
            }
        }
    }
    // convert all root nodes to spans
    for (Map.Entry<String, SSpan> rootEntry : rootNodes.entrySet()) {
        SNode root = rootEntry.getValue();
        String orderName = rootEntry.getKey();
        convertSpanToToken((SSpan) root, orderName);
    }
    // traverse through all SOrderRelations in order
    graph.traverse(new LinkedList<SNode>(rootNodes.values()), GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "TimeReconstructSOrderRelations", new GraphTraverseHandler() {

        @Override
        public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
            if (relation instanceof SOrderRelation && currNode instanceof SSpan) {
                String orderName = ((SOrderRelation) relation).getType();
                if (fromNode != null) {
                    // add a space to the text
                    StringBuilder t = textDataByName.get(orderName);
                    if (t != null) {
                        t.append(" ");
                    }
                }
                convertSpanToToken((SSpan) currNode, orderName);
            }
        }

        @Override
        public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
        }

        @Override
        public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation relation, SNode currNode, long order) {
            if (relation == null || relation instanceof SOrderRelation) {
                return true;
            } else {
                return false;
            }
        }
    });
    // update the text of the TextualDSs
    for (Map.Entry<String, StringBuilder> textDataEntry : textDataByName.entrySet()) {
        STextualDS textDS = textsByName.get(textDataEntry.getKey());
        if (textDS != null) {
            textDS.setText(textDataEntry.getValue().toString());
        }
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SSpan(org.corpus_tools.salt.common.SSpan) HashMap(java.util.HashMap) GRAPH_TRAVERSE_TYPE(org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE) GraphTraverseHandler(org.corpus_tools.salt.core.GraphTraverseHandler) SRelation(org.corpus_tools.salt.core.SRelation) STextualDS(org.corpus_tools.salt.common.STextualDS) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) HashMap(java.util.HashMap) Map(java.util.Map) SFeature(org.corpus_tools.salt.core.SFeature)

Example 37 with SNode

use of org.corpus_tools.salt.core.SNode in project ANNIS by korpling.

the class SaltProjectProvider method readFrom.

@Override
public SaltProject readFrom(Class<SaltProject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    SaltProject result = SaltFactory.createSaltProject();
    SAXParser parser;
    XMLReader xmlReader;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    MixedContentHandler handler = new MixedContentHandler();
    try {
        parser = factory.newSAXParser();
        xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(handler);
        InputSource source = new InputSource(entityStream);
        source.setEncoding("UTF-8");
        xmlReader.parse(source);
        for (SDocumentGraph g : handler.getDocGraphs()) {
            // create a separate corpus graph for each document
            SCorpusGraph corpusGraph = SaltFactory.createSCorpusGraph();
            SCorpus parentCorpus = null;
            SDocument doc = null;
            List<SNode> nodes = g.getNodes();
            Iterator<String> it;
            if (nodes != null && !nodes.isEmpty()) {
                // the path of each node ID is always the document/corpus path
                it = nodes.get(0).getPath().segmentsList().iterator();
            } else {
                // Old salt versions had a separate ID for the document graph
                // which was the document name with the suffix "_graph".
                // Thus this method of getting the corpus path is only the fallback.
                it = g.getPath().segmentsList().iterator();
            }
            while (it.hasNext()) {
                String name = it.next();
                if (it.hasNext()) {
                    // this is a sub-corpus
                    parentCorpus = corpusGraph.createCorpus(parentCorpus, name);
                } else {
                    // no more path elements left, must be a document
                    doc = corpusGraph.createDocument(parentCorpus, name);
                    break;
                }
            }
            if (doc != null) {
                result.addCorpusGraph(corpusGraph);
                doc.setDocumentGraph(g);
            }
        }
    } catch (ParserConfigurationException | SAXException ex) {
        log.error("Error when parsing XMI", ex);
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) SNode(org.corpus_tools.salt.core.SNode) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SAXException(org.xml.sax.SAXException) SCorpus(org.corpus_tools.salt.common.SCorpus) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SNode (org.corpus_tools.salt.core.SNode)37 SToken (org.corpus_tools.salt.common.SToken)15 SRelation (org.corpus_tools.salt.core.SRelation)14 ArrayList (java.util.ArrayList)13 LinkedList (java.util.LinkedList)12 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)12 SFeature (org.corpus_tools.salt.core.SFeature)12 RelannisNodeFeature (annis.model.RelannisNodeFeature)11 HashMap (java.util.HashMap)11 SAnnotation (org.corpus_tools.salt.core.SAnnotation)8 Map (java.util.Map)6 SDocument (org.corpus_tools.salt.common.SDocument)6 HashSet (java.util.HashSet)5 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)5 Annotation (annis.model.Annotation)4 TreeMap (java.util.TreeMap)4 SCorpus (org.corpus_tools.salt.common.SCorpus)4 SaltProject (org.corpus_tools.salt.common.SaltProject)4 SLayer (org.corpus_tools.salt.core.SLayer)4 JSONException (org.json.JSONException)4