<!doctype html>
<meta charset="utf-8">
<title>
{{ form.get('engine', 'tgrep2') + ' ' + form['query'] if 'query' in form else 'Treebank Search' }}
</title>
<script src="static/main.js"></script>
<link type="text/css" rel="stylesheet" href="static/style.css">
<style>
div.barplot {
    page-break-inside:avoid;
    page-break-after:auto;
    padding: 3px;
    margin: 1px;
}
div.barplot div+span {
    font: 10px sans-serif;
    padding: 3px;
    margin: 1px;
}
div.barplot span {
    position: relative;
    z-index: 1;
    left: .5em;
}
.b0, .b1, .b2, .b3, .b4, .b5 {
    position: absolute;
    z-index: 0;
    height: 1.1em;
    left: 1em;
}
.b0 { background-color: transparent; }
.b1 { background-color: lightblue; }
.b2 { background-color: lightcoral; }
.b3 { background-color: wheat; }
.b4 { background-color: khaki; }
.b5 { background-color: lightpink; }
:root { /* include the colored bars in plots when printing (to pdf) with Chrome */
    -webkit-print-color-adjust: exact;
}
</style>

<body onLoad="triggerForm('output', '{{ output }}'); triggerForm('norm', '{{
    form.get('norm', 'sents') }}'); triggerForm('engine', '{{ form.get('engine',
    'xpath' if havexpath else 'tgrep2') }}'); " >
<div class="content">
<h3>Treebank Search</h3>

<blockquote class=intro><p>
    A web-interface to query a corpus of parse trees or tokenized sentences.
    Results can be displayed in various ways or exported to a file.</p>

    <p>
    Query languages (click to toggle help):
    {% if havetgrep %}
    <a href="javascript: toggle('tgrephelp'); ">TGrep2</a>,
    {% endif %}
    {% if havexpath %}
    <a href="javascript: toggle('xpathphelp'); ">XPath</a>,
    {% endif %}
    <a href="javascript: toggle('fraghelp'); ">fragment</a>,
    <a href="javascript: toggle('regexhelp'); ">regex</a>.
    <br>

    Available macros:
    {% if havetgrep %}
    <a href="static/tgrepmacros.txt">TGrep2</a>,
    {% endif %}
    {% if havexpath %}
    <a href="static/xpathmacros.txt">XPath</a>,
    {% endif %}
    <a href="static/regexmacros.txt">regex</a>.
    <br>
    Browse through <a href="/browse">trees</a>, <a href="/browsesents">sentences</a>
    | <a href="/style">Statistics &amp; readability measures</a>
</p></blockquote>

