Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The syntax of Squirro Script strongly resembled Python, but the complexity and functionality has been reduced. On the other hand Squirro Script is optimised for dealing with Squirro items, especially around how functions are used on keywords and the functions that are provided. For cases where Squirro Script is not powerful enough, pipelets can be used to use the full power of Python instead.


Table of Contents

Table of Contents
outlinetrue
excludeTable of Contents

Language Primer

Squirro Script is modelled very closely after Python with only small syntax additions. Note however, that compared to Python only very limited functionality is available.

Example

An example script could look like this:

...

This simple script calculates the the hours keyword  keyword from the the minutes keyword keyword. It then shortens the title to a most 40 characters with an ellipsis.

Variables

There are three types of variables available. The Item Format documentation contains full details on the possible fields and keywords available.

Variable

Description

Examples

Keyword

This are facet values stored in

the 

the keywords

 field

 field of Squirro items. Each value is a list of items, but Squirro Script simplifies the handling of that by working on each list item individually.

  • minute

  • hour = minute / 60

Item field

Variables prefixed with

a 

$

 reference

 reference a core item field of Squirro items. Title, body, creation date, etc. are stored at this level.

  • $title,

  • $body = $title + "<br>" + $body

Temporary

When prefixed with

a 

@

 a

 a variable is temporary. Sometimes you need to store the result of a calculation somewhere. It is recommended to use temporary variables for this.

  • @tlen = len($title)

Variable can be assigned with the the = operator operator

Data types

There are four types of variables:

  • string

  • int

  • float

  • datetime

The types correspond to the type of Squirro facets The type of a variable is implicitly defined by the assigned value, or explicitly by using one of the to_* functions.

Literals (values)

Squirro Script supports strings and numbers. Strings are always assumed to be Unicode and are assumed to be UTF-8 encoded. Numbers can be full numbers (int) or contain a decimal separator (float). Examples: 42 or  or 3.1415926).

Conditions

Conditional logic can be implemented with the the if block block. Example:

Code Block
languagepy
if @tlen > 40:
    $title = $title + '…'
    short_title = 'false'
elif @tlen == 0:
    $title = "No title"
    short_title = 'true'
else:
    short_title = 'true'

Each Each if block  block has an indented block of code to be executed when the comparison returns true. Any number of optional elif blocks can be added which are executed if the respective condition matches. Optionally an an else block  block can be used at the end for when none of the comparisons matches.

For comparison the operators == (equal), != (not equal), '>' (greater than), '>=' (greater or equal), '<' (less than) and '<=' (less or equal) are available. If no comparison operator is used (such as as if $title:), then the variable is treated as true if it is non-empty and not zero.

...

The following operators are provided:

  • +: for addition or concatenation.

  • -: for subtraction of numeric values.

  • *: for multiplication of two numeric values.

  • /: for division of numeric values.

Lookup Tables

Lookup tables are used to link data in processed Squirro items to some other database. For this purpose lookup tables are provided in Squirro Script. When you need more matching capabilities, Known Entity Extraction is another way to go.

...

Take the following input file (the lookup table) and Squirro item:

Lookup Table

Squirro Item


products.json
Code Block
languagejs
titleproducts.json
{
    "iphone": {
        "category": "phone",
        "vendor": "Apple Inc."
    },
    "firefox": {
        "category": ["browser", "software"],
        "vendor": "Mozilla"
    }
}



Code Block
languagejs
{
    "title": "Example",
    "keywords": {
        "products": ["firefox"]
    }
}


The following Squirro script will take the products keyword and look it up in the lookup table:

lookup.sqs
Code Block
languagepytitlelookup.sqs
# Reads information from a product database (stored in JSON).
@product_table = load_table_from_json('products.json')
@products = lookup(@product_table, products)
extend_keywords(@products)

...

Code Block
languagejs
{
    "title": "Example",
    "keywords": {
        "products": ["firefox"],
        "category": ["browser", "software"],
        "vendor": ["Mozilla"]
    }
}

Functions

Functions can be called anywhere a variable can be referenced. For example:

...

The following example shows the difference between a normal function (len) and an aggregation function (count).

Script

Squirro Item

Result


Code Block
attendee_count = count(attendees)
attendee_len = len(attendees)



Code Block
languagejs
{
  "title": "Example",
  "keywords": {
    "attendees": ["Jon", "Abigail", "Mark", "Rose ", "Isabella"]
  }
}



Code Block
languagejs
{
  "title": "Example",
  "keywords": {
    "attendees": ["Jon", "Abigail", "Mark", "Rose ", "Isabella"],
    "attendees_count": [5],
    "attendees_len": [3, 7, 4, 4, 8]
  }
}


String Functions

contains(str, substring[, substrings…])

...

Some date functions take format strings. The separate page Format Strings documents this Python datetime format string.

Aggregation Functions

Aggregation functions are used on keywords and work on the entire list, instead of individual values. Most of the functions assume numeric values in the list.

...

Code Block
languagepy
minutes_len = len(minutes)
minutes_count = count(minutes)
minutes_max = max(minutes)
minutes_min = min(minutes)
minutes_sum = sum(minutes)
minutes_avg = avg(minutes)

Lookup Table Functions

extend_keywords(key_value_dict)

Applies the return value from the lookup table to the item's keywords. The key_value_dict is a dictionary of key/value pairs, which is transformed into keywords on the items. The value can also be a list of such dictionaries, in which case each one is processed in turn.

load_table_from_json(filename)

Instantiates a lookup table by loading the given filename in JSON format. The format of the file is expected to be the following:

Code Block
{
    "key": [
        {
            "output_key": "value",
            …
        },
        …
    ],
    …
}

...

Looks up the given key or keys in the lookup table. Returns a list of all the matching entries.

Other Functions

clear(variable)

Clears the given variable. This is usually used on facets or item fields to clear them.

...