Search in sources :

Example 1 with Annotation

use of org.corpus_tools.annis.api.model.Annotation in project ANNIS by korpling.

the class MetaDataPanel method setupTable.

private Grid<Annotation> setupTable(ListDataProvider<Annotation> metaData) {
    ValueProvider<Annotation, String> nameProvider = anno -> Helper.getQName(anno.getKey());
    metaData.setSortOrder(nameProvider, SortDirection.ASCENDING);
    Grid<Annotation> tblMeta = new Grid<>(Annotation.class);
    tblMeta.setDataProvider(metaData);
    Column<Annotation, String> nameColumn = tblMeta.addColumn(nameProvider);
    nameColumn.setWidthUndefined();
    nameColumn.setCaption("Name");
    nameColumn.setId("genname");
    Column<Annotation, ?> valueColumn = tblMeta.addComponentColumn(anno -> new Label(anno.getVal(), ContentMode.HTML));
    valueColumn.setId("genval");
    valueColumn.setCaption("Value");
    tblMeta.setColumns(nameColumn.getId(), valueColumn.getId());
    tblMeta.setSizeFull();
    valueColumn.setExpandRatio(1);
    return tblMeta;
}
Also used : Panel(com.vaadin.ui.Panel) ValueProvider(com.vaadin.data.ValueProvider) ListDataProvider(com.vaadin.data.provider.ListDataProvider) ContentMode(com.vaadin.shared.ui.ContentMode) Column(com.vaadin.ui.Grid.Column) VerticalLayout(com.vaadin.ui.VerticalLayout) Collection(java.util.Collection) Alignment(com.vaadin.ui.Alignment) UI(com.vaadin.ui.UI) SearchApi(org.corpus_tools.annis.api.SearchApi) ExceptionDialog(org.corpus_tools.annis.gui.components.ExceptionDialog) SortDirection(com.vaadin.shared.data.sort.SortDirection) FutureCallback(com.google.common.util.concurrent.FutureCallback) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) List(java.util.List) AnnoKey(org.corpus_tools.annis.api.model.AnnoKey) Accordion(com.vaadin.ui.Accordion) Label(com.vaadin.ui.Label) Optional(java.util.Optional) LinkedList(java.util.LinkedList) ProgressBar(com.vaadin.ui.ProgressBar) Annotation(org.corpus_tools.annis.api.model.Annotation) Grid(com.vaadin.ui.Grid) Grid(com.vaadin.ui.Grid) Label(com.vaadin.ui.Label) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) Annotation(org.corpus_tools.annis.api.model.Annotation)

Example 2 with Annotation

use of org.corpus_tools.annis.api.model.Annotation in project ANNIS by korpling.

the class CorpusBrowserPanel method fetchAnnotationsInBackground.