<form action="/" method=get name=queryform onSubmit="mergecheckboxes(); "><table>
	<tbody>
    <tr valign=top><td><label for=f1>Query:</label></td>
        <td>
            <input type=text
                name={{ 'notquery' if 'textarea' in form else 'query' }}
                id=f1 size=80 value="{{ form['query'] }}"
                {{ 'style="display: none; " disabled'|safe
                if 'textarea' in form }}>
            <textarea
                name={{ 'query' if 'textarea' in form else 'notquery' }}
                {{ '' if 'textarea' in form
                else 'style="display: none; " disabled'|safe }}
                id=f2 rows=10 cols=80>{{ form['query'] }}</textarea>
            <input type=hidden name=textarea
                value=on {{ '' if 'textarea' in form else 'disabled' }}>
        </td>
        <td><input type=submit>
			<a href="javascript: toggletextbox(); ">switch to
				<span id=textboxlink>{{ 'small' if 'textarea' in form else 'large' }}</span>
				textbox</a>
		</td>
    </tr>
    <tr><td style="vertical-align: text-top; ">
    Input:</td><td><span id=numchecked>{{ selectedtexts|length }} </span> texts selected; <a href="javascript: togglelink('selecttexts'); " id="linkselecttexts">hide selection</a>;
    Query engine:
    {% if havetgrep %}
    <label><input type=radio name=engine value=tgrep2{{
        ' checked' if form.get('engine', default) == 'tgrep2'
        }} onChange="show('treesoutput', '');">TGrep2</label>
    {% endif %}
    {% if havexpath %}
    <label><input type=radio name=engine value=xpath{{
        ' checked' if form.get('engine', default) == 'xpath'
        }} onChange="show('treesoutput', '');">XPath</label>
    {% endif %}
    {% if havefrag %}
    <label><input type=radio name=engine value=frag{{
        ' checked' if form.get('engine', default) == 'frag'
        }} onChange="show('treesoutput', '');">fragment</label>
    {% endif %}
    <label><input type=radio name=engine value=regex{{
        ' checked' if form.get('engine', default) == 'regex'
		}} onChange="hide('treesoutput', '');">regex</label>
    <div id=selecttexts style="display: {{ 'none' if form.get('query') or output == 'style' else 'block' }}; ">
    (Select <a href="javascript: checkall('textcheck', true); numchecked(); ">all</a>;
    <a href="javascript: checkall('textcheck', false); numchecked(); ">none</a>)
    {% if metadata is not none %}
    <label>Select subset from metadata:
        <select name=subset>
            <option value="">(none)</option>
            {% for cat in categoricalcolumns -%}
				{% for val in metadata[cat].unique() -%}
					<option value="{{ cat }}={{ val }}">{{ cat }}={{ val }}</option>
				{% endfor -%}
            {% endfor -%}
    </select></label>
    {% endif %}
    <br>
    <input type=hidden name=texts value="">
    {% for text in texts -%}
    <label><input type=checkbox name=textcheck value={{ loop.index0 }}{{
        ' checked' if loop.index0 in selectedtexts }} onChange="numchecked(); ">{{ text }}</label><br>
    {% endfor -%}
    </div>
    <br>
    <label>Slice:
        <input type=text name=slice value="{{ form['slice'] }}" size=10>
        (optionally, enter slice <em>n-m</em> to restrict the range of
        sentences searched of each text)
    </label><br>
    </td><td></td></tr>
    <tr>
    <td style="vertical-align:top;">Output:</td>
    <td>
    <label><input type=radio name=output value=counts{{ ' checked' if output == 'counts'
        }} onChange="show('normrow', 'norm'); hide('labelrow', 'nomorph'); " >
        Counts;</label>
    <label><input type=radio name=output value=sents{{ ' checked' if output == 'sents'
        }} onChange="hide('normrow', 'norm'); show('labelrow', 'nomorph'); ">
        Sentences;</label>
    <span id=treesoutput>
    <label><input type=radio name=output value=trees{{ ' checked' if output == 'trees'
        }} onChange="hide('normrow', 'norm'); show('labelrow', 'nomorph'); ">
        Trees;</label>
    <br>
    <label><input type=radio name=output value=brackets{{ ' checked' if output == 'brackets'
        }}  onChange="hide('normrow', 'norm'); show('labelrow', 'nomorph'); ">
        Raw trees;</label>
    <label><input type=radio name=output value=fragments{{ ' checked' if output == 'fragments'
        }}  onChange="hide('normrow', 'norm'); show('labelrow', 'nomorph'); ">
        Extract fragments from matches;</label>
    </span>
    </td><td></td></tr>
	</tbody>
    <tbody id=normrow>
    <tr>
    <td style="vertical-align:top;">Normalization:</td>
    <td><label><input type=radio name=norm value=sents{{
        ' checked' if form.get('norm', 'sents') == 'sents' }} onChange="hide('nqid', 'normquery');">
        % of Sentences;</label>
    <label><input type=radio name=norm value=consts{{
        ' checked' if form.get('norm') == 'consts' }} onChange="hide('nqid', 'normquery');">
        % of Constituents;</label>
    <label><input type=radio name=norm value=words{{
        ' checked' if form.get('norm') == 'words' }} onChange="hide('nqid', 'normquery');">
        % of Words;</label>
    <br>
    <label><input type=radio name=norm value=query{{
        ' checked' if form.get('norm') == 'query' }} onChange="show('nqid', 'normquery');">
        custom query:
            <span id=nqid> <input type=text name=normquery id=f3 size=60 value="{{
                form['normquery'] }}"></span></label>
	</td><td></td></tr>
    {% if metadata is not none %}
	<tr id=variables>
    <td style="vertical-align:top;">Variables:</td>
	<td><label>Correlate with (continuous) or break down by (categorical): <select name=target>
            <option value="">(none)</option>
            {% for cat in metadata.columns -%}
                <option value="{{ cat }}"{{ ' selected' if form.get('target') == cat }}>{{ cat }}</option>
            {% endfor -%}
    </select></label><br>
    <label>Distinguish subsets by color (categorical): <select name=target2>
            <option value="">(none)</option>
            {% for cat in categoricalcolumns -%}
                <option value="{{ cat }}"{{ ' selected' if form.get('target2') == cat }}>{{ cat }}</option>
            {% endfor -%}
    </select></label>
    <br><a href="metadata">view metadata</a>
    </td><td></td></tr>
    {% endif %}
	</tbody>
	<tbody>
    <tr id=labelrow>
    <td>Options</td><td>
    <label><input type=checkbox name=nomorph{{ ' checked' if 'nomorph' in form }}>
        Hide morphology tags</label>
    <label><input type=checkbox name=nofunc{{ ' checked' if 'nofunc' in form }}>
        Hide function tags</label>
    <label><input type=checkbox name=breakdown{{ ' checked' if 'breakdown' in form }}>
        Breakdown</label>
    <br>
    </td><td></td></tr>
	</tbody>
