The Javascript Annotator allows annotations to be generated by executing a program or expression written in JavaScript.

The layer manager has two modes of operation:

Label Mapping
New annotation labels are generated from a small JavaScript expression operating on the label from a given source layer.

The label of each annotation from the source layer directly generates the label of one annotation on the destination layer.

For each distinct label on the source layer, the specified expression is executed on the label, and the result of the expression is saved on the destination layer.

Some illustrative examples:

  • length - saves the length of the source label on the destination layer,
    e.g. test → 4
  • toUpperCase() - converts all lowercase letters to uppercase,
    e.g. test → TEST
  • replace(/[aeiou]/g,"") - removes all vowels,
    e.g. test → tst
  • replace(/[aeiou]/g,"V").replace(/[^aeiou]/g,"C") - replaces all vowels with V and all non-vowels with C,
    e.g. test → CVCC
Any operation or string of operations that would work on a JavaScript String object are valid.

The following is a list of valid string operations, curtesy of W3 Schools

Method Description
charAt(index) Returns the character at the specified index (position)
charCodeAt(index) Returns the Unicode of the character at the specified index
concat(string1, string2, ..., stringX) Joins two or more strings, and returns a new joined strings
endsWith(searchvalue,length) Checks whether a string ends with specified string/characters
includes(searchvalue,start) Checks whether a string contains the specified string/characters
indexOf(searchvalue,start) Returns the position of the first found occurrence of a specified value in a string
lastIndexOf(searchvalue,start) Returns the position of the last found occurrence of a specified value in a string
match(regexp) Searches a string for a match against a regular expression, and returns the matches
repeat(count) Returns a new string with a specified number of copies of an existing string
replace(searchvalue,newvalue) Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
search(searchvalue) Searches a string for a specified value, or regular expression, and returns the position of the match
slice(start,end) Extracts a part of a string and returns a new string
split(separator,limit) Splits a string into an array of substrings
startsWith(searchvalue,start) Checks whether a string begins with specified characters
substr(start,length) Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
substring(start,end) Extracts the characters from a string, between two specified indices
toLocaleLowerCase() Converts a string to lowercase letters, according to the host's locale
toLocaleUpperCase() Converts a string to uppercase letters, according to the host's locale
toLowerCase() Converts a string to lowercase letters
toUpperCase() Converts a string to uppercase letters
trim() Removes whitespace from both ends of a string
Full Script
A full script is given the whole annotation graph, which it can traverse, process, and add annotations to.

The script is executed for each transcript, with a variable called transcript which stores the transcript's Annotation Graph. transcript can be traversed to inspect annotations, and can have annotations added to it (on this layer only), which will be saved back to LaBB-CAT's annotation store when the script completes.

The transcript graph is populated with annotations from the layers that are referring to in script.

If your script creates new annotations on a layer that doesn't exist yet, (e.g. by using annotation.createTag("layer", label)), then the mentioned layer will be automatically created as an interval span layer. If you want another type of layer (e.g. a word tag layer), be sure to click the New Layer button, fill in the details that appear, and click the Define Layer button, to ensure the right type of layer is created.

The script might look something like the following, which tags each word with its length:

// for each turn in the transcript
for each (turn in transcript.all("turns")) {
  if (annotator.cancelling) break; // cancelled by the user
  // for each word
  for each (word in turn.all("word")) {
    // tag the word with it's length
    word.createTag("length", "length: " + word.label.length());
    log("Tagged word: " + word.label);
  } // next word
} // next turn
            
While Label Edit mode operates purely with JavaScript objects, Full Script mode operates on underlying Java objects. One effect of this is that, when operating on Strings (e.g. annotation labels), the Java (not JavaScript) String API is available.

Java String operations include:

char charAt(int index)
Returns the char value at the specified index.
int compareTo(String anotherString)
Compares two strings lexicographically.
int compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences.
String concat(String str)
Concatenates the specified string to the end of this string.
boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.
boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
boolean isEmpty()
Returns true if, and only if, length() is 0.
int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
int lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
int length()
Returns the length of this string.
boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
boolean regionMatches(int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
String[] split(String regex)
Splits this string around matches of the given regular expression.
String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
boolean startsWith(String prefix, int toffset)
Tests if the substring of this string beginning at the specified index starts with the specified prefix.
String substring(int beginIndex)
Returns a new string that is a substring of this string.
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.
String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.

// for each source annotation in the transcript
for each (var annotation in transcript.all("layer")) {
   // tag the annotation
   annotation.createTag("annotation", annotation.label.);
} // next source annotation
Script