Search in sources :

Example 1 with AnnotatedSpan

use of annis.dao.objects.AnnotatedSpan in project ANNIS by korpling.

the class CSVHelper method exportCSVData.

public static void exportCSVData(Iterator<AnnotatedMatch> matches, SortedMap<Integer, SortedSet<String>> columnsByNodePos, PrintWriter w) {
    int count = columnsByNodePos.keySet().size();
    // print values
    while (matches.hasNext()) {
        AnnotatedMatch match = matches.next();
        List<String> line = new ArrayList<>();
        int k = 0;
        for (; k < match.size(); ++k) {
            AnnotatedSpan span = match.get(k);
            Map<String, String> valueByName = new HashMap<>();
            if (span != null) {
                if (span.getAnnotations() != null) {
                    for (Annotation annotation : span.getAnnotations()) {
                        valueByName.put("anno_" + annotation.getQualifiedName(), annotation.getValue());
                    }
                }
                if (span.getMetadata() != null) {
                    for (Annotation meta : span.getMetadata()) {
                        valueByName.put("meta_" + meta.getQualifiedName(), meta.getValue());
                    }
                }
                line.add("" + span.getId());
                line.add(span.getCoveredText().replace("\t", "\\t"));
            }
            for (String name : columnsByNodePos.get(k)) {
                if (valueByName.containsKey(name)) {
                    line.add(valueByName.get(name).replace("\t", "\\t"));
                } else {
                    line.add("'NULL'");
                }
            }
        }
        for (int l = k; l < count; ++l) {
            line.add("'NULL'");
            for (int m = 0; m <= columnsByNodePos.get(l).size(); ++m) {
                line.add("'NULL'");
            }
        }
        w.append(StringUtils.join(line, "\t"));
        w.append("\n");
    }
}
Also used : AnnotatedMatch(annis.dao.objects.AnnotatedMatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotatedSpan(annis.dao.objects.AnnotatedSpan) Annotation(annis.model.Annotation)

Example 2 with AnnotatedSpan

use of annis.dao.objects.AnnotatedSpan in project ANNIS by korpling.

the class CSVHelper method exportCSVHeader.

public static SortedMap<Integer, SortedSet<String>> exportCSVHeader(Iterator<AnnotatedMatch> matches, PrintWriter w) {
    // figure out what annotations are used at each match position
    SortedMap<Integer, SortedSet<String>> columnsByNodePos = new TreeMap<>();
    while (matches.hasNext()) {
        AnnotatedMatch match = matches.next();
        for (int j = 0; j < match.size(); ++j) {
            AnnotatedSpan span = match.get(j);
            if (columnsByNodePos.get(j) == null) {
                columnsByNodePos.put(j, new TreeSet<String>());
            }
            if (span != null) {
                for (Annotation annotation : span.getAnnotations()) {
                    columnsByNodePos.get(j).add("anno_" + annotation.getQualifiedName());
                }
                for (Annotation meta : span.getMetadata()) {
                    columnsByNodePos.get(j).add("meta_" + meta.getQualifiedName());
                }
            }
        }
    }
    // important: don't close the wrapper CSVWriter!
    @SuppressWarnings("resource") CSVWriter csvWriter = new CSVWriter(w, '\t', CSVWriter.NO_QUOTE_CHARACTER, '\\');
    // print column names and data types
    int count = columnsByNodePos.keySet().size();
    ArrayList<String> headerLine = new ArrayList<>();
    for (int j = 0; j < count; ++j) {
        headerLine.add(fullColumnName(j + 1, "id"));
        headerLine.add(fullColumnName(j + 1, "span"));
        SortedSet<String> annotationNames = columnsByNodePos.get(j);
        for (String name : annotationNames) {
            headerLine.add(fullColumnName(j + 1, name));
        }
    }
    csvWriter.writeNext(headerLine.toArray(new String[headerLine.size()]));
    return columnsByNodePos;
}
Also used : ArrayList(java.util.ArrayList) CSVWriter(au.com.bytecode.opencsv.CSVWriter) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) Annotation(annis.model.Annotation) AnnotatedMatch(annis.dao.objects.AnnotatedMatch) AnnotatedSpan(annis.dao.objects.AnnotatedSpan)

Example 3 with AnnotatedSpan

use of annis.dao.objects.AnnotatedSpan in project ANNIS by korpling.

the class WekaHelper method exportArffData.

