use of annis.service.objects.Match in project ANNIS by korpling.
the class LegacyGraphConverterTest method testConvertToAOM.
/**
* Test of convertToAOM method, of class LegacyGraphConverter.
*/
@Test
public void testConvertToAOM() throws SQLException {
SaltAnnotateExtractor saltExtractor = new SaltAnnotateExtractor() {
@Override
protected SolutionKey<?> createSolutionKey() {
PostgreSqlArraySolutionKey<Long> key = new PostgreSqlArraySolutionKey<>();
key.setKeyColumnName("key");
key.setIdColumnName("id");
return key;
}
};
CorpusPathExtractor corpusPathExtractor = new ArrayCorpusPathExtractor();
saltExtractor.setCorpusPathExtractor(corpusPathExtractor);
TestAnnotateSqlGenerator.setupOuterQueryFactsTableColumnAliases(saltExtractor);
List<Match> matches = new ArrayList<>();
matches.add(Match.parseFromString("salt:/pcc2/4282/#tok_155 tiger::pos::salt:/pcc2/4282#tok_156"));
MatchGroup matchGroup = new MatchGroup(matches);
SaltProject p = saltExtractor.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
SaltAnnotateExtractor.addMatchInformation(p, matchGroup);
List<AnnotationGraph> expected = aomSqlGen.extractData(new CsvResultSetProvider(annis.sqlgen.SaltAnnotateExtractorTest.class.getResourceAsStream("SampleAnnotateResult.csv")).getResultSet());
List<AnnotationGraph> result = LegacyGraphConverter.convertToAOM(p);
assertEquals(expected.size(), result.size());
Iterator<AnnotationGraph> itGraphExpected = expected.iterator();
Iterator<AnnotationGraph> itGraphResult = result.iterator();
while (itGraphExpected.hasNext() && itGraphResult.hasNext()) {
AnnotationGraph graphExpected = itGraphExpected.next();
AnnotationGraph graphResult = itGraphResult.next();
List<AnnisNode> nodeListExpected = graphExpected.getNodes();
List<AnnisNode> nodeListResult = graphResult.getNodes();
assertEquals(nodeListExpected.size(), nodeListResult.size());
Collections.sort(nodeListExpected, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode arg0, AnnisNode arg1) {
return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
}
});
Collections.sort(nodeListResult, new Comparator<AnnisNode>() {
@Override
public int compare(AnnisNode arg0, AnnisNode arg1) {
return Long.valueOf(arg0.getId()).compareTo(Long.valueOf(arg1.getId()));
}
});
Iterator<AnnisNode> itNodeExpected = nodeListExpected.iterator();
Iterator<AnnisNode> itNodeResult = nodeListResult.iterator();
while (itNodeExpected.hasNext() && itNodeResult.hasNext()) {
checkAnnisNodeEqual(itNodeExpected.next(), itNodeResult.next());
}
}
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class QueryServiceImpl method find.
@GET
@Path("search/find")
@Produces({ "application/xml", "text/plain" })
@Override
public Response find(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames, @DefaultValue("0") @QueryParam("offset") String offsetRaw, @DefaultValue("-1") @QueryParam("limit") String limitRaw, @DefaultValue("ascending") @QueryParam("order") String orderRaw) throws IOException {
requiredParameter(query, "q", "AnnisQL query");
requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
Subject user = SecurityUtils.getSubject();
List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
for (String c : corpusNames) {
user.checkPermission("query:find:" + c);
}
int offset = Integer.parseInt(offsetRaw);
int limit = Integer.parseInt(limitRaw);
OrderType order;
try {
order = OrderType.valueOf(orderRaw.toLowerCase());
} catch (IllegalArgumentException ex) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("parameter 'order' has the invalid value '" + orderRaw + "'. It should be one of" + " 'ascending', 'random' or 'descending").build());
}
final QueryData data = queryDataFromParameters(query, rawCorpusNames);
data.setCorpusConfiguration(queryDao.getCorpusConfiguration());
data.addExtension(new LimitOffsetQueryData(offset, limit, order));
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
if (acceptHeader == null || acceptHeader.trim().isEmpty()) {
acceptHeader = "*/*";
}
List<String> knownTypes = Lists.newArrayList("text/plain", "application/xml");
// find the best matching mime type
String bestMediaTypeMatch = MIMEParse.bestMatch(knownTypes, acceptHeader);
if ("text/plain".equals(bestMediaTypeMatch)) {
return Response.ok(findRaw(data, rawCorpusNames, query), "text/plain").build();
} else {
List<Match> result = findXml(data, rawCorpusNames, query);
return Response.ok().type("application/xml").entity(new GenericEntity<MatchGroup>(new MatchGroup(result)) {
}).build();
}
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class QueryServiceImpl method findXml.
private List<Match> findXml(QueryData data, final String rawCorpusNames, final String query) throws IOException {
long start = new Date().getTime();
List<Match> result = queryDao.find(data);
long end = new Date().getTime();
logQuery("FIND", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
return result;
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class QueryServiceImpl method basicSubgraph.
protected SaltProject basicSubgraph(MatchGroup matches, @QueryParam("segmentation") String segmentation, @DefaultValue("0") @QueryParam("left") String leftRaw, @DefaultValue("0") @QueryParam("right") String rightRaw, @DefaultValue("all") @QueryParam("filter") String filterRaw) {
Subject user = SecurityUtils.getSubject();
int left = Integer.parseInt(leftRaw);
int right = Integer.parseInt(rightRaw);
SubgraphFilter filter = SubgraphFilter.valueOf(filterRaw);
QueryData data = GraphHelper.createQueryData(matches, queryDao);
data.addExtension(new AnnotateQueryData(left, right, segmentation, filter));
Set<String> corpusNames = new TreeSet<>();
for (Match singleMatch : matches.getMatches()) {
// collect list of used corpora
for (java.net.URI u : singleMatch.getSaltIDs()) {
corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
}
}
for (String c : corpusNames) {
user.checkPermission("query:subgraph:" + c);
}
List<String> corpusNamesList = new LinkedList<>(corpusNames);
if (data.getCorpusList() == null || data.getCorpusList().isEmpty()) {
throw new WebApplicationException(Response.Status.BAD_REQUEST.getStatusCode());
}
long start = new Date().getTime();
SaltProject p = queryDao.graph(data);
long end = new Date().getTime();
String options = "matches: " + matches.toString() + ", seg: " + segmentation + ", left: " + left + ", right: " + right + ", filter: " + filter;
logQuery("SUBGRAPH", "", corpusNamesList, end - start, options);
return p;
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class FindSqlGenerator method mapRow.
@Override
public Match mapRow(ResultSet rs, int rowNum) throws SQLException {
Match match = new Match();
// get size of solution
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// the order of columns is not determined and I have to combined two
// values, so save them here and combine later
List<String> corpus_path = null;
// get path
if (outputCorpusPath) {
for (int column = 1; column <= columnCount; ++column) {
if (corpusPathExtractor != null && metaData.getColumnName(column).startsWith("path_name")) {
List<String> genCorpusPath = corpusPathExtractor.extractCorpusPath(rs, metaData.getColumnName(column));
// only use corpus path if valid
if (genCorpusPath != null) {
corpus_path = genCorpusPath;
// all corpus paths are the same
break;
}
}
}
// collect the salt id, node name appendix, annotation namespaces and annotation names
Map<Integer, String> saltIDs = new TreeMap<>();
Map<Integer, String> nodeAnnoNamespaces = new HashMap<>();
Map<Integer, String> nodeAnnoNames = new HashMap<>();
for (int column = 1; column <= columnCount; ++column) {
String columnName = metaData.getColumnName(column);
if (columnName.startsWith("salt_id")) {
String numberAsString = columnName.substring("salt_id".length());
try {
int number = Integer.parseInt(numberAsString);
String saltIDForNode = rs.getString(column);
if (saltIDForNode != null) {
saltIDs.put(number, rs.getString(column));
String annoNamespace = rs.getString("node_annotation_ns" + number);
if (annoNamespace != null) {
nodeAnnoNamespaces.put(number, annoNamespace);
}
String annoName = rs.getString("node_annotation_name" + number);
if (annoName != null) {
nodeAnnoNames.put(number, annoName);
}
}
} catch (NumberFormatException ex) {
log.error("Could not extract the number for column " + columnName, ex);
}
}
}
for (Map.Entry<Integer, String> saltIDEntry : saltIDs.entrySet()) {
URI saltID = buildSaltId(corpus_path, saltIDEntry.getValue());
String qualifiedAnnoName = buildAnnoName(nodeAnnoNamespaces.get(saltIDEntry.getKey()), nodeAnnoNames.get(saltIDEntry.getKey()));
match.addSaltId(saltID, qualifiedAnnoName);
}
}
return match;
}
Aggregations