Treebank StudioDocumentation

PaCQL tutorial (for "pack-well", the Parsed Corpus Query Language)

This is an introduction to the PaCQL query language using examples from IcePaHC 2024.03, the Icelandic Parsed Historical Corpus. The best way to use this document is to try out the queries as you go through the material. Note that although this introduction will get you started, you will need to consult further sources of information in order to use parsed corpora for advanced work.

PaCQL ("pack-well") is designed to meet the following criteria:

Because many linguistic researchers are familiar with CorpusSearch (henceforth CS), this guide will occasionally point out similarities and differences between the two systems. There is considerable overlap in the functionality of the two systems and it should be easy to transfer knowledge of one system to the other.

Basic concepts

We will start right away with the first query. Type the following PaCQL query into the search box and click the Run button (or press Ctrl+Enter).
NP-SBJ idoms N-D
In the IcePaHC 2024.03 corpus, this query matches 835 noun phrases (NP) which are subjects (NP-SBJ) and which immediately dominate (idoms) a singular common noun (N) in the dative case (N-D). Dative subjects are a well known property of Icelandic syntax so we are already seeing something linguistically interesting!

The node you name first in the query, NP-SBJ in this case, becomes the "anchor" for the query (another node can be made the anchor by marking it with @; see the reference). The labels you type in a query (like NP-SBJ) are names for nodes: the same name written twice means the same node, as we will see below. The query answers the question: For which anchors in the corpus does the pattern described by the query hold? By default, the box "Anchor only" is checked and this means that the output will display only the part of the tree which is the matched anchor.
(NP-SBJ
  (Q-D öllu)          'all'-dative
  (N-D mál$)          'language'-dative
  (D-D $inu))         'the'-dative
The full text of the tree which contains the anchor is displayed above the anchor tree. Note that the words of the matching anchor are highlighted inside the full text for the matching tree when you are looking at the results, and that every node the query matched has a small label at its first word, so you can see where the NP-SBJ begins in the sentence. To interpret and analyze an example, it is often important to see its preceding and following context. For this, we have the "More context" button at the right side of each result's header line. Pressing it shows three sentences before and after it. When done reading the context, you can press "Less context" to hide them. Try unselecting "Anchor only". Each tree in which there is a matching anchor will now be displayed in full so you may have to scroll down to find the dative subject. The labels NP-SBJ and N-D in the example above are highlighted in the tree so they will still stand out.

In addition to using colors to highlight important parts of the results, the query box highlights the names of search functions like idoms, which is a relation between two nodes. If relations turn blue as you type, you can be sure that you typed them correctly. The results are shown on the page by default. Above the results there is a "Download TSV" button, which gives you tab-separated values corresponding to the coding values shown above each result tree, as well as the raw text for the relevant tree. The current query only gives us unique identifiers for the text and the tree, but the TSV format will become more interesting as we start coding for more variables.

Below the query box, next to the "Results" tab where it says "835 results" after you run the query, there is a "Summary" tab. Clicking the tab will give you a breakdown of the results by text and century. The following columns are shown in the text table.

ColumnInterpretation
results (r/T)results per text
result trees (rt/T)result trees per text (fewer than r/T if >1 result per tree)
trees (t/T)total trees per text
per 1,000 words (r/1Kw)results per 1000 words for this text

The last of these, r/1Kw, can be used as a rough measure of the frequency of a particular syntactic pattern. The section about coding queries below will introduce more sophisticated ways of studying syntactic change. The table which shows results by century is similar.

ColumnInterpretation
results (r/C)results per century
result trees (rt/C)result trees per century (fewer than r/C if >1 result per tree)
trees (t/C)total trees per century
per 1,000 words (r/1Kw)results per 1000 words for this century

