Search in sources :

Example 6 with SDocument

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

the class VisualizerPanel method createInput.

private VisualizerInput createInput() {
    VisualizerInput input = new VisualizerInput();
    input.setAnnisWebServiceURL((String) VaadinSession.getCurrent().getAttribute("AnnisWebService.URL"));
    input.setContextPath(Helper.getContext());
    input.setDotPath((String) VaadinSession.getCurrent().getAttribute("DotPath"));
    input.setId(resultID);
    input.setMarkableExactMap(markersExact);
    input.setMarkableMap(markersCovered);
    input.setMarkedAndCovered(markedAndCovered);
    input.setResult(result);
    input.setVisibleTokenAnnos(visibleTokenAnnos);
    input.setSegmentationName(segmentationName);
    if (instanceConfig != null && instanceConfig.getFont() != null) {
        input.setFont(instanceConfig.getFont());
    }
    if (entry != null) {
        input.setMappings(entry.getMappings());
        input.setNamespace(entry.getNamespace());
        String template = Helper.getContext() + "/Resource/" + entry.getVisType() + "/%s";
        input.setResourcePathTemplate(template);
    }
    // getting the whole document, when plugin is using text
    if (visPlugin != null && visPlugin.isUsingText() && result != null && result.getDocumentGraph().getNodes().size() > 0) {
        List<String> nodeAnnoFilter = null;
        if (visPlugin instanceof FilteringVisualizerPlugin) {
            nodeAnnoFilter = ((FilteringVisualizerPlugin) visPlugin).getFilteredNodeAnnotationNames(corpusName, documentName, input.getMappings());
        }
        SaltProject p = getDocument(result.getGraph().getRoots().get(0).getName(), result.getName(), nodeAnnoFilter);
        SDocument wholeDocument = p.getCorpusGraphs().get(0).getDocuments().get(0);
        Helper.addMatchToDocumentGraph(match, wholeDocument);
        input.setDocument(wholeDocument);
    } else {
        input.setDocument(result);
    }
    // getting the raw text, when the visualizer wants to have it
    if (visPlugin != null && visPlugin.isUsingRawText()) {
        input.setRawText(Helper.getRawText(corpusName, documentName));
    }
    return input;
}
Also used : VisualizerInput(annis.libgui.visualizers.VisualizerInput) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) FilteringVisualizerPlugin(annis.libgui.visualizers.FilteringVisualizerPlugin)

Example 7 with SDocument

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

the class EmbeddedVisUI method generateVisFromRemoteURL.

