Language

A text-processing pipeline. Usually you'll load this once per process, and pass the instance around your application.

Language.__init__

Initialise a Language object.

NameTypeDescription
vocabVocab A Vocab object. If True, a vocab is created via Language.Defaults.create_vocab.
make_doccallable A function that takes text and returns a Doc object. Usually a Tokenizer.
pipelinelist A list of annotation processes or IDs of annotation, processes, e.g. a Tagger object, or 'tagger'. IDs are looked up in Language.Defaults.factories.
metadict Custom meta data for the Language class. Is written to by models to add model meta data.
returnsLanguageThe newly constructed object.

Language.__call__

Apply the pipeline to some text. The text can span multiple sentences, and can contain arbtrary whitespace. Alignment into the original string is preserved.

NameTypeDescription
textunicodeThe text to be processed.
disablelist Names of pipeline components to disable.
returnsDocA container for accessing the annotations.

Language.pipe

Process texts as a stream, and yield Doc objects in order. Supports GIL-free multi-threading.

NameTypeDescription
texts-A sequence of unicode objects.
n_threadsint The number of worker threads to use. If -1, OpenMP will decide how many to use at run time. Default is 2.
batch_sizeintThe number of texts to buffer.
disablelist Names of pipeline components to disable.
yieldsDocDocuments in the order of the original text.

Language.update

Update the models in the pipeline.

NameTypeDescription
docsiterableA batch of Doc objects.
goldsiterableA batch of GoldParse objects.
dropfloatThe dropout rate.
sgdcallableAn optimizer.
returnsdictResults from the update.

Language.begin_training

Allocate models, pre-process training data and acquire an optimizer.

NameTypeDescription
gold_tuplesiterableGold-standard training data.
**cfg-Config parameters.
yieldstupleAn optimizer.

Language.use_params

Replace weights of models in the pipeline with those provided in the params dictionary. Can be used as a contextmanager, in which case, models go back to their original weights after the block.

NameTypeDescription
paramsdictA dictionary of parameters keyed by model ID.
**cfg-Config parameters.

Language.preprocess_gold

Can be called before training to pre-process gold data. By default, it handles nonprojectivity and adds missing tags to the tag map.

NameTypeDescription
docs_goldsiterableTuples of Doc and GoldParse objects.
yieldstupleTuples of Doc and GoldParse objects.

Language.to_disk

Save the current state to a directory. If a model is loaded, this will include the model.

NameTypeDescription
pathunicode or Path A path to a directory, which will be created if it doesn't exist. Paths may be either strings or Path-like objects.
disablelist Names of pipeline components to disable and prevent from being saved.

Language.from_disk

Loads state from a directory. Modifies the object in place and returns it. If the saved Language object contains a model, the model will be loaded.

NameTypeDescription
pathunicode or Path A path to a directory. Paths may be either strings or Path-like objects.
disablelist Names of pipeline components to disable.
returnsLanguageThe modified Language object.

Language.to_bytes

Serialize the current state to a binary string.

NameTypeDescription
disablelist Names of pipeline components to disable and prevent from being serialized.
returnsbytesThe serialized form of the Language object.

Language.from_bytes

Load state from a binary string.

NameTypeDescription
bytes_databytesThe data to load from.
disablelist Names of pipeline components to disable.
returnsLanguageThe Language object.

Attributes

NameTypeDescription
vocabVocabA container for the lexical types.
tokenizerTokenizerThe tokenizer.
make_doclambda text: DocCreate a Doc object from unicode text.
pipelinelistSequence of annotation functions.
metadict Custom meta data for the Language class. If a model is loaded, contains meta data of the model.

Class attributes

NameTypeDescription
Defaultsclass Settings, data and factory methods for creating the nlp object and processing pipeline.
langunicode Two-letter language ID, i.e. ISO code.