% Copyright © 2004-2009 Adam Przepiórkowski
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA.

\author{Adam Przepiórkowski, Jakub Wilk}

\section{Query Language}
\label{sec:query-syntax}

Poliqarp's query syntax is based on that of Corpus Query Processor (CQP),
perhaps the most popular program of this kind, created at the University of
Stuttgart, but it contains a number of additional features and improvements.
\footnote{%
Although the query syntax of Poliqarp is based on that of CQP, Poliqarp was
implemented from scratch within the current project and it does not contain
any CQP code.} The present section describes the syntax of Poliqarp queries
and illustrates it with numerous examples.

\subsection{Searching for orthographic forms}
\label{sec:zapytania-o-formy}

In the simplest case, a query is just a sequence of segments, e.g.:

\begin{itemize}
\item \texttt{przyszedł czas}
\item \texttt{przyszedł em rano}
\end{itemize}

There are three segments in the latter query above, corresponding to two
words: \emph{przyszedłem} and \emph{rano}.  In the case of simple queries
like the two queries above, Poliqarp attempts to identify those words which
might consist of smaller segments and to handle them properly, so also the
following queries will give the expected results:

\begin{itemize}
\item \texttt{przyszedłem rano}
\item \texttt{długom szedł}
\end{itemize}
In case of the latter query, Poliqarp will find all occurrences of the
three-segment sequence \emph{[długo][m] [szedł]}, interpretable as an adverb
(\emph{długo} `long'), an agglutinate (\emph{-m} `be'), and an l-participle
(\emph{szedł} `walk, go'), as well as all occurrences of the two-segment
sequence \emph{[długom] [szedł]}, where the first segment is interpreted as
a dative nominal form (\emph{długom} `debts'), and the second -- again, as
an l-participle.

By default, queries are interpreted in a case-sensitive manner, so the
following queries will produce different results:

\begin{itemize}
\item \texttt{przyszedł}
\item \texttt{Przyszedł}
\end{itemize}

In order to find all occurrences of the form \emph{przyszedł}, regardless of
case, the flag \texttt{/i} should be used.  Thus, the two queries below will
produce the same results, which will in particular contain all results of
both queries above.

\begin{itemize}
\item \texttt{przyszedł/i}
\item \texttt{Przyszedł/i}
\end{itemize}

Both in the graphical version and in the text version of Poliqarp, case
sensitivity can be set globally, for a whole query or a series of queries.

Queries may contain standard regular expressions over characters, specified
with the help of the following special characters: \texttt{?}, \texttt{*},
\texttt{+}, \texttt{.}, \texttt{,}, \texttt{|}, \texttt{\lbr{}}, \texttt{\rbr{}},
\texttt{[}, \texttt{]}, \texttt{(}, \texttt{)}, as well as natural numbers;
segment specifications containing regular expressions must be enclosed in
quotes \texttt{"}.  Since the formal introduction of regular expressions
lies far outside the scope of the current publication, we will be content
with discussing just a few examples, which, nevertheless, should allow the
user to understand the syntax and semantics of such regular expressions.

\begin{enumerate}

\item \texttt{"Ala|Ela"}

the character \texttt{|} introduces the alternative of two expressions, so
the query above can be used to find all occurrences of segments of the form
\emph{Ala} or \emph{Ela}, \item \texttt{"[AE]la}

square brackets denote the alternative of characters within them, so the
query above can be used to find those segments whose first character is
\emph{A} or \emph{E}, and the following two characters are \emph{la}, i.e.,
this query is equivalent to the previous query,

\item \texttt{"beza?"}

the question mark signals the optionality of the character or the expression
in parentheses which immediately precedes it, so the question above will be
used find all occurrences of the segments \emph{bez} and \emph{beza},

\item \texttt{"bez."}

the period denotes any character, so the results of this query will include
\emph{beza}, \emph{bezy}, \emph{bezą}, etc.,  but not \emph{bez} or
\emph{bezami},

\item \texttt{"bez.?"}

\emph{bez}, \emph{beza}, \emph{bezy}, \emph{bezą}, etc., but not
\emph{bezami},

\item \texttt{".z.z."}

5-character segments, where 2nd and 4th characters are \emph{z} (e.g.,
\emph{czczą} and \emph{rzezi}),

\item \texttt{".z.z..?"}

segments of length 5 or 6, where 2nd and 4th characters are \emph{z} (e.g.,
\emph{czczą}, \emph{rzezi} and \emph{szczyt}),

\item \texttt{"a*by"}

the asterisk denotes any number of occurrences of the character or the
expression in parentheses which immediately precedes it, so this query can
be used to find segments beginning with any number of \emph{a}s, followed by
\emph{by}, e.g., \emph{by} (zero occurrences of \emph{a}), \emph{aby},
\emph{aaaaby}, etc.,

\item \texttt{"Ala.*"}

segments beginning with \emph{Ala}, e.g., \emph{Ala} and \emph{Alabama},

\item \texttt{"ala.*"/i}

segments beginning with \emph{ala}, \emph{Ala}, \emph{aLa}, \emph{ALA},
etc., e.g., \emph{Ala}, \emph{alabaster} and \emph{ALABAMA},

\item \texttt{".*al+"}

the plus has a similar interpretation as the asterisk: it denotes any number
\emph{greater than zero} of occurrences of the character or the expression
in parentheses which immediately precedes it, so this query can be used to
find segments ending in \emph{al}, \emph{all}, \emph{alll} etc., but not in
\emph{a}, e.g., \emph{dal}, \emph{robal} and \emph{Gall},

\item \texttt{"a\lbr{}1,3\rbr{}b.*"/i}

the expression of the form \texttt{{n,m}} denotes from \texttt{n} to
\texttt{m} occurrences of the character or the expression in parentheses
which immediately precedes it; in this case, the query above can be used to
find segments beginning with 1 to 3 occurrences of \emph{a} or \emph{A},
followed by \emph{b} or \emph{B}, and then followed by any sequence of
characters, e.g., \emph{Aby}, \emph{aaaby}, \emph{absolutnie}, \emph{ABBA},

\item \label{flank} \texttt{".*(la)\lbr{}3,\rbr{}.*"}

\texttt{{n,}} means at least \texttt{n} occurrences, so this query will help
to find segments which contain at least three occurrences of the sequence
\emph{la} in a row, e.g., \emph{tralalala}, \emph{sialalala},

\item \texttt{"[bcćdfghjklłmnńprsśtwzźż]\lbr{}4,\rbr{}[aąeęioóuy]"/i}

segments consisting of at least 4 consonants and exactly 1 vowel,
  e.g., \emph{źdźbła} i \emph{Chrzczę},

\item \texttt{"([bcćdfghjklłmnńprsśtwzźż]\lbr{}3\rbr{}[aąeęioóuy]){2,}"/i}

segments consisting of at least two sequences of the type CCCV, where C is a
consonant, and V is a vowel, e.g., \emph{wszystko}, \emph{Zdmuchnąwszy}
i~\emph{Szmajdziński}; \texttt{{n}} means exactly \texttt{n} occurrences,

\item \texttt{"([\textasciicircum{}aąeęioóuy]\lbr{}3\rbr{}[aąeęioóuy]){2,}"/i}

as above,

\item \texttt{"(pod|na|za)jecha.*"}

segments beginning with \emph{podjecha}, \emph{najecha} or \emph{zajecha},
e.g., \emph{podjechał}, \emph{zajechawszy}.

\end{enumerate}

The specifications of segments given above must match complete segments,
rather than only their parts, hence the necessity of flanking the sequence
\texttt{(la)\lbr{}3,\rbr{}} in query \ref{flank}. above with the regular expression
\texttt{.*}, matching any sequence of characters (also the empty sequence).
The same effect can be achieved with the help of the flag
\texttt{/x},\label{flagax} which means that the given specification must be
matched by a subsequence of the segment, not necessarily by the complete
segment:

\begin{itemize}
\item \texttt{"(la)\lbr{}3,\rbr{}"/x}

segments which contain at least three occurrences of the sequence \emph{la}
in a row, e.g., \emph{tralalala}, \emph{sialalala},

\item \texttt{"(la)\lbr{}3,\rbr{}"/ix}

segments which contain a sequence like \emph{lalala}, \emph{LaLAla}, etc.,
e.g., \emph{tralalala}, \emph{SiaLaLALA}.

\end{itemize}

\subsection{Searching for base forms}
\label{sec:zapytania-o-formy-1}

The following query may be used in order to find all forms of the lexeme
\textsc{korpus}:

\begin{itemize}
\item \texttt{[base=korpus]}
\end{itemize}

The \texttt{base} attribute is one of many attributes that may be used in a
query.  The value of this attribute should specify the base form (the
lemma), so a query like \texttt{[base=pisać]} can be used to find forms such
as \emph{pisać} `write' (infinitive), \emph{piszę} (non-past form),
\emph{pisała} (l-participle), \emph{piszcie} (imperative), \emph{pisanie}
(gerund), \emph{pisano} (impersonal), \emph{pisane} (adjectival participle),
etc.

Another attribute that may be used in queries is \texttt{orth}.  The values
of this attribute specify segments, so each of the following pairs contains
queries which are equivalent.

\begin{itemize}
\item \begin{itemize}
\item \texttt{przyszedł}
\item \texttt{[orth=przyszedł]}

\end{itemize}
\item \begin{itemize}
\item \texttt{Przyszedł/i}
\item \texttt{[orth=Przyszedł/i]}

\end{itemize}
\item \begin{itemize}
\item \texttt{przyszedł czas}
\item \texttt{[orth=przyszedł][orth=czas]}

\end{itemize}
\end{itemize}

On the other hand, the two queries below are \emph{not} equivalent:

\begin{itemize}
\item \begin{itemize}
\item \texttt{przyszedłem rano}
\item \texttt{[orth=przyszedłem][orth=rano]}

\end{itemize}
\end{itemize}

In the first case, Poliqarp will guess that the word \emph{przyszedłem} may
consist of two segments, \emph{przyszedł} and \emph{em}, and will expand the
query accordingly, as described in~\S{}\ref{sec:zapytania-o-formy}.  In
contrast, the value of \texttt{orth} is always interpreted as the
specification of a single segment.

The values of \texttt{base} and \texttt{orth} may contain regular
expressions of the kind described in~\S{}\ref{sec:zapytania-o-formy} above,
e.g.:

\begin{itemize}

\item \texttt{[orth="bez.?"/i]}

find segments \emph{bez}, \emph{Beza}, \emph{bezy}, etc., but not
\emph{bzem} or \emph{bezami},

\item \texttt{[base="bez.?"/i]}

find all segments whose base form has 3 or 4 characters and starts with
\emph{bez} (understood in a case-insensitive manner), e.g., the segments
\emph{bzem}, \emph{bez}, \emph{bezami}, etc.

\end{itemize}

\subsection{Higher order queries}
\label{sec:zapyt-wysz-rzdu}

Queries about segments and about base forms may be combined.  For example,
the following query may be used to find all occurrences of the segment
\emph{minę} understood as a form of the lexeme \textsc{mina} `mine, face'
(and not, say, as a form of the lexeme \textsc{mijać}, `to pass'):

\begin{itemize}
\item \texttt{[orth=minę \& base=mina]}

\end{itemize}

A similar effect can be achieved with the help of the following query, about
those occurrences of the segment \emph{minę} which are \emph{not}
interpreted as forms of \textsc{mijać}.

\begin{itemize}
\item \texttt{[orth=minę \& base!=mijać]}
\end{itemize}

The condition that the base form be different from \emph{mijać} may also be
specified by putting the negation (the exclamation mark) before the name of
the attribute, so the query below is equivalent to the query above.

\begin{itemize}
\item \texttt{[orth=minę \& !base=mijać]}
\end{itemize}

Just as in the propositional calculus, double negation is equivalent to no
negation, so the following queries about the segment \emph{nie} understood
as a form of the pronoun \textsc{on} are fully equivalent:

\begin{itemize}
\item \texttt{[orth=nie \& base=on]}
\item \texttt{[orth=nie \& !base!=on]}
\item \texttt{[orth=nie \& !!!base!=on]}
\item \texttt{[orth=nie \& !!base=on]}
\end{itemize}

In Poliqarp queries, the operator \texttt{\&} plays the role of logical
conjunction.  The operator dual to \texttt{\&} is \texttt{|}, which plays
the role of logical disjunction, e.g.:

\begin{itemize}

\item \texttt{[base=on | base=ja]}

find all forms of lexemes \textsc{on} and \textsc{ja}, equivalent to
\texttt{[base="on|ja"]},

\item \texttt{[base=on | orth=mnie | orth=ciebie]}

find all forms of the lexeme \textsc{on}, as well as the segments
\emph{mnie} and \emph{ciebie},

\item \texttt{[orth=pora \& !(base=por | base=pora)]}

find those occurrences of the segment \emph{pora} which are not interpreted
as forms of the lexemes \textsc{por} or \textsc{pora}.

\end{itemize}

In order to better understand the difference between the operators
\texttt{\&} and \texttt{|}, let us compare the effect of the following two
queries:

\begin{itemize}
\item \texttt{[orth=minę \& base=mina]}
\item \texttt{[orth=minę | base=mina]}
\end{itemize}

The result of the former query will consist of those segments which
simultaneously (conjunction) have the orthographic form \emph{minę} and are
interpreted as a form of the lexeme \textsc{mina}.  On the other hand, the
result of the latter query will consists of segments which either
(disjunction) have the orthographic form \emph{minę}, regardless of the
interpretation of this segment, or are a form of the lexeme \textsc{mina}
(e.g., \emph{mina}, \emph{miny}, \emph{minami}).  Hence, the latter query
should return many more results than the former query.

As the examples above show, specifications of corpus positions, enclosed in
square brackets, may contain any number of conditions of the type
\texttt{attribute=value}, combined with the operators \texttt{!},
\texttt{\&} and \texttt{|}.  It is also possible to completely omit any
conditions -- the query below could be used to find all segments in the
corpus.%
\footnote{%
Could be, if not for the fact that Poliqarp contains various internal
constraints on the number of results of a query.}

\begin{itemize}
\item \texttt{[]}
\end{itemize}

This trivial specification of corpus positions, matching any segment, may be
useful for finding two forms in a certain distance from each other, e.g.,
two segments separated by two other segments, as in the following query:

\begin{itemize}
\item \texttt{[orth=się][][][base=bać]}
\end{itemize}

The result of this query will include sequences such \emph{się nikogo nie
bać}, \emph{się Boga nie boicie}, etc.

It would perhaps be more interesting to specify the \emph{upper} limit on
the number of segments which may intervene between two forms, not just the
exact number of such intervening positions.   Poliqarp makes it possible to
pose such queries, as it allows to posit regular expressions also over
corpus positions.  For example, the following query may be used to find a
form of the lexeme \textsc{bać} occurring two, three or four positions after
the segment \emph{się}:

\begin{itemize}
\item \texttt{[orth=się][]\lbr{}2,4\rbr{}[base=bać]}
\end{itemize}

The result of this query will contain all the sequences found by the
previous query, as well as sequences such as \emph{się każdy następny Rywin
będzie bał}.

A more accurate query concerning various occurrences of the inherently
reflexive verb \textsc{bać się} should find \emph{się} within a certain
window before a form of the lexeme \textsc{bać}, but without any intervening
punctuation (intervening punctuation will often indicate clause boundary),
or immediately after a form of \emph{bać}, separated from that form by at
most a single personal pronoun:

\begin{itemize}
\item \texttt{[orth=się][orth!="[.!?,:]"]\lbr{},5\rbr{}[base=bać] \\
| [base=bać][base="on|ja|ty|my|wy"]?[orth=się]}
\end{itemize}

\subsection{Searching for tags}
\label{sec:zapyt-o-znaczn}

The rather baroque query above can be simplified by replacing the condition
\texttt{orth!="[.!?,:]"} with a direct reference to the `grammatical class'
\emph{interp}:

\begin{itemize}
\item \texttt{[orth=się][pos!=interp]\lbr{},5\rbr{}[base=bać] \\
| [base=bać][base="on|ja|ty|my|wy"]?[orth=się]}
\end{itemize}

In general, the values of the \texttt{pos} attribute are the abbreviations
of names of grammatical classes discussed in~\S{}\ref{sec:klasy-gramatyczne}
(cf.~the table in \ref{skrklasflek}).  For example, a query about a
sequence of two nominal forms beginning with an \emph{a} may be formulated
as follows:

\begin{itemize}
\item \texttt{[pos=subst \& orth="a.*"]\lbr{}2\rbr{}}
\end{itemize}

The specifications of the values of \texttt{pos} may, just as in case of
\texttt{orth} and \texttt{base}, contain regular expressions.  For example,
taking into account the fact that personal pronouns are split between the
class of 3rd person pronouns \emph{ppron3} and non-3rd person pronouns
\emph{ppron12}, the following queries may be used to find any form of any
personal pronoun:

\begin{itemize}
\item \texttt{[pos=ppron12 | pos=ppron3]}
\item \texttt{[pos="ppron12|ppron3"]}
\item \texttt{[pos="ppron(12|3)"]}
\item \texttt{[pos="ppron[123]+"]}
\item \texttt{[pos="ppron.+"]}
\item \texttt{[pos=ppron/x]}
\end{itemize}

That means that the query about \textsc{bać się} may be further simplified:

\begin{itemize}
\item \texttt{[orth=się][pos!=interp]\lbr{},5\rbr{}[base=bać] \\
| [base=bać][pos=ppron/x]?[orth=się]}
\end{itemize}

Apart from the specifications of segments (with the help of \texttt{orth}),
base forms (\texttt{base}) and grammatical classes (\texttt{pos}), queries
may contain specifications of particular grammatical categories, such as
case or gender.  The following attributes may be used to this end
(cf.~\S{}\ref{sec:kat}):

\begin{tabular}{llll}
\textbf{attribute} &
\textbf{possible values}
\\
\texttt{number} &
\texttt{sg}  \texttt{pl}
\\
\texttt{case} &
\texttt{nom}  \texttt{gen}  \texttt{dat}
  \texttt{acc} \texttt{inst} \texttt{loc}  \texttt{voc}
\\
\texttt{gender} &
\texttt{m1}  \texttt{m2}  \texttt{m3}  \texttt{f}  \texttt{n}
\\
\texttt{person} &
\texttt{pri}  \texttt{sec}  \texttt{ter}
\\
\texttt{degree} &
\texttt{pos}  \texttt{comp}  \texttt{sup}
\\
\texttt{aspect} &
\texttt{imperf}  \texttt{perf}
\\
\texttt{negation} &
\texttt{aff}  \texttt{neg}
\\
\texttt{accentability} &
\texttt{akc}  \texttt{nakc}
\\
\texttt{post-prepositionality} &
\texttt{npraep}  \texttt{praep}
\\
\texttt{accommodability} &
\texttt{congr}  \texttt{rec}
\\
\texttt{agglutination} &
\texttt{agl}  \texttt{nagl}
\\
\texttt{vocalicity} &
\texttt{nwok}  \texttt{wok}
\end{tabular}

Hence, it is possible to pose the following queries:

\begin{enumerate}
\item \texttt{[number=sg]}

find singular forms

\item \texttt{[pos=subst \& number=sg]}

find singular nominal forms

\item \texttt{[pos=subst \& gender!=f]}

find masculine and neuter nominal forms

\item \label{bezskr} \texttt{[number=sg \& case="nom|acc" \&
gender="m[123]"]}


find singular masculine forms in the nominative or in the accusative case

\end{enumerate}

The following three-letter abbreviations may be used instead of the full
names of the attributes:

\begin{tabular}{llll}
\textbf{attribute} &
\textbf{abbreviation}
\\
\texttt{number} &
\texttt{nmb}
\\
\texttt{case} &
\texttt{cas}
\\
\texttt{gender} &
\texttt{gnd}
\\
\texttt{person} &
\texttt{per}
\\
\texttt{degree} &
\texttt{deg}
\\
\texttt{aspect} &
\texttt{asp}
\\
\texttt{negation} &
\texttt{neg}
\\
\texttt{accommodability} &
\texttt{acm}
\\
\texttt{accentability} &
\texttt{acn}
\\
\texttt{post-prepositionality} &
\texttt{ppr}
\\
\texttt{agglutination} &
\texttt{agg}
\\
\texttt{vocalicity} &
\texttt{vcl}
\end{tabular}

For example, the query below is equivalent to~\ref{bezskr}. above:

\begin{itemize}
\item \texttt{[nmb=sg \& cas="nom|acc" \& gnd="m[123]"]}
\end{itemize}

In the graphical and text versions of Poliqarp, it is possible to define
so-called aliases,\label{alias} i.e., abbreviations for alternative values
of a given attribute, which may themselves be used as if they were possible
values of attributes.  The current version of the National Corpus of Polish has four
such aliases already pre-defined:

\begin{tabular}{llll}
\textbf{alias} &
\textbf{definition}
\\
\texttt{masc} &
\texttt{m1}  \texttt{m2}  \texttt{m3}
\\
\texttt{noun} &
\texttt{subst}  \texttt{depr}    \texttt{ger}  \texttt{xxs}  \texttt{ppron12}  \texttt{ppron3}
\\
\texttt{pron} &
\texttt{ppron12}  \texttt{ppron3}  \texttt{siebie}
\\
\texttt{verb} &
\texttt{fin}  \texttt{praet}    \texttt{aglt}
  \texttt{bedzie}  \texttt{inf}  \texttt{imps}  \texttt{impt}
  \texttt{pact}  \texttt{ppas}  \texttt{pcon}
  \texttt{pant}  \texttt{ger}  \texttt{winien}
\end{tabular}

With the definitions of the aliases \texttt{noun} and \texttt{masc} given
above, the following two queries are equivalent:

\begin{itemize}
\item \texttt{[pos=noun \& gender=masc]}
\item \texttt{[pos="subst|depr|ger|xxs|ppron12|ppron3" \& gender="m1|m2|m3"]}
\end{itemize}

The values of grammatical classes and categories may be specified jointly,
with the use of the \texttt{tag} attribute.  For example, the following
query may be used to find singular nominative neuter nouns:

\begin{itemize}
\item \texttt{[tag=subst:sg:nom:n]}
\end{itemize}

The values of the \texttt{tag} attribute have the form
\texttt{kl:kat$_1$:kat$_2$:...:kat$_n$}, where \texttt{kl} is the name of a
grammatical class, while each of \texttt{kat$_i$} is the value of a
grammatical category appropriate for that class, in the order specified in
the table in \ref{tabzewsz}.

Just as in case of other attributes, also the specification of the value of
\texttt{tag} may contain regular expressions, e.g.:

\begin{itemize}
\item \texttt{[tag=".*:sg:(nom|acc):m[123].*"]}
\item \texttt{[tag="sg:(nom|acc):m[123]"/x]}
\end{itemize}

Specification of grammatical classes and grammatical categories may contain
variables (having the form \texttt{\$$n$}, where $n$ is a single digit), whose
values will be set only during execution of the query. For example, the
following query for an adjective and a following noun agreeing in case:

\begin{itemize}
\item
\texttt{[case=nom \& pos=adj] [case=nom \& pos=subst] | [case=gen \& pos=adj] [case=gen \& pos=subst] |} \\
\texttt{[case=dat \& pos=adj] [case=dat \& pos=subst] | [case=acc \& pos=adj] [case=acc \& pos=subst] |} \\
\texttt{[case=inst \& pos=adj] [case=inst \& pos=subst] | [case=loc \& pos=adj] [case=loc \& pos=subst] |} \\
\texttt{[case=voc \& pos=adj] [case=voc \& pos=subst]}
\end{itemize}

can be simplified to:

\begin{itemize}
\item \texttt{[case=\$1 \& pos=adj] [case=\$1 \& pos=subst]}
\end{itemize}

\subsection{Ambiguities}
\label{sec:wieloznacznoci}

One of the features that distinguish the National Corpus of Polish and Poliqarp from
other corpora and search tools is the representation and processing of
ambiguities.  There are cases where it is impossible to tell which of a
number of interpretations is the right one, as in~\ref{pij1}. below.

\begin{enumerate}
\item \label{pij1}
\begin{tabular}{lll}
Pamiętam&ją&pijaną. \\
remember.\textsc{1st}&her.\textsc{acc}&drunk.\textsc{acc/ins}
\end{tabular}

`I remember her drunk.'
\item \label{pij2}
\begin{itemize}
\item

\begin{tabular}{lll}
Pamiętam&go&pijanego. \\
remember.\textsc{1st}&him.\textsc{acc}&drunk.\textsc{acc}
\end{tabular}

`I remember him drunk.'
\item

\begin{tabular}{lll}
Pamiętam&go&pijanym. \\
remember.\textsc{1st}&him.\textsc{acc}&drunk.\textsc{ins}
\end{tabular}

\end{itemize}
\end{enumerate}

Since it is impossible to resolve the grammatical case of \emph{pijaną}
in~\ref{pij1}., both interpretations, accusative and instrumental, should be
marked in the corpus as correct in this context.

However, given that after disambiguation a single segment may contain more
than one interpretation, the question arises whether such ambiguous
segments, e.g., \emph{pijaną} in~\ref{pij1}., should be included in the
result of a query which matches only some of these interpretations, e.g., in
the result of the query \texttt{[case=acc]}.  On the one hand, the segment
\emph{pijaną} should be included in the result of \texttt{[case=acc]}, as
accusative is one of the correct interpretations of this segment in this
context, but on the other hand, this segment should \emph{not} be included,
as it is not absolutely certain that this is an accusative form.

Instead of choosing between these interpretations of a query like
\texttt{[case=acc]}, Poliqarp allows the user to pose both kinds of queries.
When a single equality sign is used, as in \texttt{[case=acc]}, all segments
whose at least one interpretation matches the given condition will be
returned, so both \emph{pijaną} and \emph{ją} in~\ref{pij1}. will be
included in the result of this query.  On the other hand, when two equality
signs are used, as in \texttt{[case==acc]}, only those segments will be
returned whose all interpretations satisfy the condition expressed with
\texttt{==}, i.e., in~\ref{pij1}., only the form \emph{ją} will match the
query.

With this distinction in hand, it is possible to search for forms which,
e.g., may in a given context be interpreted as either accusative or
genitive, so -- given a properly tagged corpus -- the following query should
give non-empty results.

\begin{itemize}
\item \texttt{[case=acc \& case=gen]}
\end{itemize}

Conversely, the query below matches those segments whose all interpretations
in the given context are at the same time accusative and genitive, so it
will necessarily produce empty results.

\begin{itemize}
\item \texttt{[case==acc \& case==gen]}
\end{itemize}

The queries above pertain to interpretations which are the result of
morphosyntactic disambiguation. The National Coropus of Polish contains also all other
interpretations assigned to a given segment by the morphological analyser.
In some situations it is useful to have access to such interpretations
rejected by the disambiguator, e.g., for the task of finding all syncretic
forms of a certain kind in the corpus, or when investigating disambiguation
errors.  For example, in order to find all syncretic accusative/genitive
forms in the corpus, regardless of their interpretation in contexts in which
they occur, the following query may be posed:

\begin{itemize}
\item \texttt{[case\textasciitilde{}acc \& case\textasciitilde{}gen]}
\end{itemize}

The final equality operator available in Poliqarp queries is
\texttt{\textasciitilde\textasciitilde}.  The following query may be used for
finding those forms which are unambiguously accusative, again,
regardless of the context in which they occur.

\begin{itemize}
\item \texttt{[case\textasciitilde\textasciitilde{}acc]}
\end{itemize}

The table below summarises the four equality operators put at the user's
disposal in Poliqarp.

\begin{tabular}{lll}
& \textbf{in the results of}  & \textbf{in the results of} \\
& \textbf{morphological analysis }  & \textbf{disambiguation} \\
\textbf{at least one interpretation}  & \texttt{\textasciitilde{}} & \texttt{=} \\
\textbf{each interpretation}  & \texttt{\textasciitilde\textasciitilde{}}  & \texttt{==}
\end{tabular}

It should be clear that the following implications hold:

\begin{itemize}

\item \texttt{[attribute\textasciitilde\textasciitilde{}value]}
$\rightarrow$ \texttt{[attribute==value]}

i.e., each match of the query
\texttt{[attribute\textasciitilde\textasciitilde{}value]} also matches the
query \texttt{[attribute==value]}

\item \texttt{[attribute==value]}
  $\rightarrow$ \texttt{[attribute=value]}

i.e., each match of the query \texttt{[attribute==value]} also matches the
query \texttt{[attribute=value]}

\item \texttt{[attribute=value]} $\rightarrow$
\texttt{[attribute\textasciitilde{}value]}

i.e., each match of the query \texttt{[attribute=value]} also matches the
query \texttt{[attribute\textasciitilde{}value]}

\end{itemize}

\subsection{Constraining matches to sentences or paragraphs}
\label{sec:szukanie-w-obrbie}

Texts contained in the National Corpus of Polish are divided into sentences and
paragraphs. This information may be taken into account in queries, in order
to constrain a query to a sentence or a paragraph, as in the query below,
which may be used to find the form \emph{się} separated from a form of the
verb \textsc{bać} by any positive number of (non-\emph{się}) segments, but
within a sentence.

\begin{itemize}
\item \texttt{[base=bać][orth!=się]+[orth=się] within s}
\end{itemize}

Similarly, the qualifier `\texttt{within p}' constrains the scope of a
query to a paragraph.

\subsection{Constraining matches with metadata}
\label{sec:metadane}

Each text in the National Corus of Polish comes with a set of data about
that text, such as its title and author, publisher, date of
publication, etc.  Some of such metadata are accessible through
Poliqarp and may be used to constrain the scope of a query, e.g., to
texts by a given author or published between certain dates.

The following meta-attributes are available in the \textbf{3rd demo of the
National Corpus of Polish}:

\begin{itemize}

  \item \texttt{author}: the author(s) of the text;

  \item \texttt{title}: the title;

  \item \texttt{source}: the source (e.g. title of the journal for news
  articles);

  \item \texttt{isbn}: the ISBN number;

  \item \texttt{issn}: the ISSN number;

  \item \texttt{type}: type of text (e.g. literary genre), possible values are:

  \begin{itemize}

    \item \texttt{lit}, i.e., fiction (subtypes:
    \texttt{lit\_proza}, i.e., prose, \texttt{lit\_poezja}, i.e., poetry,
    \texttt{lit\_dramat}, i.e., drama),

    \item \texttt{fakt}, i.e., non-fiction literature,

    \item \texttt{publ}, i.e., journalism,

    \item \texttt{nd}, i.e., academic writing and textbooks,

    \item \texttt{inf-por}, i.e., instructive writing and guidebooks,

    \item \texttt{nklas}, i.e., unclassified non-fiction books,

    \item \texttt{inne\_pisane}, i.e. miscellaneous written texts (subtypes:
    \texttt{urzed}, i.e., legal and offical texts,
    \texttt{persw}, i.e., advertisements, announcements, political marketing texts,
    \texttt{instr}, i.e., user manuals,
    \texttt{listy}, i.e., letters),

    \item \texttt{internet}, i.e. texts acquired from the Internet (subtypes:
        \texttt{net\_interakt}, i.e. originating from forums, chat rooms, instant messaging, mailing lists,
        \texttt{net\_nieinterakt}, i.e. originating form static WWW pages),

    \item \texttt{konwers}, i.e., conversational texts,

    \item \texttt{media}, i.e., spoken texts from the media,

    \item \texttt{qmow}, i.e., quasi-spoken texts;

  \end{itemize}

  \item \texttt{channel}: publication channel, possible values are:

  \begin{itemize}

    \item \texttt{prasa}, i.e., press (subchannels:
    \texttt{prasa\_dziennik}, i.e., daily,
    \texttt{prasa\_tygodnik}, i.e., weekly,
    \texttt{prasa\_miesiecznik}, i.e., monthly,
    \texttt{prasa\_inne}, i.e., other),

    \item \texttt{ksiazka}, i.e., a book,

    \item \texttt{internet},

    \item \texttt{mowiony}, i.e., spoken,

    \item \texttt{ulotka}, i.e., leaflets, announcements, ads,

    \item \texttt{rkps}, i.e., manuscripts;

  \end{itemize}

  \item \texttt{publisher};

  \item \texttt{publication\_place};

  \item \texttt{published}: publication date;

  \item \texttt{first\_published}: publication date of the first
  edition of the text;

  \item \texttt{created}: creation date;

  \item \texttt{acquired}: acquisition date;

  \item \texttt{recorded}: recording date.

\end{itemize}

Usually only some of these attributes will have a value defined, e.g., when
only the date of the publication is known, not the date of the first
publication or the date of origin, or in case of short newspaper notes,
which might lack information about the author or even the title.

In order to constrain the scope of a query with metadata, the keyword
\texttt{meta} should be placed at the end of the query and it should be
followed by specifications of values of meta-attributes.  In case the scope
of the query is also constrained to a sentence or to a paragraph, the
specification of metadata should follow the structural constraint, e.g.:

\begin{itemize}
\item \texttt{[pos=subst]\lbr{}6,\rbr{} within s meta author=Kowalski}
\end{itemize}

Just as in case of ordinary attributes such as \texttt{orth} or
\texttt{pos}, also the specifications of values of the meta-attributes
\texttt{author} and \texttt{title} may contain regular expressions.  For
example, the query below may be used to find forms of the lexeme
\textsc{wirus} in those texts whose title contains one of the sequences:
\emph{windows} or \emph{microsoft}.

\begin{itemize}
\item \texttt{[base=wirus] meta title="windows|microsoft"}
\end{itemize}

By default, the specifications of values of \texttt{author} and
\texttt{title} are taken to be case-insensitive and they are interpreted as
matching (at least) parts of values of appropriate meta-attributes, so the
following query will find sequences of nominal forms in works by,
\emph{inter alia}, Pol, Polkowski and Rampolski:

\begin{itemize}
\item \texttt{[pos=subst]\lbr{}5\rbr{} meta author=Pol}
\end{itemize}

To change that default behaviour, the flags \texttt{/X} and \texttt{/I} may
be used.  The effect of these flags is dual to the effect of the flags
\texttt{/x} and \texttt{/i} described above: the effect of \texttt{/X} is
that a given specification of the value of an attribute is understood as
matching the complete value of that attribute, while the flag \texttt{/I}
enforces the case-sensitive interpretation, as in examples below:

\begin{itemize}
\item \texttt{[pos=subst]\lbr{}5\rbr{} meta author=Pol/I}

the scope of the query is limited to texts by Pol, Polkowski, etc., but not
by Rampolski,

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta author="Marek Pol"/X}

query limited to texts by Marek Pol,

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta author=Pol/X}

query limited to texts by those authors whose complete name is either
\emph{Pol}, or \emph{POL}, or \emph{pol}, etc.

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta author=".* Pol"/I}

query limited to texts by an author whose surname is \emph{Pol}.

\end{itemize}

Regular expressions are not allowed in case of the date-valued attributes
\texttt{created}, \texttt{acquired}, \texttt{recorded},
\texttt{first\_published} and \texttt{published}.  On the
other hand, it is possible to use the lesser/greater signs \texttt{<} and
\texttt{>}, e.g.:

\begin{itemize}
\item \texttt{[pos=subst]\lbr{}5\rbr{} meta created>1950}

query limited to texts created after year 1950

\end{itemize}

Constraints on meta-attributes may be combined with the operators
\texttt{\&}, \texttt{|} and \texttt{!}, e.g:

\begin{itemize}
\item \texttt{[pos=subst]\lbr{}5\rbr{} meta created>=1951 \& created<=1960}

query limited to texts created in years 1951-1960

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta published>1900 \& author!=Sienkiewicz}

query limited to texts published after 1900, by authors other than
Sienkiewicz

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta (author=sienkiewicz \& title=potop)\\
| (author=żeromski \& title=przedwiośnie)}

query limited to \emph{Potop} by Sienkiewicz and \emph{Przedwiośnie} by
Żeromski

\item \texttt{[pos=subst]\lbr{}5\rbr{} meta styl=nd \& medium=ksiazka}

query limited to textbooks or academic writings published as a book

\end{itemize}

Former demos of the National Corpus of Polish and editions of the IPI PAN
Corpus are using different metadata schemes. Please refer to
\href{http://korpus.pl/en/cheatsheet/node3.html#SECTION00037000000000000000}{The
IPI PAN Corpus Cheatsheet} for details.

\subsection{Aligning matches}
\label{sec:wyrwyn}

In order to make the results of a query more readable, it is possible
to place within the query proper, i.e., before the qualifiers
\texttt{within} and \texttt{meta}, a special alignment marker, \texttt{\textasciicircum}, as in:

\begin{itemize}
\item \texttt{[pos=adj \& case=nom]+\textasciicircum[pos=subst \& case=nom]+}
\end{itemize}

Instead of the usual three columns containing the left context of the match,
the match itself, and the right context, the results of this query will be
split into four columns, containing, respectively, the left context, the
left match, i.e., the sequence of segments matching the part of the query
before the alignment marker \texttt{\textasciicircum} (here, a non-empty
sequence of nominative adjectives), the right match (here, a non-empty
sequence of nominative nouns), and the right match.
