Learning Readable Corpus Queries from a Handful of Example Sentences

A linguist hunting for a construction, such as Portuguese clitic climbing, English tough-movement, or a collocation that only occurs inside relative clauses, usually knows it when she sees it but cannot write the formal query that finds it. One option is to train a neural classifier on a few labelled sentences. That finds many instances, but the linguist cannot see what it is matching, cannot rerun the same search on the next corpus release, and cannot defend the resulting frequency counts in a paper. The other option is a symbolic corpus query, meaning a pattern over part-of-speech tags, morphology and dependency relations written in a language like the Corpus Query Language (CQL). Such a query is transparent and reproducible, but writing it takes expertise and it tends to miss instances. This is the accuracy-interpretability trade-off in a setting where interpretability is a working requirement: corpus linguistics depends on counts that other researchers can reproduce.

We propose to treat the executable query itself as the interpretable model. From three to five positive and negative example sentences, the system learns a short logical combination of linguistic predicates that compiles directly to CQL or a dependency-graph query. A neural "teacher" that judges sentences is never shown to the user as an answer. Its only job is to find where the current query and the evidence disagree. The system then shows the linguist near-miss sentences, each of which fails exactly one constraint of the current query. Each such question asks her, in effect, whether that one constraint should stay. The output is a query she can read, edit and cite. Its explanation is faithful by construction because the explanation and the decision procedure are the same object. The remaining gap between the query and the neural teacher becomes a measured quantity rather than a hidden one.

Core Claim

Our claim is that example-driven query induction, with a vocabulary of Universal Dependencies predicates as the bottleneck and teacher-guided near-miss questions as the interaction, recovers target hit sets more accurately and with fewer user labels than three alternatives: single-example pattern inference, black-box natural-language-to-CQL generation, and free-form semantic concept bottlenecks. We also claim that the sentences the neural teacher retrieves but no short query can capture are informative in their own right. They should concentrate on parser errors and on phenomena the annotation scheme cannot express. If so, the accuracy gap tells the linguist where her annotated corpus is unreliable for her question.

Formally, let $\mathcal{P}$ be a library of boolean predicates over a candidate match. Examples are "token has UPOS=VERB", "head relation is xcomp", "lemma in set S", "precedes token j", and "within a clause". A query $q$ is a sparse disjunction of conjunctions over $\mathcal{P}$, and $|q|$ is its number of predicates. Given user labels $L$ and teacher scores $t(s)\in[0,1]$ on an unlabelled sample $U$, we search

$$q^\ast = \arg\min_{q} \sum_{(s,y)\in L} \ell(q(s),y) + \alpha \sum_{s\in U} \ell(q(s),t(s)) + \lambda |q|,$$

where $\ell$ is a 0/1 loss, $\alpha$ down-weights the teacher relative to the human, and $\lambda$ sets how readable the query must be. Sweeping $\lambda$ traces an explicit accuracy-interpretability curve, with execution F1 plotted against query length.

Relation to Prior Work

The access side supplies both motivation and constraints. Syntactic Search by Example (Spike) infers a dependency-path query from one marked-up example sentence over an Odinson index. We keep its executable target but drop the single-example assumption and the manual markup. We also add disjunction and negation, which Spike cannot express. The Linguist's Search Engine: An Overview lets users generalize a parsed seed sentence by clicking tree nodes, and argues that parser consistency matters more than parser perfection. We test that claim directly by measuring where parser errors land in the query-teacher gap. From Text to CQL: Bridging Natural Language and Corpus Search Engine translates a natural-language request into CQL with BART or GPT-4 and releases the TCQL benchmark. It is our strongest competitor and our evaluation substrate. Its output is already symbolic, so the difference is not that we produce a readable artifact and it does not. The difference is the learning signal: it needs the user to verbalize the phenomenon, and it gives no principled way to repair a wrong query. Its reported weakness on within containment constraints is the case our near-miss questions target.

The interpretability side supplies the method. Concept Bottleneck Models route predictions through human-readable concepts that a person can override. Interactive Concept Bottleneck Models choose which concepts to ask a human about. Our near-miss questioning is that idea applied when the concepts are corpus predicates and the "intervention" is keeping or dropping a constraint. Interpretable-by-Design Text Classification with Iteratively Generated Concept Bottleneck (TBM) grows its concept set from the current model's failures, and we borrow that failure-driven loop. TBM also constrains our design: its free-form semantic concepts trailed black boxes by 19.5% on fake news detection, and they need LLM calls per sentence, which is prohibitive across a corpus of hundreds of millions of tokens. Adaptive Concept Discovery for Interpretable Few-Shot Text Classification separates prototypical concepts (what a class looks like) from discriminative ones (what separates it from a neighbour). We reuse this split: positives seed the conjunctive core, and negatives seed exclusion clauses. Our concepts are executable predicates, not text descriptions matched by embedding. A disentangled linguistic graph model for explainable aspect-based sentiment analysis shows that routing decisions through POS, dependency and tree-distance channels can be accurate and explainable. That supports our choice of predicate vocabulary, but its explanations are internal to a sentiment classifier and cannot be run as searches. Universal Dependencies fixes the 17 POS tags and 37 relations that make the predicate library portable across languages.

Research Program

We will build three components on top of an existing indexed backend, using BlackLab-style CQL as in OpenSoNaR, and Odinson for dependency queries.

