Search in sources :

Example 21 with SDocumentGraph

use of org.corpus_tools.salt.common.SDocumentGraph in project ANNIS by korpling.

the class AutoTokQuery method analyzingQuery.

@Override
public void analyzingQuery(SaltProject saltProject) {
    List<SToken> tokens = new ArrayList<>();
    for (SCorpusGraph g : saltProject.getCorpusGraphs()) {
        if (g != null) {
            for (SDocument doc : g.getDocuments()) {
                SDocumentGraph docGraph = doc.getDocumentGraph();
                List<SNode> sNodes = docGraph.getNodes();
                if (sNodes != null) {
                    for (SNode n : sNodes) {
                        if (n instanceof SToken) {
                            tokens.add((SToken) n);
                        }
                    }
                }
            }
        }
    }
    // select one random token from the result
    if (!tokens.isEmpty()) {
        int tries = 10;
        int r = new Random().nextInt(tokens.size() - 1);
        String text = CommonHelper.getSpannedText(tokens.get(r));
        while ("".equals(text) && tries > 0) {
            r = new Random().nextInt(tokens.size() - 1);
            text = CommonHelper.getSpannedText(tokens.get(r));
            tries--;
        }
        if ("".equals(text)) {
            finalAql = null;
        } else {
            finalAql = "\"" + text + "\"";
        }
    }
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SNode(org.corpus_tools.salt.core.SNode) Random(java.util.Random) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) ArrayList(java.util.ArrayList) SDocument(org.corpus_tools.salt.common.SDocument) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph)

Example 22 with SDocumentGraph

use of org.corpus_tools.salt.common.SDocumentGraph in project ANNIS by korpling.

the class CommonHelper method getTokenAnnotationLevelSet.

public static Set<String> getTokenAnnotationLevelSet(SaltProject p) {
    Set<String> result = new TreeSet<String>();
    for (SCorpusGraph corpusGraphs : p.getCorpusGraphs()) {
        for (SDocument doc : corpusGraphs.getDocuments()) {
            SDocumentGraph g = doc.getDocumentGraph();
            result.addAll(getTokenAnnotationLevelSet(g));
        }
    }
    return result;
}
Also used : TreeSet(java.util.TreeSet) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SDocument(org.corpus_tools.salt.common.SDocument) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph)

Example 23 with SDocumentGraph

use of org.corpus_tools.salt.common.SDocumentGraph in project ANNIS by korpling.

the class SaltProjectProvider method writeTo.

@Override
public void writeTo(SaltProject project, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    SaltXML10Writer writer = new SaltXML10Writer();
    try {
        XMLStreamWriter xml = outFactory.createXMLStreamWriter(entityStream, "UTF-8");
        xml.writeStartDocument("1.1");
        xml.writeCharacters("\n");
        long startTime = System.currentTimeMillis();
        // output XMI root element
        writer.writeXMIRootElement(xml);
        for (SCorpusGraph corpusGraph : project.getCorpusGraphs()) {
            for (SDocument doc : corpusGraph.getDocuments()) {
                // make sure that any ANNIS feature on the document is copied to the document graph
                SDocumentGraph docGraph = doc.getDocumentGraph();
                for (SFeature feat : doc.getFeatures()) {
                    if (AnnisConstants.ANNIS_NS.equals(feat.getNamespace())) {
                        SFeature newFeat = SaltFactory.createSFeature();
                        feat.copy(newFeat);
                        docGraph.addFeature(newFeat);
                    }
                }
                writer.writeObjects(xml, docGraph);
            }
        }
        xml.writeEndDocument();
        long endTime = System.currentTimeMillis();
        log.debug("Saving XMI (" + mediaType.toString() + ") needed {} ms", endTime - startTime);
    } catch (XMLStreamException ex) {
        log.error("exception when serializing SDocumentGraph", ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SaltXML10Writer(org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer) SDocument(org.corpus_tools.salt.common.SDocument) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SFeature(org.corpus_tools.salt.core.SFeature)

Example 24 with SDocumentGraph

use of org.corpus_tools.salt.common.SDocumentGraph 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

SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)24 SNode (org.corpus_tools.salt.core.SNode)12 ArrayList (java.util.ArrayList)9 SDocument (org.corpus_tools.salt.common.SDocument)8 SaltProject (org.corpus_tools.salt.common.SaltProject)8 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)7 SToken (org.corpus_tools.salt.common.SToken)7 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 SRelation (org.corpus_tools.salt.core.SRelation)5 RelannisNodeFeature (annis.model.RelannisNodeFeature)4 DataSourceSequence (org.corpus_tools.salt.util.DataSourceSequence)4 TreeSet (java.util.TreeSet)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Pattern (java.util.regex.Pattern)3 SCorpus (org.corpus_tools.salt.common.SCorpus)3 SFeature (org.corpus_tools.salt.core.SFeature)3 Row (annis.gui.widgets.grid.Row)2 IOException (java.io.IOException)2