CWB Encoding Tutorial
CWB Encoding Tutorial
Contents
1 Prerequisites
10
8 Sentence alignment
12
12
Prerequisites
In order to follow this tutorial, you need to install the IMS Open Corpus Workbench (CWB),
version 3.0 or newer, which can be downloaded from
http://cwb.sourceforge.net/download.php
It is easiest to install a pre-compiled binary package, following instructions on the Web page and in
the enclosed README file. You should also install the CWB/Perl interface, which includes the useful
cwb-make and cwb-regedit programs. A data package with all input files needed for the examples
in this tutorial below is available from
http://cwb.sourceforge.net/documentation.php
The standard CWB input format is one-word-per-line text,1 with the surface form in the first column
and token-level annotations specified as additional TAB-separated columns. XML tags for sentence
boundaries and other structural annotation must appear on separate lines. This file format is also
called verticalized text and has the customary file extension .vrt. An example of the verticalized
text format for a short sentence with part-of-speech and lemma annotations is shown in Figure 1.
This file, as well as all other input files required by the following examples are made available in the
accompanying data package.
<s>
It
was
an
elephant
.
</s>
PP
VBD
DT
NN
SENT
it
be
an
elephant
.
Previous versions of the CWB would default to the current working directory. As a result, simply typing cwb-encode
on the command line would litter this directory with a number of empty data files and then hang, waiting for corpus
data on the standard input.
It is probably a good idea to add this setting to your login profile (~/.profile or ~/.login).
If you do not want to set the environment variable, you need to invoke cwb-make with
$ cwb-make -r /home/stefan/registry -V EXAMPLE
The following examples assume that you either use the default registry directory or have set the
CORPUS_REGISTRY variable appropriately.
You can also specify multiple registry directories separated by colon characters (:), both in
the CORPUS_REGISTRY environment variable and the -r options of command-line tools. This is
convenient e.g. if some corpora are stored on external hard drives that are not always mounted.
Such optional registry directories may be prefixed by a question mark (?) in order to indicate
that they may not be accessible (otherwise CQP and some other tools will print warnings to
alert you to possible typos in the registry path). For instance, one of the lead CWB developers
has the following registry path in his ~/.bashrc configuration:
$ export CORPUS_REGISTRY=/Corpora/registry:?/Volumes/X/CWB/registry
Note that the built-in default registry directory /usr/local/share/cwb/registry is not automatically appended to this path. If you want to specify additional registry directories but keep
the default one, you will have to include it in the value of CORPUS_REGISTRY.
The -V switch enables additional validation passes when an index is created and when data files are
compressed. It should be omitted when encoding very large corpora (above 50 million tokens), in
order to speed up processing. In this case, it is also advisable to limit memory usage with the -M
option. The amount specified should be somewhat less than the amount of physical RAM available
(depending on the number of users etc.; too little is better than too much). For instance, on a Linux
machine with 128 MB of RAM, -M 64 is a safe choice. Note that the cwb-make utility applies a default
limit of -M 75 if not explicit -M option is given.
Display some information about the encoded corpus (add -s option for details and to ascertain
that all necessary data files have been created).
$ cwb-describe-corpus EXAMPLE
If you do not have the CWB/Perl inteface installed, by far the best thing you can do is to install
the CWB/Perl modules and the included scripts, and then go back to Section 2. If it is absolutely
impossible to install CWB/Perl or you really want to learn the nitty-gritty of corpus encoding, continue
here.
In the manual procedure, indexing and compression are performed in separate steps by dierent
tools. First, you have to run cwb-makeall in order to build the necessary index files.
cwb-makeall -V EXAMPLE
Note that cwb-makeall accepts the same -V, -M and -r options as cwb-make.
When the index files have been created, the corpus can already be used with CQP and other CWB
tools. However, it is recommended that you compress the binary data files to save disk space and
improve performance, especially for large corpora (above 10 million tokens). Compression is only
supported for p-attributes at the current time.
c 20052010 Stefan Evert & The OCWB Development Team
For positional attributes, both the token stream data and the index can be compressed. There
are separate tools for compressing the two types of data files.
The token stream can be compressed with the cwb-huffcode tool. Use the -P option to process
a single attribute, or compress all p-attributes with -A.
$ cwb-huffcode -A EXAMPLE
Index files can be compressed with the cwb-compress-rdx tool, which accepts the same options.
$ cwb-compress-rdx -A EXAMPLE
When compression was successful, both tools will display the full pathnames of uncompressed data
files that are now redundant and can be deleted (namely, attrib.corpus after running cwb-huffcode,
as well as attrib.corpus.rev and attrib.corpus.rdx after running cwb-compress-rdx).
If you run cwb-makeall again, it will show now that the p-attributes are compressed. Note that the
compressed data files are validated by default, so it is safe to remove the redundant files. Validation
can be turned o with the -T option, but is less performance-critical than with cwb-makeall.
NB: If you re-encode a corpus, it is important to erase all files in the data directory first. The
cwb-makeall program will not recognize that existing index files or compressed data files are
out of date, and will therefore fail to rebuild them automatically. This is one of the reasons why
the CWB/Perl cwb-make tool should be preferred.
Nowadays, machine-readable text and linguistic annotations are often provided in XML format.
Version 3.0 of the IMS Open Corpus Workbench oers improved XML support, which is activated
by the following encoding options: -x for XML compatibility mode (recognises default entities and
skips comments as well as an XML declaration), -s to skip blank lines in the input, and -B to strip
whitespace from tokens. The verticalized text format with TAB-separated p-attributes is still required
by cwb-encode, but this format can easily be generated from an arbitrary XML file with the help of
an XSLT stylesheet. Figure 2 shows a typical example of an XML input file for the CWB (note that
this is still a well-formed XML file).
XML elements (i.e. matching pairs of start and end tags) can be encoded as s-attributes, which have
to be declared with -S flags (for the file vss.vrt, the flags -S story -S p -S s would be used). If XML
regions of the same type are nested, encoding will only work correctly if you add :0 to the s-attribute
declaration, which enables a rudimentary XML parser built into cwb-encode. Attribute-value pairs
in XML start tags, such as
<story num="4" title="A Thrilling Experience">
can be stored as a single unparsed text string (num="4" title="A Thrilling Experience") by using
the flag -V instead of -S. This form of encoding is not convenient for CQP queries, though. It
is more desirable to declare XML tag attributes explicitly, which will automatically split the XML
elements into multiple s-attributes. Note that the options -xsB should (almost) always be used and
will automatically ignore the XML declaration and the comment line in Figure 2.
Encode the verticalized XML file vss.vrt as a CWB corpus, with indexing and compression. NB:
The last attribute declaration flag (-0 collection) is a digit zero (for a null attribute, see
below).
c 20052010 Stefan Evert & The OCWB Development Team
to obtain a well-formed XML file. Instead of removing such tags during data preparation, they can
directly be filtered out by the cwb-encode tool. For this purpose, they have to be declared with the
flag -0 (digit zero, for null attribute) instead of -S or -V. All start and end tags of these elements
will be ignored completely. There is no need to add :0 or XML attribute declarations. Note that
all XML tags that have not been declared with a -S, -V or -0 flag will be encoded as literal tokens
(without annotations), accompanied by a warning message.
You may have noticed in Figure 2 that the XML file is declared to be in ISO-8859-1 (or Latin1) encoding rather than the standard UTF-8 format. The CWB has been developed exclusively for
ISO-8859-1 data (which is still widely used for German corpus data, e.g. by the TreeTagger and
associated tools). While it is possible to store and query data in other ISO-8859-x encodings or
Unicode data in UTF-8 format some features will not work properly unless the ISO-8859-1 encoding
is used.4 If you need to handle non-Latin-1 data with the CWB, make sure that you are aware of the
precise limitations. It is probably a good idea to look for other users on the CWB mailing list who
have experience with such corpora. Version 3.1 of the CWB is expected to provide basic support for
ISO-8859-x and UTF-8 encodings.
In order to add positional attributes to a corpus that has already been encoded, create input data
in the standard verticalized format, but listing only the new attributes. Figure 3 shows an example of
such an input file, containing WordNet synonyms for the tokens from Figure 1 (without attempting
any form of word sense disambiguation). A corresponding list of synonyms for the complete VSS corpus
can be found in the file syns.vrt.
|
|be|cost|live|work|equal|exist|occur|...|
|
|elephant|
|
Figure 3: WordNet synonyms for the text shown in Figure 1 (excerpt from file syns.vrt)
The special notation seen in Figure 3 indicates that the synonyms for any given word constitute
an unordered set (or feature set in CWB terminology). Vertical bars (|) separate individual set
elements and enclose the entire set; a single bar | denotes the empty set. Feature sets are stored as
plain strings in a CWB-encoded corpus, but the special notation enables the query processor CQP
to test whether a particular string is contained in the set, match all set elements against a regular
expression, and compute the intersection of two sets.
The file syns.vrt is encoded as usual, but the default word attribute has to be suppressed with
the option -p -. It is highly recommended to check that the number of tokens in the new file
(wc -l syns.vrt) is equal to the corpus size (as reported by cwb-lexdecode -S EXAMPLE), so
that the new attribute is properly aligned to the rest of the corpus.
$ cwb-encode -d /corpora/data/vss -f syns.vrt -p - -P syn/
4
In particular, case-insensitive (%c) and accent-insensitive (%d) matching as well as LATEX notation for accented
characters are only supported for ISO-8859-1 data. Regular expressions do not work properly for UTF-8 data and should
only be used to express simple prefix and suffix constraint such as .*able in this case.
Notice the slash (/) appended to the attribute name syn. This notation indicates that the
new attribute should be treated as a feature set; cwb-encode will automatically validate and
normalise the supplied values, issuing warnings if they are not well-formed feature sets.
The registry file for the corpus VSS (usually /usr/local/share/cwb/registry/vss) has to be edited
in order to declare the new attribute. Add the line
ATTRIBUTE syn
at the bottom of the file. If the CWB/Perl interface has been installed, the registry file can also
be edited from the command line with the cwb-regedit registry editor script:
$ cwb-regedit VSS :add :p syn
This script can also be used to list and delete attributes, and to print basic information about a
corpus (similar to cwb-describe-corpus, but easier for further processing). Type cwb-regedit -h
for further information.
Now you can build index files and compress the new attribute:
$ cwb-make -V VSS
In order to add structural attributes with computed start and end points (corpus positions),
you can use the cwb-s-encode tool. The corresponding start and end positions of existing s-attributes
can be obtained with cwb-s-decode. The following example adds information about sentence length
to the VSS corpus.
The existing s attribute is decoded into a temporary file, then awk is used to compute sentence
lengths, and the resulting annotated regions are encoded with cwb-s-encode.
$ cwb-s-decode VSS -S s > s.list
$ awk BEGIN { FS=OFS="\t" } { print $1, $2, $2-$1+1 } s.list > s_len.list
$ cwb-s-encode -d /corpora/data/vss -f s_len.list -V s_len
Note that it is currently not necessary to run cwb-make after adding an s-attribute.
However, the new attribute still has to be declared in the registry file, either by manually adding
STRUCTURE s_len
or from the command line using the registry editor script:
$ cwb-regedit VSS :add :s s_len
Tables of corpus positions as input for cwb-s-encode can also be created from CQP query results
using the dump or tabulate command in a CQP session.
In order to add XML annotations (e.g. <np> and <pp> tags inserted by a chunk parser) to an
existing corpus, the usual strategy is to decode the token stream (and other attributes if necessary) to
a temporary file. A chunk parser will often expect <s> and </s> tags marking sentence boundaries.
Decode token stream (word forms) with start and end tags for <s> regions.
c 20052010 Stefan Evert & The OCWB Development Team
The cwb-lexdecode tool procides access to the lexicon of positional attributes, i.e. lists of all word
forms or annotation strings (types) with their corpus frequencies. The -S option prints the size of
corpus (tokens) and lexicon (types) only, -P selects the desired p-attribute, -f shows corpus frequencies,
and -s lists the lexicon entries alphabetically (according to the internal sort order). In order to sort
the lexicon by frequency, an external program (e.g. sort) has to be used.
$ cwb-lexdecode -S
-P lemma VSS
$ cwb-lexdecode -f -s -P lemma VSS | tail -20
$ cwb-lexdecode -f
-P lemma VSS | sort -nr -k 1 | head -20
It is also possible to annotate strings from a file (called tags.txt here) with corpus frequencies. The
file must be in one-word-per-line format. -0 (digit zero) prints a frequency of 0 for unknown strings
rather than issuing a warning message; it can be combined with -f to the mnemonic form -f0.
$ cwb-lexdecode -f0 -P pos -F tags.txt VSS
With the -p option, word forms or annotations matching a regular expression can be extracted. Caseinsensitive and accent-insensitive matching is selected with -c and -d, respectively.6 The example
below is similar to the CQP query [lemma = "over.+" %c]; but may be considerably faster on a
large corpus.
$ cwb-lexdecode -f -P lemma -p over.+ -c VSS
An entire corpus or selected attributes from a corpus can be printed in various formats with the
cwb-decode tool. Note that options and switches must appear before the corpus name, and the flags
used to select attributes after the corpus name. Use -P to select p-attributes and -S for s-attributes.
With the -s and -e options, a part of the corpus (identified by start and end corpus position) can be
printed.
5
6
http://www.ims.uni-stuttgart.de/~kermes/YAC/YAC.shtml
Recall that these flags are only guaranteed to work correctly for a corpus in ISO-8859-1 (Latin-1) encoding.
10
-C -s 7299 -e 7303
VSS
-P word -P pos -S s
-C refers to the compact one-word-per-line format expected by cwb-encode. For a full textual copy
of a CWB corpus, use -ALL to select all positional and structural attributes.
$ cwb-decode
-C
VSS
-ALL
> vss-corpus.vrt
The resulting file vss-corpus.vrt can be re-encoded with cwb-encode (using appropriate flags) to give
an exact copy of the VSS corpus. -Cx is almost identical to the compact format, but changes some
details in order to generate a well-formed XML document (unless there are overlapping regions or
s-attributes with simple annotations).7
$ cwb-decode -Cx VSS -ALL > vss-corpus.xml
$ xmllint --noout vss-corpus.xml # not well-formed :-(
This output format can reliably be re-encoded when the -xsB options are used. Finally, -X produces a
native XML output format (following a fixed DTD), which can be post-processed and formatted with
XSLT stylesheets.
$ cwb-decode
-X -s 7299 -e 7303
VSS
Note that the regions of s-attributes are not translated into XML regions. Instead, the start and end
tags are represented by special empty <tag> elements.
The cwb-scan-corpus computes combinatorial frequency tables for an encoded corpus. Similar
to the group command in CQP, it is a faster and more memory-efficient alternative for the extraction
of simple structures from large corpora, and is not restricted to singletons and pairs. The output
of cwb-scan-corpus is an unordered list of n-tuples and their frequencies, which have to be postprocessed and sorted with external tools. The simple example below prints the twenty most frequent
(lemma, pos) pairs in the VSS corpus, using the -C option to filter punctuation and noise from the list
of lemmata (note that -C applies to all selected attributes).
$ cwb-scan-corpus -C VSS lemma pos | sort -nr -k 1 | head -20
A non-negative oset can be added to each field key in order to collect bigrams, trigrams, etc. The
following example derives a simple language model in the form of all sequences of three consecutive
part-of-speech tags together with their occurrence counts. Only the twenty most frequent sequences
are displayed.
$ cwb-scan-corpus VSS pos+0 pos+1 pos+2 | sort -nr -k 1 | head -20
For a large corpus such as the BNC, the scan results can directly be written to a file with the -o switch.
If the filename ends in .gz (such as the file language-model.gz in the example below), the output file
is automatically compressed (using gzip).
$ cwb-scan-corpus -o language-model.gz BNC pos+0 pos+1 pos+2
7
In order to re-create the original input file vss.vrt as a well-formed XML document, it would have been necessary to
store the full strings of attribute-value pairs from XML start tags by using -V flags instead of -S in cwb-encode attribute
declarations (e.g. -V story:0+num+title). In the cwb-decode call, problematic s-attributes created by auto-splitting of
these attribute-value pairs (story_num, story_title, s_len, np_head, . . . ) can then be omitted. The specification -S
story would print the full attribute-value pairs in <story> tags, etc.
11
The values of the selected p-attributes can also be filtered with regular expressions. The following
command identifies part-of-speech sequences at the end of sentences (indicated by the tag SENT =
sentence-ending punctuation).
$ cwb-scan-corpus VSS pos+0 pos+1 pos+2=/SENT/ | sort -nr -k 1 | head -20
Since the third key is used only for filtering, we can suppress it in the output by marking it as a
constraint key with the ? character. Note that it may be necessary to enclose more complex keys
(containing shell metacharacters) in single quotes.
$ cwb-scan-corpus VSS pos+0 pos+1 ?pos+2=/SENT/ | sort -nr -k 1 | head -20
Note that cwb-scan-corpus can operate both on p-attributes and on s-attributes with annotated
values. To obtain by-story frequency lists for the VSS corpus, use the following command:
$ cwb-scan-corpus -o freq-by-story.tbl VSS lemma+0 story_title+0
As a special case, s-attributes without annotated values can be used to restrict the corpus scan to
regions of a particular type. For instance, the constraint key ?footnote would only scan <footnote>
regions. Keep in mind that such special constraints must not include a regular expression part.
The final example extracts pairs of adjacent adjectives and nouns from the VSS corpus, e.g. as candidate
data for adjective-noun collocations. Constraint keys are used to identify adjectives and nouns, and
only nouns starting with a vowel are accepted here. Note the c and d modifiers (case- and diacriticsinsensitive matching) on this regular expression.
$ cwb-scan-corpus -C VSS lemma+0 ?pos+0=/JJ.*/ lemma+1=/[aeiou].+/cd ?pos+1=/NN.*/
Except for the -C option, this command line is equivalent to the following CQP commands, but it will
execute much faster on a large corpus.
> A = [pos = "JJ.*"] [pos = "NN.*" & lemma = "[aeiou].+" %cd];
> group A matchend lemma by match lemma;
The cwb-scan-corpus command is limited to relatively simple constraints on tokens and it can only
match patterns with fixed osets (but not e.g. determiner and noun separated by an arbitrary number
of adjectives). To obtain frequency tables for more complex patterns, use CQP queries in combination
with the tabulate function. The resulting data tables can be saved to disk files and loaded into a
relational database or processed with a software package for statistical analysis.
Sentence alignment
Describe sentence aligner, encoding of alignment attributes, and import of pre-existing alignment
TODO
TODO
CWB traditionally had a more flexible registry file format (which is still accepted for backward
compatibility), but standard format is recommended for new corpora (and is enforced by the
CWB/Perl scripts)
12
note that directory and file paths in HOME and INFO entries have to be double-quoted if they
contain blanks or other non-standard characters (ASCII letters, digits, -, _, / and . are ok, as
long as path does not begin with .); in double-quoted path, " must be escaped as \" and the
backslash \ as \\; cwb-encode and cwb-regedit should always create valid entries, quoted as
necessary
13