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;
}
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());
});
}
}
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());
}
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;
}
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;
}
Aggregations