The century table is accompanied by a visualization which displays the development of r/1Kw over time. The drop in dative subject in the 16th century is due to the New Testament translation of Oddur Gottskálksson. He moved to Norway at a young age and perhaps it is for that reason that diagnostics for oblique subjects are weak in his language. Translation effects from Luther are also a plausible source of odd patterns. The reader may feel free to use Treebank Studio to develop their own explanation of these facts!

Adding a second relation

If we want to look at dative subjects in finite subordinate clauses, we can use the following query.
IP-SUB idoms NP-SBJ
NP-SBJ idoms N-D
IP-SUB matches finite subordinate clauses and the two instances of NP-SBJ in the query are taken to be the same noun phrase. A label for a node, like NP-SBJ, is a name. When you put the cursor on a name, every place where the same node is named gets a light shade. This query means that the IP-SUB should immediately dominate a phrase NP-SBJ and this same phrase should dominate a dative noun N-D. IP-SUB is the first name in the query, so it is the anchor. There are 260 results in the corpus which match this pattern, one of which is the following:
(IP-SUB
  (NEG eigi)
  (BEPS sé)
  (NP-SBJ
    (Q-D öllu)
    (N-D mál$)
    (D-D $inu))
  (ADVP (ADV orðfimlega) (CONJP *ICH*-4))
  (VAN farið)
  (CONJP-4 (CONJ eða) (ADVP (ADV skörulega)))) 
If you want to search for exactly the dative subjects which include quantifiers or determiners, but may or may not include other elements, you can use the following queries.
IP-SUB idoms NP-SBJ
NP-SBJ idoms Q-D
IP-SUB idoms NP-SBJ
NP-SBJ idoms D-D
However, if you want to capture all dative subjects in the same query, you will need to use more advanced pattern matching. For this purpose, we can use regular expressions.

Using regular expressions

In PaCQL, every label pattern in a query is a regular expression. A full treatment of regular expressions is beyond the scope of this guide, but a few basic patterns demonstrate the power of this tool. First, a list of characters inside a bracket matches any one of these characters. The following therefore matches all IP-SUB clauses whose subject immediately dominates one of the labels N-D, Q-D or D-D.
IP-SUB idoms NP-SBJ
NP-SBJ idoms [NQD]-D
A parenthesized list of strings of characters where the strings are separated by the pipe symbol (|) matches any of the strings on the list. We can use this technique to expand the search to include finite main clauses IP-MAT.
IP-(MAT|SUB) idoms NP-SBJ
NP-SBJ idoms [NQD]-D
In fact, there are some more part-of-speech tags beyond N-D, Q-D and D-D which are used to annotate dative elements in the corpus. For example, we have not looked at dative pronouns yet, PRO-D. As the reader may have noticed, all the dative tags end with the "dash tag" -D and we can use the regular expression .*-D "dot star" to capture all of these dative tags in a simple manner. The formal meaning of the star is "zero or more repetitions of the preceding character" and the dot is a special symbol which matches any character! We are now up to 3786 dative subjects!
IP-(MAT|SUB) idoms NP-SBJ
NP-SBJ idoms .*-D

Coding queries

When doing quantitative analysis, we usually want a table with columns where each example of interest is coded for whichever variables are under investigation. Coding queries are used for this purpose. In PaCQL, we always get some columns in the output and it is always possible to download a .tsv file for further analysis. But to study a syntactic pattern, we most often need to define the dependent variable and let the query capture when pattern x applied (the 1 values), and when it could have applied but did not (the 0 values). The query below defines the dependent variable "ov" whose value is 1 if "ov:1" holds but 0 if "ov:0" holds. The values are tried in the order written and each result gets the first one that holds; results for which no value holds are left out. As in the other examples above, this is a query for IcePaHC.
IP-(MAT|SUB) idoms MDPI

ov:1
MDPI sprec NP-OB[12]
NP-OB[12] sprec VB   

ov:0
MDPI sprec VB
VB sprec NP-OB[12]

