Search in sources :

Example 1 with Match

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;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Match(annis.service.objects.Match) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) ResultSetTypedIterator(annis.sqlgen.ResultSetTypedIterator) DataAccessException(org.springframework.dao.DataAccessException) PrintWriter(java.io.PrintWriter) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Match

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());
}
Also used : MatchGroup(annis.service.objects.MatchGroup) Match(annis.service.objects.Match) AnnotatedMatch(annis.dao.objects.AnnotatedMatch)

Example 3 with Match

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;
}
Also used : QueryData(annis.ql.parser.QueryData) ArrayList(java.util.ArrayList) Match(annis.service.objects.Match) TreeSet(java.util.TreeSet) QueryNode(annis.model.QueryNode)

Example 4 with Match

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++;
    }
}
Also used : SDocument(org.corpus_tools.salt.common.SDocument) Match(annis.service.objects.Match) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph)

Example 5 with Match

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;
    }
}
Also used : HashMap(java.util.HashMap) AnnisAttribute(annis.service.objects.AnnisAttribute) Stopwatch(com.google.common.base.Stopwatch) WebResource(com.sun.jersey.api.client.WebResource) Match(annis.service.objects.Match) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) SubgraphFilter(annis.service.objects.SubgraphFilter) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisCorpusAccessException(annis.exceptions.AnnisCorpusAccessException) MatchGroup(annis.service.objects.MatchGroup) BufferedReader(java.io.BufferedReader)

Aggregations

Match (annis.service.objects.Match)18 MatchGroup (annis.service.objects.MatchGroup)9 LinkedList (java.util.LinkedList)8 SaltProject (org.corpus_tools.salt.common.SaltProject)8 WebResource (com.sun.jersey.api.client.WebResource)5 ArrayList (java.util.ArrayList)5 QueryData (annis.ql.parser.QueryData)4 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)4 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 SubgraphFilter (annis.service.objects.SubgraphFilter)3 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)3 AnnisCorpusAccessException (annis.exceptions.AnnisCorpusAccessException)2 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)2 AnnisQLSyntaxException (annis.exceptions.AnnisQLSyntaxException)2 AnnotationGraph (annis.model.AnnotationGraph)2 QueryNode (annis.model.QueryNode)2 AnnisAttribute (annis.service.objects.AnnisAttribute)2 MatrixQueryData (annis.sqlgen.MatrixQueryData)2