private void fetchAnnotationsInBackground(UI ui) {
    CorporaApi api = new CorporaApi(Helper.getClient(ui));
    try {
        final List<Annotation> nodeAnnos = api.nodeAnnotations(corpus, true, true).stream().filter(a -> !Objects.equals(a.getKey().getNs(), "annis") && !Objects.equals(a.getKey().getName(), "tok")).collect(Collectors.toList());
        final List<Annotation> metaAnnos = new LinkedList<>(nodeAnnos);
        final Set<AnnoKey> metaAnnoKeys = Helper.getMetaAnnotationNames(corpus, ui);
        nodeAnnos.removeIf(anno -> metaAnnoKeys.contains(anno.getKey()));
        metaAnnos.removeIf(anno -> !metaAnnoKeys.contains(anno.getKey()));
        final List<Component> components = api.components(corpus, "Dominance", null);
        final List<Annotation> allEdgeAnnos = new LinkedList<>();
        final Map<Component, List<Annotation>> edgeAnnosByComponent = new LinkedHashMap<>();
        components.addAll(api.components(corpus, "Pointing", null));
        for (Component c : components) {
            try {
                List<Annotation> annos = api.edgeAnnotations(corpus, c.getType().getValue(), c.getLayer(), c.getName(), true, true);
                edgeAnnosByComponent.put(c, annos);
                allEdgeAnnos.addAll(annos);
            } catch (ApiException ex) {
            // Ignore any not found errors
            }
        }
        getUI().access(() -> {
            TreeSet<CorpusBrowserEntry> nodeAnnoItems = new TreeSet<>();
            TreeSet<CorpusBrowserEntry> edgeAnnoItems = new TreeSet<>();
            TreeSet<CorpusBrowserEntry> edgeTypeItems = new TreeSet<>();
            TreeSet<CorpusBrowserEntry> metaAnnoItems = new TreeSet<>();
            progress.setVisible(false);
            accordion.setVisible(true);
            boolean stripNodeAnno = canExcludeNamespace(nodeAnnos);
            boolean stripEdgeName = canExcludeNamespace(allEdgeAnnos);
            boolean stripEdgeAnno = true;
            HashSet<String> nodeAnnoNames = new HashSet<>();
            HashSet<String> edgeAnnoNames = new HashSet<>();
            HashSet<String> edgeNames = new HashSet<>();
            boolean hasDominance = false;
            boolean hasEmptyDominance = false;
            // do some preparations first
            for (Annotation a : nodeAnnos) {
                // check for ambiguous names
                if (!nodeAnnoNames.add(a.getKey().getName())) {
                    stripNodeAnno = false;
                }
            }
            for (Component c : components) {
                // check if collected edge names are unique
                if (!edgeNames.add(Helper.getQName(c))) {
                    stripEdgeName = false;
                }
                // check if we need to add the general dominance example edge
                if (c.getType() == AnnotationComponentType.DOMINANCE) {
                    hasDominance = true;
                    if (c.getName() == null || c.getName().isEmpty()) {
                        hasEmptyDominance = true;
                    }
                }
            }
            for (List<Annotation> annos : edgeAnnosByComponent.values()) {
                for (Annotation a : annos) {
                    // check for ambiguous names
                    if (!edgeAnnoNames.add(a.getKey().getName())) {
                        stripEdgeAnno = false;
                    }
                }
            }
            // fill the actual containers
            for (Annotation a : nodeAnnos) {
                String name = stripNodeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
                CorpusBrowserEntry cbe = new CorpusBrowserEntry();
                cbe.setName(name);
                cbe.setExample(name + "=\"" + a.getVal() + "\"");
                cbe.setCorpus(corpus);
                nodeAnnoItems.add(cbe);
            }
            // edge type entry
            if (hasDominance && !hasEmptyDominance) {
                CorpusBrowserEntry cbe = new CorpusBrowserEntry();
                cbe.setName("(dominance)");
                cbe.setCorpus(corpus);
                cbe.setExample("node & node & #1 > #2");
                edgeTypeItems.add(cbe);
            }
            for (Component c : components) {
                CorpusBrowserEntry cbeEdgeType = new CorpusBrowserEntry();
                String name = stripEdgeName ? c.getName() : Helper.getQName(c);
                if ((name == null || name.isEmpty()) && c.getType() == AnnotationComponentType.DOMINANCE) {
                    cbeEdgeType.setName("(dominance)");
                } else {
                    cbeEdgeType.setName(name);
                }
                cbeEdgeType.setCorpus(corpus);
                if (c.getType() == AnnotationComponentType.POINTING) {
                    cbeEdgeType.setExample("node & node & #1 ->" + c.getName() + " #2");
                } else if (c.getType() == AnnotationComponentType.DOMINANCE) {
                    cbeEdgeType.setExample("node & node & #1 >" + c.getName() + " #2");
                }
                edgeTypeItems.add(cbeEdgeType);
            }
            // edge annotation entries
            for (Map.Entry<Component, List<Annotation>> entry : edgeAnnosByComponent.entrySet()) {
                Component c = entry.getKey();
                for (Annotation a : entry.getValue()) {
                    CorpusBrowserEntry cbeEdgeAnno = new CorpusBrowserEntry();
                    String edgeAnno = stripEdgeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
                    cbeEdgeAnno.setName(edgeAnno);
                    cbeEdgeAnno.setCorpus(corpus);
                    if (c.getType() == AnnotationComponentType.POINTING) {
                        cbeEdgeAnno.setExample("node & node & #1 ->" + c.getName() + "[" + a.getKey().getName() + "=\"" + a.getVal() + "\"] #2");
                    } else if (c.getType() == AnnotationComponentType.DOMINANCE) {
                        cbeEdgeAnno.setExample("node & node & #1 >[" + a.getKey().getName() + "=\"" + a.getVal() + "\"] #2");
                    }
                    edgeAnnoItems.add(cbeEdgeAnno);
                }
            }
            boolean stripMetaName = canExcludeNamespace(metaAnnos);
            for (Annotation a : nodeAnnos) {
                String name = stripMetaName ? a.getKey().getName() : Helper.getQName(a.getKey());
                CorpusBrowserEntry cbe = new CorpusBrowserEntry();
                cbe.setName(name);
                cbe.setExample(name + "=\"" + a.getVal() + "\"");
                cbe.setCorpus(corpus);
                nodeAnnoItems.add(cbe);
            }
            for (Annotation a : metaAnnos) {
                String name = stripNodeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
                CorpusBrowserEntry cbe = new CorpusBrowserEntry();
                cbe.setName(name);
                cbe.setExample(name + "=\"" + a.getVal() + "\"");
                cbe.setCorpus(corpus);
                metaAnnoItems.add(cbe);
            }
            lblNoNodeAnno.setVisible(nodeAnnoItems.isEmpty());
            tblNodeAnno.setVisible(!nodeAnnoItems.isEmpty());
            tblNodeAnno.setItems(new ArrayList<>(nodeAnnoItems));
            lblNoEdgeAnno.setVisible(edgeAnnoItems.isEmpty());
            tblEdgeAnno.setVisible(!edgeAnnoItems.isEmpty());
            tblEdgeAnno.setItems(edgeAnnoItems);
            lblNoEdgeTypes.setVisible(edgeTypeItems.isEmpty());
            tblEdgeTypes.setVisible(!edgeTypeItems.isEmpty());
            tblEdgeTypes.setItems(edgeTypeItems);
            lblNoMetaAnno.setVisible(metaAnnoItems.isEmpty());
            tblMetaAnno.setVisible(!metaAnnoItems.isEmpty());
            tblMetaAnno.setItems(metaAnnoItems);
        });
    } catch (ApiException e) {
        getUI().access(() -> {
            ExceptionDialog.show(e, "Error fetching corpus annotations", getUI());
        });
    }
}
Also used : Panel(com.vaadin.ui.Panel) SelectionListener(com.vaadin.event.selection.SelectionListener) VerticalLayout(com.vaadin.ui.VerticalLayout) SelectionEvent(com.vaadin.event.selection.SelectionEvent) Alignment(com.vaadin.ui.Alignment) UI(com.vaadin.ui.UI) ExceptionDialog(org.corpus_tools.annis.gui.components.ExceptionDialog) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) AnnoKey(org.corpus_tools.annis.api.model.AnnoKey) CorpusBrowserEntry(org.corpus_tools.annis.gui.beans.CorpusBrowserEntry) Accordion(com.vaadin.ui.Accordion) Label(com.vaadin.ui.Label) Map(java.util.Map) LinkedList(java.util.LinkedList) ProgressBar(com.vaadin.ui.ProgressBar) CorporaApi(org.corpus_tools.annis.api.CorporaApi) Query(org.corpus_tools.annis.gui.objects.Query) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) QueryLanguage(org.corpus_tools.annis.gui.objects.QueryLanguage) Component(org.corpus_tools.annis.api.model.Component) ApiException(org.corpus_tools.annis.ApiException) Optional(java.util.Optional) Annotation(org.corpus_tools.annis.api.model.Annotation) AnnotationComponentType(org.corpus_tools.annis.api.model.AnnotationComponentType) CorpusBrowserEntry(org.corpus_tools.annis.gui.beans.CorpusBrowserEntry) CorporaApi(org.corpus_tools.annis.api.CorporaApi) Annotation(org.corpus_tools.annis.api.model.Annotation) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) AnnoKey(org.corpus_tools.annis.api.model.AnnoKey) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Component(org.corpus_tools.annis.api.model.Component) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ApiException(org.corpus_tools.annis.ApiException) HashSet(java.util.HashSet)

