use of annis.dao.objects.AnnotatedMatch 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");
}
}
use of annis.dao.objects.AnnotatedMatch 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;
}
use of annis.dao.objects.AnnotatedMatch 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");
}
}
use of annis.dao.objects.AnnotatedMatch 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);
}
use of annis.dao.objects.AnnotatedMatch 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;
}
Aggregations