private void generateVisFromRemoteURL(final String visName, final String rawUri, Map<String, String[]> args) {
    try {
        // find the matching visualizer
        final VisualizerPlugin visPlugin = this.getVisualizer(visName);
        if (visPlugin == null) {
            displayMessage("Unknown visualizer \"" + visName + "\"", "This ANNIS instance does not know the given visualizer.");
            return;
        }
        URI uri = new URI(rawUri);
        // fetch content of the URI
        Client client = null;
        AnnisUser user = Helper.getUser();
        if (user != null) {
            client = user.getClient();
        }
        if (client == null) {
            client = Helper.createRESTClient();
        }
        final WebResource saltRes = client.resource(uri);
        displayLoadingIndicator();
        // copy the arguments for using them later in the callback
        final Map<String, String[]> argsCopy = new LinkedHashMap<>(args);
        Background.runWithCallback(new Callable<SaltProject>() {

            @Override
            public SaltProject call() throws Exception {
                return saltRes.get(SaltProject.class);
            }
        }, new FutureCallback<SaltProject>() {

            @Override
            public void onFailure(Throwable t) {
                displayMessage("Could not query the result.", t.getMessage());
            }

            @Override
            public void onSuccess(SaltProject p) {
                // TODO: allow to display several visualizers when there is more than one document
                SCorpusGraph firstCorpusGraph = null;
                SDocument doc = null;
                if (p.getCorpusGraphs() != null && !p.getCorpusGraphs().isEmpty()) {
                    firstCorpusGraph = p.getCorpusGraphs().get(0);
                    if (firstCorpusGraph.getDocuments() != null && !firstCorpusGraph.getDocuments().isEmpty()) {
                        doc = firstCorpusGraph.getDocuments().get(0);
                    }
                }
                if (doc == null) {
                    displayMessage("No documents found in provided URL.", "");
                    return;
                }
                if (argsCopy.containsKey(KEY_INSTANCE)) {
                    Map<String, InstanceConfig> allConfigs = loadInstanceConfig();
                    InstanceConfig newConfig = allConfigs.get(argsCopy.get(KEY_INSTANCE)[0]);
                    if (newConfig != null) {
                        setInstanceConfig(newConfig);
                    }
                }
                // now it is time to load the actual defined instance fonts
                loadInstanceFonts();
                // generate the visualizer
                VisualizerInput visInput = new VisualizerInput();
                visInput.setDocument(doc);
                if (getInstanceConfig() != null && getInstanceConfig().getFont() != null) {
                    visInput.setFont(getInstanceFont());
                }
                Properties mappings = new Properties();
                for (Map.Entry<String, String[]> e : argsCopy.entrySet()) {
                    if (!KEY_SALT.equals(e.getKey()) && e.getValue().length > 0) {
                        mappings.put(e.getKey(), e.getValue()[0]);
                    }
                }
                visInput.setMappings(mappings);
                String[] namespace = argsCopy.get(KEY_NAMESPACE);
                if (namespace != null && namespace.length > 0) {
                    visInput.setNamespace(namespace[0]);
                } else {
                    visInput.setNamespace(null);
                }
                String baseText = null;
                if (argsCopy.containsKey(KEY_BASE_TEXT)) {
                    String[] value = argsCopy.get(KEY_BASE_TEXT);
                    if (value.length > 0) {
                        baseText = value[0];
                    }
                }
                List<SNode> segNodes = CommonHelper.getSortedSegmentationNodes(baseText, doc.getDocumentGraph());
                if (argsCopy.containsKey(KEY_MATCH)) {
                    String[] rawMatch = argsCopy.get(KEY_MATCH);
                    if (rawMatch.length > 0) {
                        // enhance the graph with match information from the arguments
                        Match match = Match.parseFromString(rawMatch[0]);
                        Helper.addMatchToDocumentGraph(match, doc);
                    }
                }
                Map<String, String> markedColorMap = new HashMap<>();
                Map<String, String> exactMarkedMap = Helper.calculateColorsForMarkedExact(doc);
                Map<String, Long> markedAndCovered = Helper.calculateMarkedAndCoveredIDs(doc, segNodes, baseText);
                Helper.calulcateColorsForMarkedAndCovered(doc, markedAndCovered, markedColorMap);
                visInput.setMarkedAndCovered(markedAndCovered);
                visInput.setMarkableMap(markedColorMap);
                visInput.setMarkableExactMap(exactMarkedMap);
                visInput.setContextPath(Helper.getContext());
                String template = Helper.getContext() + "/Resource/" + visName + "/%s";
                visInput.setResourcePathTemplate(template);
                visInput.setSegmentationName(baseText);
                // TODO: which other thing do we have to provide?
                Component c = visPlugin.createComponent(visInput, null);
                // add the styles
                c.addStyleName("corpus-font");
                c.addStyleName("vis-content");
                Link link = new Link();
                link.setCaption("Show in ANNIS search interface");
                link.setIcon(ANNISFontIcon.LOGO);
                link.setVisible(false);
                link.addStyleName("dontprint");
                link.setTargetName("_blank");
                if (argsCopy.containsKey(KEY_SEARCH_INTERFACE)) {
                    String[] interfaceLink = argsCopy.get(KEY_SEARCH_INTERFACE);
                    if (interfaceLink.length > 0) {
                        link.setResource(new ExternalResource(interfaceLink[0]));
                        link.setVisible(true);
                    }
                }
                VerticalLayout layout = new VerticalLayout(link, c);
                layout.setComponentAlignment(link, Alignment.TOP_LEFT);
                layout.setSpacing(true);
                layout.setMargin(true);
                setContent(layout);
                IDGenerator.assignID(link);
            }
        });
    } catch (URISyntaxException ex) {
        displayMessage("Invalid URL", "The provided URL is malformed:<br />" + ex.getMessage());
    } catch (LoginDataLostException ex) {
        displayMessage("LoginData Lost", "No login data available any longer in the session:<br /> " + ex.getMessage());
    } catch (UniformInterfaceException ex) {
        if (ex.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()) {
            displayMessage("Corpus access forbidden", "You are not allowed to access this corpus. " + "Please login at the <a target=\"_blank\" href=\"" + Helper.getContext() + "\">main application</a> first and then reload this page.");
        } else {
            displayMessage("Service error", ex.getMessage());
        }
    } catch (ClientHandlerException ex) {
        displayMessage("Could not generate the visualization because the ANNIS service reported an error.", ex.getMessage());
    } catch (Throwable ex) {
        displayMessage("Could not generate the visualization.", ex.getMessage() == null ? ("An unknown error of type " + ex.getClass().getSimpleName()) + " occured." : ex.getMessage());
    }
}
Also used : VisualizerPlugin(annis.libgui.visualizers.VisualizerPlugin) WebResource(com.sun.jersey.api.client.WebResource) URISyntaxException(java.net.URISyntaxException) Properties(java.util.Properties) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) Match(annis.service.objects.Match) InstanceConfig(annis.libgui.InstanceConfig) VerticalLayout(com.vaadin.ui.VerticalLayout) List(java.util.List) LinkedList(java.util.LinkedList) Client(com.sun.jersey.api.client.Client) Component(com.vaadin.ui.Component) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) ExternalResource(com.vaadin.server.ExternalResource) AnnisUser(annis.libgui.AnnisUser) URISyntaxException(java.net.URISyntaxException) LoginDataLostException(annis.libgui.LoginDataLostException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) VisualizerInput(annis.libgui.visualizers.VisualizerInput) LoginDataLostException(annis.libgui.LoginDataLostException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Link(com.vaadin.ui.Link)

Example 8 with SDocument

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

the class DocBrowserController method createInput.

/**
 * Creates the input. It only takes the salt project or the raw text from the
 * text table, never both, since the increase the performance for large texts.
 *
 * @param corpus the name of the toplevel corpus
 * @param docName the name of the document
 * @param config the visualizer configuration
 * @param isUsingRawText indicates, whether the text from text table is taken,
 * or if the salt project is traversed.
 * @param nodeAnnoFilter A list of node annotation names for filtering the nodes or null if no filtering should be applied.
 * @return a {@link VisualizerInput} input, which is usable for rendering the
 * whole document.
 */
public static VisualizerInput createInput(String corpus, String docName, Visualizer config, boolean isUsingRawText, List<String> nodeAnnoFilter) {
    VisualizerInput input = new VisualizerInput();
    // set mappings and namespaces. some visualizer do not survive without
    input.setMappings(parseMappings(config));
    input.setNamespace(config.getNamespace());
    String encodedToplevelCorpus = urlPathEscape.escape(corpus);
    String encodedDocument = urlPathEscape.escape(docName);
    if (isUsingRawText) {
        WebResource w = Helper.getAnnisWebResource();
        w = w.path("query").path("rawtext").path(encodedToplevelCorpus).path(encodedDocument);
        RawTextWrapper rawTextWrapper = w.get(RawTextWrapper.class);
        input.setRawText(rawTextWrapper);
    } else {
        // get the whole document wrapped in a salt project
        SaltProject txt = null;
        WebResource res = Helper.getAnnisWebResource().path("query").path("graph").path(encodedToplevelCorpus).path(encodedDocument);
        if (nodeAnnoFilter != null) {
            res = res.queryParam("filternodeanno", Joiner.on(",").join(nodeAnnoFilter));
        }
        txt = res.get(SaltProject.class);
        if (txt != null) {
            SDocument sDoc = txt.getCorpusGraphs().get(0).getDocuments().get(0);
            input.setResult(sDoc);
        }
    }
    return input;
}
Also used : VisualizerInput(annis.libgui.visualizers.VisualizerInput) RawTextWrapper(annis.service.objects.RawTextWrapper) WebResource(com.sun.jersey.api.client.WebResource) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject)

