Skip to content

OPTIONAL

Overview

The OPTIONAL operator is used to write sub-conditions that can trigger or not in a rule. This allows you to write a minor number of rules, each one triggering with more sentences that would normally require a higher number of rules.

For example, these rules will trigger:

SCOPE SENTENCE
{
    DOMAIN(dom1)
    {
        LEMMA("dog")
        <1:5>
        !LEMMA("good")
        <1:2>
        !LEMMA("boy")
    }

    DOMAIN(dom1)
    {
        LEMMA("dog")
        <1:5>
        LEMMA("good")
        <1:2>
        LEMMA("boy")
    }
}

if they are applied to the following sentences:

I have a beautiful dog. 
My dog is really a good boy.

Because of the second rule, you need to put negations (!) at the first rule.

With the OPTIONAL operator, you need a single rule:

SCOPE SENTENCE
{
    DOMAIN(dom1)
    {
        LEMMA("dog")
        OPTIONAL 
        {
            <1:5>
            LEMMA("good")
            <1:2>
            LEMMA("boy")                
        }
    }   
}

Note

If the OPTIONAL operator is introduced by sequences or logical operators, they will have to be inserted inside parenthesis. If such operator is succeded by sequences or logical operators, they will have to be inserted outside parenthesis.

Warning

It is not possible to use boolean operators inside an OPTIONAL statement when writing a sub-condition.

The syntax of the OPTIONAL operator is the following:

operand
OPTIONAL
{
    sub-condition
}
...

where OPTIONAL is in uppercase and can be placed wherever you need it in the rule.

One more complex example. This rule will trigger:

SCOPE SENTENCE
{
    DOMAIN(dom1)
    {
        LEMMA("woman")
        OPTIONAL 
        {
            <1:3>
            TYPE(PRE)
            <1:3>
            LEMMA("umbrella")
        }
        <1:3>
        LEMMA("go")
        <1:3>
        LEMMA("cinema")
        OPTIONAL 
        {
            <1:2>
            TYPE(PRE)
            <1:3>
            LEMMA("partner")
        }       
    }   
}

if run against the following texts:

The woman went to the cinema.
The woman with the umbrella went to the cinema.
The woman went to the cinema with her partner.
The woman with the umbrella went to the cinema with her partner.

because the optional sub-conditions allow the rule to trigger with or without them.

Normally, more specific rules with negations would be required—like the example above—to trigger in case of these sentences, because of all the various elements in each sentence, all of them enclosed in two OPTIONAL of a single rule.

Tip

In order to alternatively combine the NEXT operator functionality with the OPTIONAL operator functionality, consider this workaround.

For example, suppose that you want to extract people's names and—eventually—their addresses, if they occur in the text.

Consider this template:

TEMPLATE(PERSONAL_DATA)
{
    @Name,
    @Address
}

The following rule will generate a syntax error:

IDENTIFY(PERSONAL_DATA)
{
    @Name[TYPE(NPH)]
    NEXT
    OPTIONAL
    {
        @Address[TYPE(ADR)]
    }
}

if applied to this input text:

John is 20. He lives in Naples.

but if you change the rule like this:

IDENTIFY(PERSONAL_DATA)
{
    @Name[TYPE(NPH)]
    NEXT
    (
        @Address[TYPE(ADR)]
        OR
        POSITION(BEGIN SENTENCE)
    )
}

you will get this record:

Template:PERSONAL_DATA

Field Value
@Name John

If, on the other hand, the changed rule is applied to this input text:

John is 20. He lives in Baltimora Street.

you will get this record:

Template: PERSONAL_DATA

Field Value
@Name John
@Address Baltimora Street

The rule above allows you extract the address if it occurs in a text or it will extract nothing more than the Name field, thanks to the POSITION attribute workaround.

OPTIONAL attributes in OR

It is possible to define an OR relationship between the attributes in the OPTIONAL operator.

This rule:

SCOPE SENTENCE 
{
    DOMAIN(dom1)
    {
        ROLE(SUBJECT)
        &SV
        TYPE(VER)
        OPTIONAL
        {
            <1:6>
            LEMMA("lottery"),
            <1:6>
            TYPE(MON)
        }
    }
}

avoids three different rules, like:

SCOPE SENTENCE
{
    DOMAIN(dom1)
    {
        ROLE(SUBJECT)
        &SV
        TYPE(VER)
    }

    DOMAIN(dom1)
    {
        ROLE(SUBJECT)
        &SV
        TYPE(VER)
        <1:6>
        LEMMA("lottery)
    }

    DOMAIN(dom1)
    {
        ROLE(SUBJECT)
        &SV
        TYPE(VER)
        <1:6>
        TYPE(MON)
    }
}

The comma (,) after the LEMMA attribute creates copies of the rule with the specific pieces of conditions separated by the comma.

The rule above will trigger in texts like:

My father won the lottery.
My father won 1.000.000€.
My father won the lottery, 1.000.000€ all of a sudden.