Example 3 with Annotation

use of org.corpus_tools.annis.api.model.Annotation in project ANNIS by korpling.

the class MetaDataPanelTest method showPcc2CorpusBrowser.

@Test
void showPcc2CorpusBrowser() throws Exception {
    // Open a corpus browser for pcc2
    Button button = mock(Button.class);
    CorpusListPanel corpusList = ui.getSearchView().getControlPanel().getCorpusList();
    corpusList.initCorpusBrowser("pcc2", button);
    // Get window by its title
    Window w = _get(Window.class, spec -> spec.withCaption("Corpus information for pcc2"));
    // Check that the caption for the meta data panel is correct and it contains the meta data for
    // pcc2
    MetaDataPanel metaPanel = _get(w, MetaDataPanel.class, spec -> spec.withCaption("Metadata"));
    // Wait for corpus browser content to load
    awaitCondition(30, () -> _find(w, ProgressBar.class).isEmpty());
    Accordion accordion = _get(metaPanel, Accordion.class);
    assertEquals("pcc2", accordion.getTab(0).getCaption());
    @SuppressWarnings("unchecked") Grid<Annotation> metaGrid = _get(metaPanel, Grid.class);
    @SuppressWarnings("unchecked") Column<Annotation, String> nameColumn = (Column<Annotation, String>) metaGrid.getColumns().get(0);
    assertEquals("Name", nameColumn.getCaption());
    @SuppressWarnings("unchecked") Column<Annotation, Label> valueColumn = (Column<Annotation, Label>) metaGrid.getColumns().get(1);
    assertEquals("Value", valueColumn.getCaption());
    // Check name of each annotation row
    assertEquals("full_name", GridKt._getFormattedRow(metaGrid, 0).get(0));
    assertEquals("URL", GridKt._getFormattedRow(metaGrid, 1).get(0));
    assertEquals("version", GridKt._getFormattedRow(metaGrid, 2).get(0));
    assertEquals("annotation_description", GridKt._getFormattedRow(metaGrid, 3).get(0));
    assertEquals("annotation_levels", GridKt._getFormattedRow(metaGrid, 4).get(0));
    assertEquals("language", GridKt._getFormattedRow(metaGrid, 5).get(0));
    assertEquals("source", GridKt._getFormattedRow(metaGrid, 6).get(0));
    // Also check some of the annotation values
    assertEquals("<a href=\"http://www.aclweb.org/anthology/W/W04/W04-0213.pdf\" target=\"_new\">link</a>", GridKt._get(metaGrid, 1).getVal());
    assertEquals("7.0", GridKt._get(metaGrid, 2).getVal());
    assertEquals("German", GridKt._get(metaGrid, 5).getVal());
}
Also used : Window(com.vaadin.ui.Window) Label(com.vaadin.ui.Label) Annotation(org.corpus_tools.annis.api.model.Annotation) CorpusListPanel(org.corpus_tools.annis.gui.controlpanel.CorpusListPanel) Accordion(com.vaadin.ui.Accordion) Button(com.vaadin.ui.Button) Column(com.vaadin.ui.Grid.Column) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Annotation

