HomeProductUse casesPricingBlogContactSign in Book a demo

Blog

Lua, the scripting language in smartextract

Lua is a small, simple scripting language. Many applications use it as an extension language, from World of Warcraft to Adobe Photoshop, and smartextract does too. Lua has a lot in common with Python, but differs in a few points worth knowing.

Last updated:

This article sums up what you need to know. If you know Python, you can be writing Lua within a few minutes.

Basic syntax

Strings, numbers and function calls are written in Lua almost as in Python. A few details differ:

Boolean values are written true and false.

What Python calls None is called nil in Lua.

Everything after -- is a comment.

Operators

Boolean and numeric operators are mostly the same as in Python, with two exceptions:

The "not equal" operator is written ~= rather than !=.

The string concatenation operator is .. (two dots). Unlike in Python, trying to add strings with the + operator raises an error.

Code blocks and control flow

Whitespace has no meaning in Lua, so indentation is optional. In Python, by contrast, indentation is part of the syntax. In Lua, every code block is closed with the end keyword.

The numeric for loop looks like this:

for i = 1, 5 do
  print(i)
end

Data structures in Lua

'Dictionaries' What Python calls a dictionary is called a table in Lua. The syntax is slightly different from Python's:

local doc = {type = "invoice", pages = 3}

To access elements, use square brackets with the key name. If a key does not exist, Lua simply returns nil instead of raising an error:

print(doc["type"])    -- invoice
print(doc["sender"])  -- nil

To iterate over the entries in a table, use the pairs function:

for key, value in pairs(doc) do
  print(key, value)
end

Lists

Lua has no separate list type. Instead, it uses tables with numeric indices to build lists. Here is a simple example:

local formats = {"pdf", "png", "tiff"}

One important difference from Python is that indexing starts at 1. You can get the length of a list with the # operator. This is how it works:

print(formats[1])  -- pdf
print(#formats)    -- 3

To iterate over a list, you can use a numeric for loop or the ipairs function, which is similar to Python's enumerate:

for i = 1, #formats do
  print(formats[i])
end

for i, format in ipairs(formats) do
  print(i, format)
end

Objects and modules

Lua has no built-in object system, but it offers syntax that gives tables object-like behaviour. If a table key is a valid identifier, you can access it with dot notation:

print(doc.type)  -- invoice

Modules in Lua, such as the built-in string, table and math modules, are tables of functions. Lua also supports method calls with colon syntax, which read more easily:

local s = "smartextract"
print(string.upper(s))  -- SMARTEXTRACT
print(s:upper())        -- SMARTEXTRACT

The last expression above relies on the metatable mechanism, which we will not go into here.

Standard libraries in Lua

Compared with Python, Lua comes with few built-in functions. These are the ones you will need most often:

- require loads modules and usually returns a function or a table of functions. In smartextract, require gives you access to the machine learning models and the document processing libraries.

- The string table contains the string manipulation functions, for example for pattern matching. You will need it for almost any text processing task.

- The table table provides table manipulation functions, including ones for sorting and managing tables.

- The math table contains the mathematical functions, such as math.min, math.max and math.exp.

- pairs and ipairs, which appeared above, iterate over tables: pairs over all entries, ipairs over the numeric entries only.

- error and assert handle errors: error(message) raises an error with the given message, and assert(v, message) raises one if v is nil or false.

Note that some standard libraries available in regular Lua, such as os, io and debug, are not included in smartextract, presumably because of their specialised use cases or performance considerations.

A few differences from Python that are easy to trip over:

Indexing in Lua starts at 1, not at 0 as in Python. The end index in string and table operations is also inclusive:

print(string.sub("smartextract", 1, 5))  -- smart

In conditions, only nil and false count as false. All other values, including zero and empty collections, count as true:

if 0 then print("0 is true") end
if {} then print("an empty table is true") end

When the argument of a function call is a literal string or a table, you can leave out the parentheses:

print "no parentheses needed"
print(type {})  -- table

Variables in Lua are global by default. To create a local variable, use the keyword ‘local’.

For more on individual functions, see the Lua reference manual. If you are new to the language, we also recommend "Programming in Lua" by Roberto Ierusalimschy, a readable introduction.

Send us a document and watch the extraction.

30 minutes, your own documents, no preparation needed.