public static void exportArffData(Iterator<AnnotatedMatch> matches, SortedMap<Integer, SortedSet<String>> columnsByNodePos, PrintWriter w) {
    int count = columnsByNodePos.keySet().size();
    w.append("\n@data\n\n");
    // print values
    while (matches.hasNext()) {
        AnnotatedMatch match = matches.next();
        List<String> line = new ArrayList<>();
        int k = 0;
        for (; k < match.size(); ++k) {
            AnnotatedSpan span = match.get(k);
            Map<String, String> valueByName = new HashMap<>();
            if (span != null) {
                if (span.getAnnotations() != null) {
                    for (Annotation annotation : span.getAnnotations()) {
                        valueByName.put("anno_" + annotation.getQualifiedName(), annotation.getValue());
                    }
                }
                if (span.getMetadata() != null) {
                    for (Annotation meta : span.getMetadata()) {
                        valueByName.put("meta_" + meta.getQualifiedName(), meta.getValue());
                    }
                }
                line.add("'" + span.getId() + "'");
                line.add("'" + span.getCoveredText().replace("'", "\\'") + "'");
            }
            for (String name : columnsByNodePos.get(k)) {
                if (valueByName.containsKey(name)) {
                    line.add("'" + valueByName.get(name).replace("'", "\\'") + "'");
                } else {
                    line.add("'NULL'");
                }
            }
        }
        for (int l = k; l < count; ++l) {
            line.add("'NULL'");
            for (int m = 0; m <= columnsByNodePos.get(l).size(); ++m) {
                line.add("'NULL'");
            }
        }
        w.append(StringUtils.join(line, ","));
        w.append("\n");
    }
}
Also used : AnnotatedMatch(annis.dao.objects.AnnotatedMatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotatedSpan(annis.dao.objects.AnnotatedSpan) Annotation(annis.model.Annotation)

Example 4 with AnnotatedSpan

use of annis.dao.objects.AnnotatedSpan in project ANNIS by korpling.

the class AnnotatedMatchIterator method next.

@Override
public AnnotatedMatch next() {
    List<Long> key = new ArrayList<>();
    AnnotatedSpan[] matchedSpans = new AnnotatedSpan[0];
    if (lastSpan != null) {
        key = lastSpan.getKey();
        if (key == null) {
            matchedSpans = new AnnotatedSpan[0];
        } else {
            matchedSpans = new AnnotatedSpan[key.size()];
            setSpanForAllMatchedPositions(key, matchedSpans, lastSpan);
        }
        lastSpan = null;
    }
    while (itSpan.hasNext()) {
        AnnotatedSpan span = itSpan.next();
        List<Long> newKey = span.getKey();
        if (matchedSpans.length == 0) {
            matchedSpans = new AnnotatedSpan[newKey.size()];
        }
        if (key.isEmpty() || newKey.equals(key)) {
            setSpanForAllMatchedPositions(newKey, matchedSpans, span);
            key = newKey;
            lastSpan = null;
        } else {
            // save reference to the already fetched span since we can't get back
            lastSpan = span;
            // we finished collecting all relevant spans
            break;
        }
    }
    // HACK: delete metadata spans for non-first nodes
    for (int i = 1; i < matchedSpans.length; i++) {
        if (matchedSpans[i] != null) {
            matchedSpans[i].setMetadata(new LinkedList<Annotation>());
        }
    }
    return new AnnotatedMatch(matchedSpans);
}
Also used : AnnotatedMatch(annis.dao.objects.AnnotatedMatch) ArrayList(java.util.ArrayList) AnnotatedSpan(annis.dao.objects.AnnotatedSpan) Annotation(annis.model.Annotation)

Example 5 with AnnotatedSpan

use of annis.dao.objects.AnnotatedSpan in project ANNIS by korpling.

the class MatrixSqlGenerator method extractData.

@Override
public List<AnnotatedMatch> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
    List<AnnotatedMatch> matches = new ArrayList<>();
    Map<List<Long>, AnnotatedSpan[]> matchesByGroup = new HashMap<>();
    int rowNum = 0;
    while (resultSet.next()) {
        long id = resultSet.getLong("id");
        // create key
        Array sqlKey = resultSet.getArray("key");
        Validate.isTrue(!resultSet.wasNull(), "Match group identifier must not be null");
        Validate.isTrue(sqlKey.getBaseType() == Types.BIGINT, "Key in database must be from the type \"bigint\" but was \"" + sqlKey.getBaseTypeName() + "\"");
        Long[] keyArray = (Long[]) sqlKey.getArray();
        int matchWidth = keyArray.length;
        List<Long> key = Arrays.asList(keyArray);
        if (!matchesByGroup.containsKey(key)) {
            matchesByGroup.put(key, new AnnotatedSpan[matchWidth]);
        }
        AnnotatedSpan span = spanExtractor.mapRow(resultSet, rowNum);
        // (node could have matched several times)
        for (int posInMatch = 0; posInMatch < key.size(); posInMatch++) {
            if (key.get(posInMatch) == id) {
                matchesByGroup.get(key)[posInMatch] = span;
            }
        }
        rowNum++;
    }
    for (AnnotatedSpan[] match : matchesByGroup.values()) {
        matches.add(new AnnotatedMatch(Arrays.asList(match)));
    }
    return matches;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Array(java.sql.Array) AnnotatedMatch(annis.dao.objects.AnnotatedMatch) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AnnotatedSpan(annis.dao.objects.AnnotatedSpan)

Aggregations

AnnotatedSpan (annis.dao.objects.AnnotatedSpan)7 AnnotatedMatch (annis.dao.objects.AnnotatedMatch)6 Annotation (annis.model.Annotation)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 Array (java.sql.Array)2 SortedSet (java.util.SortedSet)2 TreeMap (java.util.TreeMap)2 CSVWriter (au.com.bytecode.opencsv.CSVWriter)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1