use of org.corpus_tools.annis.api.model.Annotation in project ANNIS by korpling.

the class GeneralTextExporter method getAllAnnotationsAsExporterKey.

/**
 * Queries all annotations for the given corpora.
 *
 * @param corpora The corpora to query
 * @param api An API object used to perform the lookup
 * @return A list of annotation names.
 * @throws ApiException
 */
protected List<String> getAllAnnotationsAsExporterKey(Collection<String> corpora, CorporaApi api) throws ApiException {
    LinkedList<String> keys = new LinkedList<>();
    keys.add("tok");
    List<Annotation> attributes = new LinkedList<>();
    for (String corpus : corpora) {
        attributes.addAll(api.nodeAnnotations(corpus, false, false));
    }
    for (Annotation a : attributes) {
        if (a.getKey().getName() != null) {
            keys.add(a.getKey().getName());
        }
    }
    return keys;
}
Also used : LinkedList(java.util.LinkedList) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) Annotation(org.corpus_tools.annis.api.model.Annotation)

Example 5 with Annotation

use of org.corpus_tools.annis.api.model.Annotation in project ANNIS by korpling.

the class FlatQueryBuilder method getAvailableAnnotationNames.

public Set<String> getAvailableAnnotationNames() {
    Set<String> result = new TreeSet<>();
    // get current corpus selection
    Collection<String> corpusSelection = cp.getState().getSelectedCorpora();
    CorporaApi api = new CorporaApi(Helper.getClient(UI.getCurrent()));
    try {
        for (String corpus : corpusSelection) {
            for (Annotation a : api.nodeAnnotations(corpus, false, false)) {
                result.add(a.getKey().getName());
            }
        }
    } catch (ApiException ex) {
        log.error(null, ex);
    }
    result.add("tok");
    return result;
}
Also used : CorporaApi(org.corpus_tools.annis.api.CorporaApi) TreeSet(java.util.TreeSet) Annotation(org.corpus_tools.annis.api.model.Annotation) ApiException(org.corpus_tools.annis.ApiException)

Aggregations

Annotation (org.corpus_tools.annis.api.model.Annotation)8 TreeSet (java.util.TreeSet)5 Accordion (com.vaadin.ui.Accordion)4 Label (com.vaadin.ui.Label)4 LinkedList (java.util.LinkedList)4 ApiException (org.corpus_tools.annis.ApiException)4 CorporaApi (org.corpus_tools.annis.api.CorporaApi)4 Panel (com.vaadin.ui.Panel)3 UI (com.vaadin.ui.UI)3 List (java.util.List)3 Optional (java.util.Optional)3 ListDataProvider (com.vaadin.data.provider.ListDataProvider)2 ContentMode (com.vaadin.shared.ui.ContentMode)2 Alignment (com.vaadin.ui.Alignment)2 Button (com.vaadin.ui.Button)2 Grid (com.vaadin.ui.Grid)2 Column (com.vaadin.ui.Grid.Column)2 ProgressBar (com.vaadin.ui.ProgressBar)2 VerticalLayout (com.vaadin.ui.VerticalLayout)2 Window (com.vaadin.ui.Window)2