We have already learned about finite clauses and the regular expression IP-(MAT|SUB) captures both main clauses and subordinate clauses. The (A|B) notation is regular expression syntax that means that either of the options A or B will match. The query has a main section which is the only PaCQL section that does not have a section header with a column, you can just type the main section without any introduction, and here the line IP-(MAT|SUB) idoms MDPI is the main section. Here, MDPI is a modal (MP) in the present tense (P) and here (I) stands for the indicative. There are more tags for modals, but this is just an example. The important part here are the two coding sections. These sections name a variable, ov, and section ov:1 describes a pattern that, combined with the main section, is the sentences where OV syntax applies. The relation sprec stands for sisterwise precedence, so the modal is the sister of some object (NP-OB[12]) and the same object is the sister of a main verb in the infinitive (VB). The [12] pattern is also regular expression syntax for one character alternatives, matching both direct (NP-OB1) and indirect (NP-OB2) objects here.

So, in a finite clause with a finite modal, if the modal sisterwise-precedes sprec an object that sisterwise-precedes a main verb, this result gets code 1 in the ov column. But if the modal sisterwise-precedes the main verb, and the main verb sisterwise-precedes the object, the ov column gets code 0. And these are good diagnostics for OV vs. VO syntax. This is exactly the kind of data that is useful for analysis. These few lines, that make up the query, do the kind of work that has sometimes taken a series of queries and shell scripts and post-processing steps to achieve. For this kind of work, PaCQL is simpler than people in historical syntax may be used to. Not all of it is simple, of course. For the above example, you still need to know a bit about regular expressions, and you also have to understand how the annotation of the treebank works, and you have to know how the syntax of the language (Icelandic) works, the linguistic stuff. However, if you do, the logistics of extracting this distribution from the corpus can be easier with PaCQL than some of the alternatives.

Meta coding

It is also important to be able to add other types of columns to the result set. Meta coding adds some of these columns in a very simple way. The meta block in the query has to be separated from the rest of the query by a blank line like other sections. The first line of the meta section has to be "meta:" and this first line must be followed by one or more meta lines. Let's add to the example we are working with.

IP-(MAT|SUB) idoms MDPI

ov:1
MDPI sprec NP-OB[12]
NP-OB[12] sprec VB   

ov:0
MDPI sprec VB
VB sprec NP-OB[12]

meta:
text century
text year
tree treewords
node nodewords NP-OB[12]

In terms of matching a syntactic pattern, this is exactly the same query as the previous one, it just has the added meta: section.

For text level meta coding, it varies depending on the corpus which columns are available. IcePaHC offers century and year, for example, and they have now been added to the output. We can also have tree level meta coding, and here we use treewords, the number of words in the tree (and indicator for whether this is a long sentence). We also have node level meta coding, and node nodewords NP-OB[12] adds a column for how many words there are in the object that was matched. This column is interesting to have if you want to study how heavy phrases behave. Additionally, all queries return the text id of each text, the id of the tree, and the text of the tree that was matched. All of this together outputs a result set with several columns that are all meaningful for the analysis. The PaCQL query does the type of work here that would have required a much more complex setup in some earlier studies. Here are some examples of columns supported when using IcePaHC.

Text level meta coding: The text attributes of the are corpora on Treebank Studio are listed in the reference. For the tree level and node level meta coding, these are the possibilities that are currently offered: Tree level meta coding: Node level meta coding: The columns will all have some default column name, and you can just use that if you want, but you can also name the columns yourself. A column can be given a name of your own, by writing the name first: node heaviness:nodewords NP-OB1 gives the column heaviness instead of nodewords.
IP-(MAT|SUB) idoms MDPI

ov:1
MDPI sprec NP-OB[12]
NP-OB[12] sprec VB   

ov:0
MDPI sprec VB
VB sprec NP-OB[12]

meta:
text period:century
text year
tree sentence_length:treewords
node heaviness:nodewords NP-OB[12]

Definition section