</table></form>
<div id=tgrephelp style="display: none; "><pre>{{ """TGrep2 operators:
  A < B       A is the parent of (immediately dominates) B.
  A > B       A is the child of B.
  A <N B      B is the Nth child of A (the first child is <1).
  A >N B      A is the Nth child of B (the first child is >1).
  A <, B      Synonymous with A <1 B.
  A >, B      Synonymous with A >1 B.
  A <-N B     B is the Nth-to-last child of A (the last child is <-1).
  A >-N B     A is the Nth-to-last child of B (the last child is >-1).
  A <- B      B is the last child of A (synonymous with A <-1 B).
  A >- B      A is the last child of B (synonymous with A >-1 B).
  A <` B      B is the last child of A (also synonymous with A <-1 B).
  A >` B      A is the last child of B (also synonymous with A >-1 B).
  A <: B      B is the only child of A.
  A >: B      A is the only child of B.
  A << B      A dominates B (A is an ancestor of B).
  A >> B      A is dominated by B (A is a descendant of B).
  A <<, B     B is a left-most descendant of A.
  A >>, B     A is a left-most descendant of B.
  A <<` B     B is a right-most descendant of A.
  A >>` B     A is a right-most descendant of B.
  A <<: B     There is a single path of descent from A and B is on it.
  A >>: B     There is a single path of descent from B and A is on it.
  A . B       A immediately precedes B.
  A , B       A immediately follows B.
  A .. B      A precedes B.
  A ,, B      A follows B.
  A $ B       A is a sister of B (and A != B).
  A $. B      A is a sister of and immediately precedes B.
  A $, B      A is a sister of and immediately follows B.
  A $.. B     A is a sister of and precedes B.
  A $,, B     A is a sister of and follows B.
  A = B       A is also matched by B.
"""|e }}</pre>
<a href="http://tedlab.mit.edu/~dr/Tgrep2/">More information</a>
</div>
<div id=xpathhelp style="display: none; "><pre>Find a particular word:

//node[@word='loopt']

This is case-sensitive.
If you want to find all inflectional variants of the verb 'lopen', do:

//node[@lemma='lopen']

To find main clauses:

//node[@cat="smain"]

Finite subordinate clauses:

//node[@cat="cp" and node[@rel="body" and @cat="ssub"]]

This locates "cp" nodes with an "ssub" child that has "body" as function tag (relation).
</pre>
More information:<br>
<a href="https://en.wikipedia.org/wiki/XPath">General XPath overview</a><br>
<a href="http://rug-compling.github.io/dact/cookbook/">Using XPath on Alpino treebanks</a>.
</div>
<div id=fraghelp style="display: none; "><pre>{{ """Tree fragments.

regular bracket trees:
(S (NP Mary) (VP (VB is) (JJ rich)) (. .))
(S (NP ) (VP (VB is) (JJ )) (. .))

discontinuous trees:
(S (VP (VB 0) (JJ 2)) (NP 1) (? 3)) is John rich ?
(S (VP (VB 0) (JJ 2)) (NP 1) (? 3)) is   ?
"""|e }}</pre>
See <a href="http://andreasvc.github.io/discodop/fileformats.html#bracket">
http://andreasvc.github.io/discodop/fileformats.html#bracket</a>
</div>
<div id=regexhelp style="display: none; "><pre>Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves.

The special characters are:
    "."      Matches any character except a newline.
    "^"      Matches the start of the string.
    "$"      Matches the end of the string or just before the newline at
             the end of the string.
    "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
             Greedy means that it will match as many repetitions as possible.
    "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
    "?"      Matches 0 or 1 (greedy) of the preceding RE.
    *?,+?,?? Non-greedy versions of the previous three special characters.
    {m,n}    Matches from m to n repetitions of the preceding RE.
    {m,n}?   Non-greedy version of the above.
    "\\"     Either escapes special characters or signals a special sequence.
    []       Indicates a set of characters.
             A "^" as the first character indicates a complementing set.
    "|"      A|B, creates an RE that will match either A or B.
    (...)    Matches the RE inside the parentheses.
             The contents can be retrieved or matched later in the string.
    (?:...)  Non-grouping version of regular parentheses.
    (?i)     Perform case-insensitive matching.

The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.
    \A       Matches only at the start of the string.
    \Z       Matches only at the end of the string.
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       Matches any decimal digit.
    \D       Matches any non-digit character.
    \s       Matches any whitespace character.
    \S       Matches any non-whitespace character.
    \w       Matches any alphanumeric character.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.
</pre>
<a href="http://docs.python.org/3/library/re.html#regular-expression-syntax">More information</a>.
</div>

<div id=result>
{%- block result %}{% endblock -%}
</div></div></body>