The first component is a predicate library compiled from CoNLL-U fields: lemma, UPOS, morphological features, dependency relation and direction, linear order, and sentence or clause containment. Every predicate maps to a CQL or Odinson fragment, so any learned query runs natively on the index.

The second component is the search procedure. It anchors on dependency paths shared by the positive examples, as Spike does, then adds and prunes predicates greedily, or by small integer programming when the candidate set is small, to minimize the objective above.

The third component is the teacher and questioner. The teacher is an LLM judge prompted with the user's examples. It is applied only to a few thousand candidate sentences sampled near the current query's decision boundary, never to the whole corpus. The questioner ranks sentences that the teacher and the query disagree on, plus near-misses that fail exactly one predicate. It then asks the user about the one that most reduces uncertainty over which predicates belong in $q$.

The interface shows the query as a short plain-language gloss beside the CQL. For each predicate, it shows one example the predicate admits and one it excludes.

The analysis asks whether residual teacher-only hits concentrate on sentences where the automatic parse is wrong. To test this, we run identical searches over gold and parser-produced versions of the same UD treebank, such as Portuguese bosque-ud-2.6 as used in ET. We then measure what fraction of the residual disappears under gold annotation.

Baselines

The naive reference point is Spike-style single-example induction: one positive sentence, with its anchor dependency path turned into a query, and no learning. It shows how much the extra examples and the interaction add. We run it on the same corpus index, with the same example drawn from the simulated user's first positive.

The strongest existing method is black-box Text-to-CQL from From Text to CQL. On Chinese TCQL, GPT-4 with three in-context examples reports 67.49% exact match and 77.85% execution accuracy. BART-Chinese reports 46.52% exact match. We will rerun this baseline under a fair protocol. It receives TCQL's natural-language request plus the same positive and negative example sentences our method sees. It is allowed the same number of repair turns as our method asks questions, receiving the current hit list as feedback. Prompt tuning is limited to the same development split. Beating it means higher execution F1 against the gold query's hit set at equal user effort, or equal F1 with clearly fewer labels. We also aim for a larger margin on the Within subset, where its errors are concentrated.

The accuracy ceiling is a black-box sentence classifier: an encoder fine-tuned on all labels collected in a session, plus the teacher's pseudo-labels. We report how far the best query of each length falls below it. A TBM-style semantic bottleneck run with the same labels shows whether free-form concepts can stand in for executable predicates.

The ablation that isolates the key ingredient replaces near-miss and disagreement questioning with randomly chosen sentences to label, keeping the label budget fixed. A second ablation restricts queries to single conjunctions, which matches Spike's expressivity.

Why not just…?

Why not just prompt an LLM to write the CQL and let the linguist inspect the hits? This is the real threat, because the LLM's output is already a readable query. Its reported 67.49% exact match leaves roughly a third of TCQL queries wrong, with containment constraints hardest. When a query is wrong, the user has no structured way to locate which constraint is at fault. Our head-to-head under equal repair turns settles whether near-miss questions beat free-form feedback. We also test a hybrid in which the LLM's query initializes our search. If the hybrid ties the plain LLM, the contribution shrinks to an interface, and we will say so.

Why not just use a neural classifier with saliency maps? It will likely retrieve more, which is why it serves as our ceiling. But its hits cannot be regenerated on a new corpus release without the model, and a saliency map does not tell the user which grammatical condition to change. The DLGM authors report that attribution-based explanations are coarse and miss syntactic dependencies (DLGM). We quantify the cost through the measured gap and through reproducibility: rerunning on a re-parsed corpus changes classifier outputs but leaves a query's definition fixed.

Why not just intersect the dependency patterns of several positive examples? This is a cheap extension of Spike and could capture most of the gain on simple collocation queries. We include it as a baseline. It cannot learn exclusions from negative examples or form disjunctions. We expect it to fail mainly on Condition and Within queries, and the per-type breakdown will show whether that holds.

Why not just use TBM-style free-form concepts? Its concepts are sentence-level semantic judgments that must be scored by an LLM on every sentence, so they cannot be executed over an index. TBM also lost 19.5% to black boxes on a structurally demanding task (TBM).

Proposed Experiments

The main benchmark is a simulated-user version of TCQL. For each gold query, we execute it to obtain a gold hit set. A simulated user supplies k ∈ {1, 3, 5} positive and negative examples and answers questions truthfully according to gold membership. The primary metric is execution F1 of the learned query against the gold hit set. Secondary metrics are the number of labels needed to reach F1 0.9, query length, and exact or AST-level match via CQLBLEU, broken down by Simple, Condition and Within. This requires the TCQL corpora to be indexed; if they are not released, we re-index EnWiki and regenerate gold hit sets. TCQL is template-built and collocation-heavy, so it underrepresents syntactic phenomena. We therefore add a second suite of dependency-defined constructions written as gold Odinson queries over UD treebanks. Performance on this suite is reported separately. Since no reported result was found on Lacuna for example-based query induction on UD, the baselines above define the reference numbers for that suite.

The decisive checks are threefold. The first is whether the method beats rerun GPT-4 Text-to-CQL on execution F1 at equal user effort, particularly on Within queries. The second is whether near-miss questioning beats random labelling at a fixed budget; if it does not, the interaction contributes nothing. The third is whether more than half of the teacher-only residual on bosque-ud-2.6 disappears when gold parses replace predicted ones; if it does, the query-teacher gap really does locate parser error. A small user study with linguistics students, following the qualitative protocol of OpenSoNaR, will test whether real users reach correct queries in fewer turns and can correctly paraphrase what their final query matches.