Example 9 with SDocument

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

the class SaltAnnotateExtractor method extractData.

@Override
public SaltProject extractData(ResultSet resultSet) throws SQLException, DataAccessException {
    SaltProject project = SaltFactory.createSaltProject();
    try {
        SCorpusGraph corpusGraph = null;
        SDocumentGraph graph = null;
        // fn: parent information (pre and component) id to node
        FastInverseMap<Long, SNode> nodeByRankID = new FastInverseMap<>();
        TreeSet<Long> allTextIDs = new TreeSet<>();
        TreeMap<Long, String> tokenTexts = new TreeMap<>();
        TreeMap<Long, SToken> tokenByIndex = new TreeMap<>();
        TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath = new TreeMap<>();
        Map<String, ComponentEntry> componentForSpan = new HashMap<>();
        // clear mapping functions for this graph
        // assumes that the result set is sorted by key, pre
        nodeByRankID.clear();
        SDocument document = null;
        AtomicInteger numberOfRelations = new AtomicInteger();
        int match_index = 0;
        SolutionKey<?> key = createSolutionKey();
        int counter = 0;
        while (resultSet.next()) {
            if (counter % 1000 == 0) {
                log.debug("handling resultset row {}", counter);
            }
            counter++;
            // List<String> annotationGraphKey =
            key.retrieveKey(resultSet);
            if (key.isNewKey()) {
                // create the text for the last graph
                if (graph != null && document != null) {
                    createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
                    createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
                    addOrderingRelations(graph, nodeBySegmentationPath);
                }
                // new match, reset everything
                nodeByRankID.clear();
                tokenTexts.clear();
                tokenByIndex.clear();
                componentForSpan.clear();
                Integer matchstart = resultSet.getInt("matchstart");
                corpusGraph = SaltFactory.createSCorpusGraph();
                corpusGraph.setName("match_" + (match_index + matchstart));
                project.addCorpusGraph(corpusGraph);
                graph = SaltFactory.createSDocumentGraph();
                document = SaltFactory.createSDocument();
                document.setDocumentGraphLocation(org.eclipse.emf.common.util.URI.createFileURI(Files.createTempDir().getAbsolutePath()));
                List<String> path = corpusPathExtractor.extractCorpusPath(resultSet, "path");
                SCorpus toplevelCorpus = SaltFactory.createSCorpus();
                toplevelCorpus.setName(path.get(0));
                corpusGraph.addNode(toplevelCorpus);
                Validate.isTrue(path.size() >= 2, "Corpus path must be have at least two members (toplevel and document)");
                SCorpus corpus = toplevelCorpus;
                for (int i = 1; i < path.size() - 1; i++) {
                    SCorpus subcorpus = SaltFactory.createSCorpus();
                    subcorpus.setName(path.get(i));
                    corpusGraph.addSubCorpus(corpus, subcorpus);
                    corpus = subcorpus;
                }
                document.setName(path.get(path.size() - 1));
                document.setId("" + match_index);
                corpusGraph.addDocument(corpus, document);
                document.setDocumentGraph(graph);
                match_index++;
            }
            // end if new key
            // get node data
            SNode node = createOrFindNewNode(resultSet, graph, allTextIDs, tokenTexts, tokenByIndex, nodeBySegmentationPath, key, nodeByRankID);
            long rankID = longValue(resultSet, RANK_TABLE, "id");
            long componentID = longValue(resultSet, COMPONENT_TABLE, "id");
            if (!resultSet.wasNull()) {
                nodeByRankID.put(rankID, node);
                createRelation(resultSet, graph, nodeByRankID, node, numberOfRelations);
                if (node instanceof SSpan) {
                    componentForSpan.put(node.getId(), new ComponentEntry(componentID, 'c', stringValue(resultSet, COMPONENT_TABLE, "namespace"), stringValue(resultSet, COMPONENT_TABLE, "name")));
                }
            }
        }
        // the last match needs a primary text, too
        if (graph != null) {
            createMissingSpanningRelations(graph, nodeByRankID, tokenByIndex, componentForSpan, numberOfRelations);
            createPrimaryTexts(graph, allTextIDs, tokenTexts, tokenByIndex);
            addOrderingRelations(graph, nodeBySegmentationPath);
        }
    } catch (Exception ex) {
        log.error("could not map result set to SaltProject", ex);
    }
    return project;
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SSpan(org.corpus_tools.salt.common.SSpan) HashMap(java.util.HashMap) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SToken(org.corpus_tools.salt.common.SToken) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) TreeSet(java.util.TreeSet) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) TreeMap(java.util.TreeMap) SaltException(org.corpus_tools.salt.exceptions.SaltException) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SCorpus(org.corpus_tools.salt.common.SCorpus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with SDocument

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

Aggregations

SDocument (org.corpus_tools.salt.common.SDocument)15 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)12 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)8 SaltProject (org.corpus_tools.salt.common.SaltProject)6 SNode (org.corpus_tools.salt.core.SNode)6 LinkedList (java.util.LinkedList)4 SToken (org.corpus_tools.salt.common.SToken)4 VisualizerInput (annis.libgui.visualizers.VisualizerInput)3 Match (annis.service.objects.Match)3 HashMap (java.util.HashMap)3 SCorpus (org.corpus_tools.salt.common.SCorpus)3 WebResource (com.sun.jersey.api.client.WebResource)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Random (java.util.Random)2 TreeSet (java.util.TreeSet)2 SRelation (org.corpus_tools.salt.core.SRelation)2 AnnisUser (annis.libgui.AnnisUser)1 InstanceConfig (annis.libgui.InstanceConfig)1 LoginDataLostException (annis.libgui.LoginDataLostException)1