use of org.corpus_tools.annis.gui.widgets.grid.AnnotationGrid in project ANNIS by korpling.
the class SingleGridComponent method createAnnotationGrid.
private boolean createAnnotationGrid() {
String resultID = input.getId();
grid = new AnnotationGrid(mediaController, pdfController, resultID);
grid.addStyleName(getMainStyle());
grid.addStyleName(Helper.CORPUS_FONT_FORCE);
grid.setEscapeHTML(Boolean.parseBoolean(input.getMappings().getOrDefault(MAPPING_ESCAPE_HTML, "true")));
LinkedList<Class<? extends SNode>> types = new LinkedList<>();
if (isShowingSpanAnnotations()) {
types.add(SSpan.class);
}
if (isShowingTokenAnnotations()) {
types.add(SToken.class);
}
grid.setAnnosWithNamespace(EventExtractor.computeDisplayedNamespace(input, types));
layout.addComponent(grid);
SDocumentGraph graph = input.getDocument().getDocumentGraph();
List<SNode> sortedSegmentationNodes = Helper.getSortedSegmentationNodes(this.segmentationName, graph);
Map<SToken, Integer> token2index = Helper.createToken2IndexMap(graph, this.enforcedText);
Preconditions.checkArgument(!token2index.isEmpty(), "Token list must be non-empty");
LinkedHashMap<String, ArrayList<Row>> rowsByAnnotation = computeAnnotationRows(token2index);
// Get Mappings
String gridTemplates = input.getMappings().getOrDefault(MAPPING_GRID_TEMPLATES, "");
// Parse Mappings
if (!gridTemplates.equals("")) {
String[] split = gridTemplates.split("\\|\\|");
for (String s : split) {
// example of s: entity="person"==>:), or infstat==><b>%%value%%</b>
String[] unit_split = s.split("==>");
Set<Map.Entry<String, ArrayList<Row>>> set = rowsByAnnotation.entrySet();
// Displaying elements of LinkedHashMap
Iterator<Map.Entry<String, ArrayList<Row>>> iterator = set.iterator();
while (iterator.hasNext()) {
// iterate over rows
Map.Entry<String, ArrayList<Row>> me = iterator.next();
String rowKey = me.getKey();
ArrayList<Row> rowValue = me.getValue();
for (Row rowValue1 : rowValue) {
ArrayList<GridEvent> rowEvents = rowValue1.getEvents();
if (unit_split[0].indexOf('=') < 0) {
// unit_split[0] is a single instruction, e.g., infstat
// check if the key of a row in rowsByAnnotation is unit_split[0]
// if it is, we need to change every value of this row, else we dont do anything
String rowName = rowKey.split("::")[1];
if (rowName.equals(unit_split[0])) {
// iterate over all values and replace the value with the unit_split[1]
for (GridEvent ev : rowEvents) {
String origValue = ev.getValue();
String newValue = unit_split[1].replaceAll("%%value%%", origValue);
ev.setValue(newValue);
}
}
} else {
// its a instruction like entity='person'
// first break this split into entity and person
// check if rowKey is entity, then when iterating over events, check if value is
// person
String rowName = rowKey.split("::")[1];
String targetRow = unit_split[0].split("=")[0];
String targetValue = unit_split[0].split("=")[1].replaceAll("\"", "");
if (rowName.equals(targetRow)) {
// iterate over all values and replace the value with the unit_split[1]
for (GridEvent ev : rowEvents) {
String origValue = ev.getValue();
if (origValue.equals(targetValue)) {
ev.setValue(unit_split[1]);
}
// String newValue = unit_split[1].replaceAll("%%value%%",origValue);
}
}
}
}
}
}
}
// add tokens as row
Row tokenRow = computeTokenRow(sortedSegmentationNodes, graph, rowsByAnnotation, token2index);
String tokenRowCaption = "tok";
if (isHidingToken()) {
// We have to add the invisible token row avoid issues with the layout
// (see https://github.com/korpling/ANNIS/issues/524)
// but we don't want the invisible token layer to override an actual "tok"
// annotation layer (see https://github.com/korpling/ANNIS/issues/596)
tokenRow.setStyle("invisible_token");
tokenRowCaption = "";
grid.setTokRowKey("");
}
if (isTokenFirst()) {
// copy original list but add token row at the beginning
LinkedHashMap<String, ArrayList<Row>> newList = new LinkedHashMap<>();
newList.put(tokenRowCaption, Lists.newArrayList(tokenRow));
newList.putAll(rowsByAnnotation);
rowsByAnnotation = newList;
} else {
// just add the token row to the end of the list
rowsByAnnotation.put(tokenRowCaption, Lists.newArrayList(tokenRow));
}
EventExtractor.removeEmptySpace(rowsByAnnotation, tokenRow);
// check if the token row only contains empty values
boolean tokenRowIsEmpty = true;
for (GridEvent tokenEvent : tokenRow.getEvents()) {
if (tokenEvent.getValue() != null && !tokenEvent.getValue().trim().isEmpty()) {
tokenRowIsEmpty = false;
break;
}
}
if (!isHidingToken() && canShowEmptyTokenWarning()) {
lblEmptyToken.setVisible(tokenRowIsEmpty);
}
grid.setRowsByAnnotation(rowsByAnnotation);
return !tokenRowIsEmpty;
}
use of org.corpus_tools.annis.gui.widgets.grid.AnnotationGrid in project ANNIS by korpling.
the class AnnisUITest method searchPcc2InverseOrder.
@Test
void searchPcc2InverseOrder() throws Exception {
selectCorpus("pcc2");
// Set inverse order in search options
TabSheet optionTabSheet = _get(_get(ControlPanel.class), TabSheet.class);
optionTabSheet.setSelectedTab(_get(SearchOptionsPanel.class));
awaitCondition(10, () -> !_find(optionTabSheet, ComboBox.class, spec -> spec.withCaption("Order")).isEmpty());
@SuppressWarnings("unchecked") ComboBox<OrderEnum> orderComboBox = _get(optionTabSheet, ComboBox.class, spec -> spec.withCaption("Order"));
_setValue(orderComboBox, OrderEnum.INVERTED);
// Set the query and submit query
_get(AqlCodeEditor.class).getPropertyDataSource().setValue("\"Die\"");
MockVaadin.INSTANCE.clientRoundtrip();
awaitCondition(5, () -> "\"Die\"".equals(ui.getQueryState().getAql().getValue()));
awaitCondition(5, () -> "Valid query, click on \"Search\" to start searching.".equals(ui.getSearchView().getControlPanel().getQueryPanel().getLastPublicStatus()));
Button searchButton = _get(Button.class, spec -> spec.withCaption("Search"));
_click(searchButton);
// Wait until the count is displayed
String expectedStatus = "4 matches\nin 2 documents";
awaitCondition(60, () -> expectedStatus.equals(ui.getSearchView().getControlPanel().getQueryPanel().getLastPublicStatus()), () -> "Waited for status \"" + expectedStatus + "\" but was \"" + ui.getSearchView().getControlPanel().getQueryPanel().getLastPublicStatus() + "\"");
ResultViewPanel resultView = _get(ResultViewPanel.class);
awaitCondition(30, () -> _find(resultView, SingleResultPanel.class).size() == 4);
// Test that the cell values have the correct token value
SingleResultPanel resultPanel = _find(SingleResultPanel.class).get(0);
KWICComponent kwicVis = _get(resultPanel, KWICComponent.class);
AnnotationGrid kwicGrid = _get(kwicVis, AnnotationGrid.class);
ArrayList<Row> tokens = kwicGrid.getRowsByAnnotation().get("tok");
assertEquals(1, tokens.size());
assertEquals(Arrays.asList("fürs", "Dallgower", "Tor", "gab", ".", "Die", "Seeburger", "und", "einige", "Groß-Glienicker", "haben"), tokens.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList()));
}
use of org.corpus_tools.annis.gui.widgets.grid.AnnotationGrid in project ANNIS by korpling.
the class AnnisUITest method tokenSearchPcc2.
@Test
void tokenSearchPcc2() throws Exception {
executeTokenSearch("pcc2", 399, 2);
// Test that the cell values have the correct token value
SingleResultPanel resultPanel = _find(SingleResultPanel.class).get(0);
KWICComponent kwicVis = _get(resultPanel, KWICComponent.class);
AnnotationGrid kwicGrid = _get(kwicVis, AnnotationGrid.class);
ArrayList<Row> tokens = kwicGrid.getRowsByAnnotation().get("tok");
assertEquals(1, tokens.size());
assertEquals(Arrays.asList("Feigenblatt", "Die", "Jugendlichen", "in", "Zossen", "wollen"), tokens.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList()));
// Check the annotation values are shown
ArrayList<Row> lemmaRows = kwicGrid.getRowsByAnnotation().get("tiger::lemma");
assertEquals(1, lemmaRows.size());
assertEquals(Arrays.asList("Feigenblatt", "der", "jugendliche", "in", "Zossen", "wollen"), lemmaRows.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList()));
ArrayList<Row> posRows = kwicGrid.getRowsByAnnotation().get("tiger::pos");
assertEquals(1, posRows.size());
assertEquals(Arrays.asList("NN", "ART", "NN", "APPR", "NE", "VMFIN"), posRows.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList()));
// Test that we can show the first metadata for the button
List<Button> infoButtons = _find(Button.class, spec -> spec.withPredicate(b -> "Show metadata".equals(b.getDescription())));
assertEquals(10, infoButtons.size());
_click(infoButtons.get(0));
Window infoWindow = _get(Window.class);
assertEquals("Info for salt:/pcc2/11299", infoWindow.getCaption());
awaitCondition(30, () -> !_find(infoWindow, Accordion.class).isEmpty());
Accordion metaAccordion = _get(infoWindow, Accordion.class);
@SuppressWarnings("rawtypes") List<Grid> metadataGrids = _find(metaAccordion, Grid.class);
assertEquals(2, metadataGrids.size());
assertEquals("11299 (document)", metaAccordion.getTab(metadataGrids.get(0)).getCaption());
assertEquals("pcc2 (corpus)", metaAccordion.getTab(metadataGrids.get(1)).getCaption());
@SuppressWarnings("unchecked") Annotation firstAnno = (Annotation) GridKt._get(metadataGrids.get(0), 0);
assertEquals("Dokumentname", firstAnno.getKey().getName());
assertEquals("pcc-11299", firstAnno.getVal());
@SuppressWarnings("unchecked") Annotation secondAnno = (Annotation) GridKt._get(metadataGrids.get(0), 1);
assertEquals("Genre", secondAnno.getKey().getName());
assertEquals("Politik", secondAnno.getVal());
@SuppressWarnings("unchecked") Annotation thirdAnno = (Annotation) GridKt._get(metadataGrids.get(0), 2);
assertEquals("Titel", thirdAnno.getKey().getName());
assertEquals("Feigenblatt", thirdAnno.getVal());
// Disable the part-of-speech token annotation display
TreeSet<String> visibleAnnos = new TreeSet<>(Arrays.asList("tiger::lemma"));
resultPanel.setVisibleTokenAnnosVisible(visibleAnnos);
assertNull(kwicGrid.getRowsByAnnotation().get("tiger:pos"));
assertNotNull(kwicGrid.getRowsByAnnotation().get("tiger::lemma"));
// Change the context and test that the KWIC displayed also changed
resultPanel.changeContext(1, 6, false);
// Since the action will replace the whole result panel, we have to get all
// variables again.
List<String> expectedTokens = Arrays.asList("Feigenblatt", "Die", "Jugendlichen", "in", "Zossen", "wollen", "ein");
awaitCondition(10, () -> {
List<SingleResultPanel> allResults = _find(SingleResultPanel.class);
if (allResults.isEmpty()) {
return false;
}
List<AnnotationGrid> annoGrids = _find(allResults.get(0), AnnotationGrid.class);
if (annoGrids.isEmpty()) {
return false;
}
ArrayList<Row> tokensUpdatedContext = annoGrids.get(0).getRowsByAnnotation().get("tok");
if (tokensUpdatedContext.isEmpty()) {
return false;
}
List<String> actualTokens = tokensUpdatedContext.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList());
return expectedTokens.equals(actualTokens);
});
}
use of org.corpus_tools.annis.gui.widgets.grid.AnnotationGrid in project ANNIS by korpling.
the class AnnisUITest method tokenSearchDialog.
@Test
void tokenSearchDialog() throws Exception {
executeTokenSearch("dialog.demo", 102, 1);
// Test that there is a grid visualizer
SingleResultPanel resultPanel = _find(SingleResultPanel.class).get(0);
GridComponent gridVis = _get(resultPanel, GridComponent.class, spec -> spec.withPredicate(g -> !(g instanceof KWICComponent)));
AnnotationGrid annoGrid = _get(gridVis, AnnotationGrid.class);
ArrayList<Row> tokens = annoGrid.getRowsByAnnotation().get("default_ns::norm0");
assertEquals(1, tokens.size());
assertEquals(Arrays.asList("äh", "fang", "einfach", "mal", "an"), tokens.get(0).getEvents().stream().map(GridEvent::getValue).collect(Collectors.toList()));
// Open the video visualizer and check that media component is loaded
Button btOpenVisualizer = _get(resultPanel, Button.class, spec -> spec.withCaption("video"));
_click(btOpenVisualizer);
awaitCondition(120, () -> !_find(resultPanel, MediaElementPlayer.class).isEmpty());
MediaElementPlayer player = _get(resultPanel, MediaElementPlayer.class, spec -> spec.withCount(1));
assertEquals("video/webm", player.getState().getMimeType());
assertEquals("/Binary?file=dialog.demo%2Fdialog.demo%2Fdialog.demo.webm&toplevelCorpusName=dialog.demo", player.getState().getResourceURL());
// Close the visualizer again
_click(btOpenVisualizer);
awaitCondition(120, () -> _find(resultPanel, MediaElementPlayer.class).isEmpty());
}
Aggregations