Search in sources :

Example 1 with AnnotationGrid

use of annis.gui.widgets.grid.AnnotationGrid in project ANNIS by korpling.

the class GridComponent method createAnnotationGrid.

private void 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().getProperty(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> tokens = CommonHelper.getSortedSegmentationNodes(segmentationName, graph);
    Preconditions.checkArgument(!tokens.isEmpty(), "Token list must be non-empty");
    RelannisNodeFeature featTokStart = (RelannisNodeFeature) tokens.get(0).getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_NODE).getValue();
    long startIndex = featTokStart.getTokenIndex();
    RelannisNodeFeature featTokEnd = (RelannisNodeFeature) tokens.get(tokens.size() - 1).getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_NODE).getValue();
    long endIndex = featTokEnd.getTokenIndex();
    LinkedHashMap<String, ArrayList<Row>> rowsByAnnotation = computeAnnotationRows(startIndex, endIndex);
    // Get Mappings
    String gridTemplates = input.getMappings().getProperty(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 set = rowsByAnnotation.entrySet();
            // Displaying elements of LinkedHashMap
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                // iterate over rows
                Map.Entry me = (Map.Entry) iterator.next();
                String rowKey = (String) me.getKey();
                ArrayList<Row> rowValue = (ArrayList<Row>) 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
    AtomicInteger tokenOffsetForText = new AtomicInteger(-1);
    Row tokenRow = computeTokenRow(tokens, graph, rowsByAnnotation, startIndex, tokenOffsetForText);
    if (isHidingToken()) {
        tokenRow.setStyle("invisible_token");
    }
    if (isTokenFirst()) {
        // copy original list but add token row at the beginning
        LinkedHashMap<String, ArrayList<Row>> newList = new LinkedHashMap<>();
        newList.put("tok", Lists.newArrayList(tokenRow));
        newList.putAll(rowsByAnnotation);
        rowsByAnnotation = newList;
    } else {
        // just add the token row to the end of the list
        rowsByAnnotation.put("tok", 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);
    grid.setTokenIndexOffset(tokenOffsetForText.get());
}
Also used : SNode(org.corpus_tools.salt.core.SNode) HashSet(java.util.HashSet) Set(java.util.Set) GridEvent(annis.gui.widgets.grid.GridEvent) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AnnotationGrid(annis.gui.widgets.grid.AnnotationGrid) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) Iterator(java.util.Iterator) RelannisNodeFeature(annis.model.RelannisNodeFeature) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Row(annis.gui.widgets.grid.Row) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

AnnotationGrid (annis.gui.widgets.grid.AnnotationGrid)1 GridEvent (annis.gui.widgets.grid.GridEvent)1 Row (annis.gui.widgets.grid.Row)1 RelannisNodeFeature (annis.model.RelannisNodeFeature)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)1 SNode (org.corpus_tools.salt.core.SNode)1