Skip to content
marekkubis edited this page Sep 7, 2014 · 19 revisions

WQuery embeds WPath expressions in imperative constructs such as loops, conditional statements and sequential execution blocks. Below we describe briefly its syntax.

Emissions

Syntax
emit expr

An emission simply returns the result of evaluating the WPath expression passed as its argument.

Example
emit {car}
Result
{ car:1:n auto:1:n automobile:1:n machine:6:n motorcar:1:n }
{ car:2:n railcar:1:n railway car:1:n railroad car:1:n }
{ car:3:n gondola:3:n }
{ car:4:n elevator car:1:n }
{ cable car:1:n car:5:n }

Blocks

Syntax
do
    expr_1
    expr_2
    ...
    expr_n
end

A block of code executes sequentially the enclosed WQuery expressions. The result of evaluating a block is a concatenation of the results of the expressions enclosed by do and end.

Example
do 
    emit {car:3:n} 
    emit {car:1:n} 
    emit {person:1:n} 
end
Result
{ car:3:n gondola:3:n }
{ car:1:n auto:1:n automobile:1:n machine:6:n motorcar:1:n }
{ person:1:n individual:1:n someone:1:n somebody:1:n mortal:1:n soul:2:n }

Assignments

Syntax
%var := expr

An assignment binds the result of evaluating a WPath expression to a variable. WQuery variables start with % sign. As in the case of WPath variables, the %-prefixed variables may be referenced in WPath expressions.

Example
do
    %a := {car:1:n}
    emit %a.hypernym
end
Result
{ car:1:n auto:1:n automobile:1:n machine:6:n motorcar:1:n } hypernym { motor vehicle:1:n automotive vehicle:1:n }

Conditionals

Syntax
if [condition]
    i_expr
else
    e_expr

The result of a conditional expression consists of the result of i_expr if the condition enclosed in [ and ] holds. Otherwise, the result of e_expr is returned. The else block is optional. If there is no else block and the condition does not hold the result is empty. The syntax of the condition to be evaluated is the same as in the case of WPath filters. All the WPath conditional operators can be used.

Example
if [count({}) < 10000] 
    emit `This is a small wordnet.`
else 
    emit `This is a big one.`
Result
'This is a small wordnet.'

From Loop

Syntax
from wpath_expr
    wquery_expr

A from loop executes the WQuery expression defined in its body for every set of bindings associated with the dataset returned by the WPath expression specified in its head. The variables bound by the WPath expression in the head can be referenced in the body. The result of a from loop execution is a concatenation of the results obtained by executing its body for every set of bindings returned by its head.

Example
from {}$a.hypernym$b do
    if [$a.pos != $b.pos] 
        emit $a,$b
end
Result
(no result)

While Loop

Syntax
while [condition]
    wquery_expr

A while loop executes the WQuery expression in its body as long as the condition enclosed in [ and ] holds true. The result of a while loop execution is a concatenation of the results obtained by subsequent executions of its body.

Example
do
    %i := 0
    %f := 1
    
    while [%i < 10] do
        emit %i,%f
        %i := %i + 1
        %f := %f * %i
    end
end
Result
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880