Scripting

Chinese Text Analyser comes with built-in support for the Lua scripting language.

You can gain access to many of the core features of Chinese Text Analyser by writing scripts in Lua, importing the ‘cta’ module and then running those scripts from within the program.

For a full list of features and functions available to Lua scripts please see the Lua API appendix.

The examples section also contains a detailed example that starts with a simple script and works up to a more complicated one.

Running Scripts

A Lua script is simply a text file that ends in the .lua extension, and that conforms to the syntax of the Lua programming language (see here for a brief overview). You can use any text editor to create your Lua scripts, and then run these scripts from within Chinese Text Analyser.

You can bring up the ‘Run Script’ dialog by selecting Tools -> Run Script... from the main menu, or by using the keyboard shortcut Ctrl+R.

_images/run-script-dialog.png

Enter the name of the script you wish to run in the ‘Script’ field or click ‘Browse’ to locate a script on your hard drive.

Chinese Text Analyser keeps track of recently run scripts, which can be accessed by clicking on the drop down box at the end of the ‘Script’ field. This list is initially populated by some example scripts that are provided with Chinese Text Analyser.

You can then click ‘Run’ to run the script.

If there was an error with loading or running the script, information about the error will be shown in the dialog box:

_images/script-error.png

Script Details

If you would like information about your script to be displayed on the ‘Run Script’ dialog, you can add a special comment to the beginning of your script file.

The comment should be standard multi-line Lua comment that contains a special marker @cta at the beginning of a line. Everything after the marker will be displayed in the ‘Run Script’ dialog. You can split the comment over multiple lines by starting each line with a space.

For example the ‘anki-cloze.lua’ script that is one of the example scripts provided with Chinese Text Analyser has the following comment:

--[[
@cta Recursively scans a directory for .txt files.  Each file is processed
 to find 'mostly known' sentences, and each sentence found is output as an
 Anki compatible cloze deleted sentence.
--]]

Which is displayed like so:

_images/script-details.png

Script Monitoring

While the script is running, the ‘Run Script’ dialog will be updated to show the total running time and the total amount of bytes generated by the script.

If no output has been generated for a long period of time, it is likely that the script has hung or is stuck in an infinite loop.

You can kill running scripts at any time by clicking on the ‘cross’ icon that appears while the script is running.

_images/script-running.png

Script Output

Once a script has finished running you can copy the output to the clipboard, save the output to a file, or both. You do this by clicking on the ‘Clipboard’ or ‘File’ buttons.

If the ‘Always copy to clipboard’ checkbox is checked, then the output of the script will automatically be copied to the clipboard when the script finishes running.

_images/script-finished.png