Search in sources :

Example 66 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class TransitivePrecedenceOptimizer method transform.

@Override
public QueryData transform(QueryData data) {
    // initialize helper variables
    HashSet<Long> visitedNodes = new HashSet<>();
    for (List<QueryNode> alternative : data.getAlternatives()) {
        Map<Long, Set<Precedence>> outJoins = createInitialJoinMap(alternative);
        for (QueryNode node : alternative) {
            Set<String> segmentations = getAllSegmentations(node);
            visitedNodes.clear();
            // we apply the algorithm node by node
            // tok == null segmentation
            propagateNodePrecedence(node, node, visitedNodes, outJoins, null, null);
            for (String s : segmentations) {
                propagateNodePrecedence(node, node, visitedNodes, outJoins, null, s);
            }
        }
    }
    return data;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TreeSet(java.util.TreeSet) QueryNode(annis.model.QueryNode) HashSet(java.util.HashSet)

Example 67 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class FrequencyQueryPanel method createAutomaticEntriesForQuery.

private void createAutomaticEntriesForQuery(String query) {
    if (query == null || query.isEmpty()) {
        return;
    }
    try {
        state.getFrequencyTableDefinition().removeAllItems();
        lblErrorOrMsg.setVisible(false);
        counter = 0;
        List<QueryNode> nodes = parseQuery(query);
        Collections.sort(nodes, new Comparator<QueryNode>() {

            @Override
            public int compare(QueryNode o1, QueryNode o2) {
                if (o1.getVariable() == null) {
                    return o2 == null ? 0 : -1;
                }
                return o1.getVariable().compareTo(o2.getVariable());
            }
        });
        // calculate the nodes that are part of every alternative
        Multimap<String, Integer> alternativesOfVariable = LinkedHashMultimap.create();
        int maxAlternative = 0;
        for (QueryNode n : nodes) {
            if (n.getAlternativeNumber() != null) {
                maxAlternative = Math.max(n.getAlternativeNumber(), maxAlternative);
                alternativesOfVariable.put(n.getVariable(), n.getAlternativeNumber());
            }
        }
        Set<String> allowedVariables = new LinkedHashSet<>();
        for (QueryNode n : nodes) {
            // we assume that the alternative numbering is continuous and without gaps
            if (alternativesOfVariable.get(n.getVariable()).size() == (maxAlternative + 1)) {
                allowedVariables.add(n.getVariable());
            }
        }
        if (maxAlternative > 0 && allowedVariables.isEmpty()) {
            lblErrorOrMsg.setVisible(true);
        }
        Set<UserGeneratedFrequencyEntry> generatedEntries = new HashSet<>();
        for (QueryNode n : nodes) {
            if (!n.isArtificial() && allowedVariables.contains(n.getVariable())) {
                if (n.getNodeAnnotations().isEmpty()) {
                    UserGeneratedFrequencyEntry entry = new UserGeneratedFrequencyEntry();
                    entry.setAnnotation("tok");
                    entry.setComment("automatically created from " + n.toAQLNodeFragment());
                    entry.setNr(n.getVariable());
                    if (!generatedEntries.contains(entry)) {
                        int id = counter++;
                        state.getFrequencyTableDefinition().addItem(id, entry);
                        generatedEntries.add(entry);
                    }
                } else {
                    QueryAnnotation firstAnno = n.getNodeAnnotations().iterator().next();
                    UserGeneratedFrequencyEntry entry = new UserGeneratedFrequencyEntry();
                    entry.setAnnotation(firstAnno.getName());
                    entry.setComment("automatically created from " + n.toAQLNodeFragment());
                    entry.setNr(n.getVariable());
                    if (!generatedEntries.contains(entry)) {
                        int id = counter++;
                        state.getFrequencyTableDefinition().addItem(id, entry);
                        generatedEntries.add(entry);
                    }
                }
            }
        }
    } catch (UniformInterfaceException ex) {
    // non-valid query, ignore
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryAnnotation(annis.model.QueryAnnotation) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) QueryNode(annis.model.QueryNode) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

QueryNode (annis.model.QueryNode)67 Join (annis.model.Join)12 LinkedList (java.util.LinkedList)11 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)10 ArrayList (java.util.ArrayList)8 QueryAnnotation (annis.model.QueryAnnotation)7 Precedence (annis.sqlgen.model.Precedence)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Dominance (annis.sqlgen.model.Dominance)5 PointingRelation (annis.sqlgen.model.PointingRelation)5 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 TreeSet (java.util.TreeSet)5 Test (org.junit.Test)5 Identical (annis.sqlgen.model.Identical)4 LeftDominance (annis.sqlgen.model.LeftDominance)4 Near (annis.sqlgen.model.Near)4 RightDominance (annis.sqlgen.model.RightDominance)4 TestUtils.uniqueLong (annis.test.TestUtils.uniqueLong)4 QueryData (annis.ql.parser.QueryData)3