use of annis.service.objects.Match in project ANNIS by korpling.
the class QueryDaoImpl method find.
@Transactional(readOnly = true)
@Override
public boolean find(final QueryData queryData, final OutputStream out) {
prepareTransaction(queryData);
Boolean finished = getJdbcTemplate().execute(new ConnectionCallback<Boolean>() {
@Override
public Boolean doInConnection(Connection con) throws SQLException, DataAccessException {
try (Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
String sql = findSqlGenerator.toSql(queryData);
PrintWriter w;
try (ResultSet rs = stmt.executeQuery(sql)) {
w = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
ResultSetTypedIterator<Match> itMatches = new ResultSetTypedIterator<>(rs, findSqlGenerator);
int i = 1;
while (itMatches.hasNext()) {
// write single match to output stream
Match m = itMatches.next();
w.print(m.toString());
w.print("\n");
// flush after every 10th item
if (i % 10 == 0) {
w.flush();
}
i++;
}
// end for each match
}
w.flush();
return true;
} catch (UnsupportedEncodingException ex) {
log.error("Your system is not able to handle UTF-8 but ANNIS really needs this charset", ex);
}
return false;
}
});
return finished;
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class AnnisRunner method doFind.
public void doFind(String annisQuery) {
List<Match> matches = queryDao.find(analyzeQuery(annisQuery, "find"));
MatchGroup group = new MatchGroup(matches);
out.println(group.toString());
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class GraphHelper method createQueryData.
/**
* This is a helper function to make it easier to create a correct query data object
* from a {@link MatchGroup} for a {@link AnnisDao#graph(annis.ql.parser.QueryData) } query.
*
* @param matchGroup
* @return
*/
public static QueryData createQueryData(MatchGroup matchGroup, QueryDao annisDao) {
QueryData queryData = new QueryData();
Set<String> corpusNames = new TreeSet<>();
for (Match m : matchGroup.getMatches()) {
// collect list of used corpora and created pseudo QueryNodes for each URI
List<QueryNode> pseudoNodes = new ArrayList<>(m.getSaltIDs().size());
for (java.net.URI u : m.getSaltIDs()) {
pseudoNodes.add(new QueryNode());
corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
}
queryData.addAlternative(pseudoNodes);
}
List<Long> corpusIDs = annisDao.mapCorpusNamesToIds(new LinkedList<>(corpusNames));
queryData.setCorpusList(corpusIDs);
queryData.addExtension(matchGroup);
return queryData;
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class SaltAnnotateExtractor method addMatchInformation.
/**
* Sets additional match (global) information about the matched nodes and
* annotations.
*
* This will add the {@link AnnisConstants#FEAT_MATCHEDIDS) to all {@link SDocument} elements of the
* salt project.
*
* @param p The salt project to add the features to.
* @param matchGroup A list of matches in the same order as the corpus graphs
* of the salt project.
*/
public static void addMatchInformation(SaltProject p, MatchGroup matchGroup) {
int matchIndex = 0;
for (Match m : matchGroup.getMatches()) {
// get the corresponding SDocument of the salt project
SCorpusGraph corpusGraph = p.getCorpusGraphs().get(matchIndex);
SDocument doc = corpusGraph.getDocuments().get(0);
setMatchedIDs(doc.getDocumentGraph(), m);
matchIndex++;
}
}
use of annis.service.objects.Match in project ANNIS by korpling.
the class GeneralTextExporter method convertText.
@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
try {
if (keys == null || keys.isEmpty()) {
// auto set
keys = new LinkedList<>();
keys.add("tok");
List<AnnisAttribute> attributes = new LinkedList<>();
for (String corpus : corpora) {
attributes.addAll(annisResource.path("corpora").path(urlPathEscape.escape(corpus)).path("annotations").queryParam("fetchvalues", "false").queryParam("onlymostfrequentvalues", "false").get(new AnnisAttributeListType()));
}
for (AnnisAttribute a : attributes) {
if (a.getName() != null) {
String[] namespaceAndName = a.getName().split(":", 2);
if (namespaceAndName.length > 1) {
keys.add(namespaceAndName[1]);
} else {
keys.add(namespaceAndName[0]);
}
}
}
}
Map<String, String> args = new HashMap<>();
for (String s : argsAsString.split("&|;")) {
String[] splitted = s.split("=", 2);
String key = splitted[0];
String val = "";
if (splitted.length > 1) {
val = splitted[1];
}
args.put(key, val);
}
int stepSize = 10;
// 1. Get all the matches as Salt ID
InputStream matchStream = annisResource.path("search/find/").queryParam("q", Helper.encodeJersey(queryAnnisQL)).queryParam("corpora", StringUtils.join(corpora, ",")).accept(MediaType.TEXT_PLAIN_TYPE).get(InputStream.class);
try (BufferedReader inReader = new BufferedReader(new InputStreamReader(matchStream, "UTF-8"))) {
WebResource subgraphRes = annisResource.path("search/subgraph");
MatchGroup currentMatches = new MatchGroup();
String currentLine;
int offset = 0;
// 2. iterate over all matches and get the sub-graph for a group of matches
while (!Thread.currentThread().isInterrupted() && (currentLine = inReader.readLine()) != null) {
Match match = Match.parseFromString(currentLine);
currentMatches.getMatches().add(match);
if (currentMatches.getMatches().size() >= stepSize) {
WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
if (args.containsKey("segmentation")) {
res = res.queryParam("segmentation", args.get("segmentation"));
}
SubgraphFilter filter = getSubgraphFilter();
if (filter != null) {
res = res.queryParam("filter", filter.name());
}
Stopwatch stopwatch = Stopwatch.createUnstarted();
stopwatch.start();
SaltProject p = res.post(SaltProject.class, currentMatches);
stopwatch.stop();
// export was fast enough
if (stopwatch.elapsed(TimeUnit.MILLISECONDS) < 500 && stepSize < 50) {
stepSize += 10;
}
convertText(LegacyGraphConverter.convertToResultSet(p), keys, args, out, offset - currentMatches.getMatches().size());
currentMatches.getMatches().clear();
if (eventBus != null) {
eventBus.post(offset + 1);
}
}
offset++;
}
if (Thread.interrupted()) {
return new InterruptedException("Exporter job was interrupted");
}
// query the left over matches
if (!currentMatches.getMatches().isEmpty()) {
WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
if (args.containsKey("segmentation")) {
res = res.queryParam("segmentation", args.get("segmentation"));
}
SubgraphFilter filter = getSubgraphFilter();
if (filter != null) {
res = res.queryParam("filter", filter.name());
}
SaltProject p = res.post(SaltProject.class, currentMatches);
convertText(LegacyGraphConverter.convertToResultSet(p), keys, args, out, offset - currentMatches.getMatches().size() - 1);
}
offset = 0;
}
out.append("\n");
out.append("\n");
out.append("finished");
return null;
} catch (AnnisQLSemanticsException | AnnisQLSyntaxException | AnnisCorpusAccessException | UniformInterfaceException | IOException ex) {
return ex;
}
}
Aggregations