Writing the patterns that match the nodes repeatedly can be annoying, especially if they involve a lot of regular expression syntax, and one must be careful to spell them exactly the same if they are to refer to the same node in the structure. This is where definition sections come in, titled define:. In such a section, you can write a descriptive word before the pattern you want to match and then just use that word in the query instead of the pattern itself. The exact same query as before is shown below but now using definitions.

define:
clause IP-(MAT|SUB)  
modal MDPI
object NP-OB[12] 

clause idoms modal

ov:1
modal sprec object
object sprec VB   

ov:0
modal sprec VB
VB sprec object

meta:
text period:century
text year
tree sentence_length:treewords
node heaviness:nodewords NP-OB[12]

This is often easier to type, less likely to contain errors, and often easier to read. Once you have gotten used to sprec meaning sisterwise precedence, the line modal sprec object is very clear. Notice that we keep getting exactly 673 results, a sign that the queries are equivalent with respect to the results that they return.

Combining syntactically defined coding variables

Coding gets more interesting when we add more coding variables to the same query. Consider this one:

define:
clause IP-(MAT|SUB)  
modal MDPI
object NP-OB[12] 

clause idoms modal
object domswords> 0

ov:1
modal sprec object
object sprec VB   

ov:0
modal sprec VB
VB sprec object

pronoun:1
object idoms PRO.* 

pronoun:0 

light:1
object domswords 1

light:0
object domswords> 1

meta:
text period:century
text year
tree sentence_length:treewords
node heaviness:nodewords object

Here, object domswords> 0 is added as a check to make sure that the objects being matched are not empty categories. An object that is more than zero words is overt. In this case, we get the same results, but it never hurts to double check. The query adds two coding variables, pronoun checks whether the matched object is a pronoun, using object idoms PRO.*. Tags for pronouns start with PRO, so they are matched by this pattern. A coding section that only has a header and no query lines below it, like pronoun:0, is a section that matches the elsewhere pattern, all results that match the other parts of the query and do not get 1 for pronoun, get a 0. The coding variable light is similar, it gives the code 1 if the object is exactly one word, and 0 otherwise. The light objects would include a lot of pronouns, but also some single noun objects. By the way, you can use any kind of a value for the output of the column, not just 1 an 0, for example, it would be fine to use the header light:yes to give the value yes to all the outputs that match that section.

Logic variables

We can use the columns built in the last section to introduce a new concept in PaCQL, which is logic variables. The next query adds one of these to the previous one:
define:
clause IP-(MAT|SUB)  
modal MDPI
object NP-OB[12] 

clause idoms modal
object domswords> 0

ov:1
modal sprec object
object sprec VB   

ov:0
modal sprec VB
VB sprec object

pronoun:1
object idoms PRO.* 

pronoun:0 

light:1
object domswords 1

light:0
object domswords> 1

?pronounorlight:1
(pronoun == 1 or light == 1) and not (pronoun == 1 and light == 1) 

?pronounorlight:0

meta:
text period:century
text year
tree sentence_length:treewords
node heaviness:nodewords object
A logic variable heading starts with a question mark, ?, and its lines are logic expressions that use the logic operators and, or, not over the output of the other columns. So here, the expression (pronoun == 1 or light == 1) and not (pronoun == 1 and light == 1) uses an exclusive or to match results that are pronouns or light but not both. It would code as 1 the light nouns and and heavy pronoun phrases (which would be something like a pronoun object with a relative clause, perhaps). This mechanism adds power to how columns can be combined in the output in PaCQL.

Syntactic relations

Some of the most common relations are listed here; the reference and the relations page have all of them. If you are new to searching treebanks, no need to remember all of those right away, but if you are an experienced CorpusSearch user, the list will give you a sense of how familiar concepts are expressed in PaCQL.

Two-node relations (A relation B):

One-node relations (they test a node that the two-node relations found): Finding a node anywhere in the tree, with one pattern (see the reference):

Hopefully, this has been a useful introduction to the query language for you. Feel free to explore the other parts of the documentation for more details.