pynlpl.formats.folia.Description

class pynlpl.formats.folia.Description(doc, *args, **kwargs)

Bases: pynlpl.formats.folia.AbstractElement

Description is an element that can be used to associate a description with almost any other FoLiA element

Method Summary

__init__(doc, *args, **kwargs) Required keyword arguments: * value=: The text content for the description (str or unicode)
accepts(Class[, raiseexceptions, parentinstance])
add(child, *args, **kwargs)
addable(parent[, set, raiseexceptions]) Tests whether a new element of this class can be added to the parent.
addidsuffix(idsuffix[, recursive]) Appends a suffix to this element’s ID, and optionally to all child IDs as well.
addtoindex([norecurse]) Makes sure this element (and all subelements), are properly added to the index.
ancestor(*Classes) Find the most immediate ancestor of the specified type, multiple classes may be specified.
ancestors([Class]) Generator yielding all ancestors of this element, effectively back-tracing its path to the root element.
append(child, *args, **kwargs)
context(size[, placeholder, scope]) Returns this word in context, {size} words to the left, the current word, and {size} words to the right
copy([newdoc, idsuffix]) Make a deep copy of this element and all its children.
copychildren([newdoc, idsuffix]) Generator creating a deep copy of the children of this element.
count(Class[, set, recursive, ignore, node]) Like AbstractElement.select(), but instead of returning the elements, it merely counts them.
deepvalidation() Perform deep validation of this element.
description() Obtain the description associated with the element.
feat(subset) Obtain the feature class value of the specific subset.
findcorrectionhandling(cls) Find the proper correctionhandling given a textclass by looking in the underlying corrections where it is reused
findreplaceables(parent[, set]) Internal method to find replaceable elements.
getindex(child[, recursive, ignore]) Get the index at which an element occurs, recursive by default!
getmetadata([key]) Get the metadata that applies to this element, automatically inherited from parent elements
gettextdelimiter([retaintokenisation]) Return the text delimiter for this class.
hasphon([cls, strict, correctionhandling]) Does this element have phonetic content (of the specified class)
hastext([cls, strict, correctionhandling]) Does this element have text (of the specified class)
incorrection() Is this element part of a correction? If it is, it returns the Correction element (evaluating to True), otherwise it returns None
insert(index, child, *args, **kwargs)
items([founditems]) Returns a depth-first flat list of all items below this element (not limited to AbstractElement)
json([attribs, recurse, ignorelist]) Serialises the FoLiA element and all its contents to a Python dictionary suitable for serialisation to JSON.
leftcontext(size[, placeholder, scope]) Returns the left context for an element, as a list.
next([Class, scope, reverse]) Returns the next element, if it is of the specified type and if it does not cross the boundary of the defined scope.
originaltext([cls]) Alias for retrieving the original uncorrect text.
parsexml(node, doc, **kwargs) Internal class method used for turning an XML element into an instance of the Class.
phon([cls, previousdelimiter, strict, …]) Get the phonetic representation associated with this element (of the specified class)
phoncontent([cls, correctionhandling]) Get the phonetic content explicitly associated with this element (of the specified class).
postappend() This method will be called after an element is added to another and does some checks.
previous([Class, scope]) Returns the previous element, if it is of the specified type and if it does not cross the boundary of the defined scope.
relaxng([includechildren, extraattribs, …]) Returns a RelaxNG definition for this element (as an XML element (lxml.etree) rather than a string)
remove(child) Removes the child element
replace(child, *args, **kwargs) Appends a child element like append(), but replaces any existing child element of the same type and set.
resolveword(id)
rightcontext(size[, placeholder, scope]) Returns the right context for an element, as a list.
select(Class[, set, recursive, ignore, node]) Select child elements of the specified class.
setdoc(newdoc) Set a different document.
setdocument(doc) Associate a document with this element.
setparents() Correct all parent relations for elements within the scop.
settext(text[, cls]) Set the text for this element.
speech_speaker() Retrieves the speaker of the audio or video file associated with the element.
speech_src() Retrieves the URL/filename of the audio or video file associated with the element.
stricttext([cls]) Alias for text() with strict=True
text([cls, retaintokenisation, …]) Get the text associated with this element (of the specified class)
textcontent([cls, correctionhandling]) Get the text content explicitly associated with this element (of the specified class).
textvalidation([warnonly]) Run text validation on this element.
toktext([cls]) Alias for text() with retaintokenisation=True
updatetext() Recompute textual value based on the text content of the children.
xml([attribs, elements, skipchildren]) Serialises the FoLiA element and all its contents to XML.
xmlstring([pretty_print]) Serialises this FoLiA element and all its contents to XML.
__iter__() Iterate over all children of this element.
__len__() Returns the number of child elements under the current element.
__str__() Alias for text()

Class Attributes

ACCEPTED_DATA = (<class 'pynlpl.formats.folia.Description'>, <class 'pynlpl.formats.folia.Comment'>)
ANNOTATIONTYPE = None
AUTH = True
AUTO_GENERATE_ID = False
LABEL = 'Description'
OCCURRENCES = 1
OCCURRENCES_PER_SET = 0
OPTIONAL_ATTRIBS = (0, 2, 3, 5, 4, 11)
PHONCONTAINER = False
PRIMARYELEMENT = True
PRINTABLE = False
REQUIRED_ATTRIBS = None
REQUIRED_DATA = None
SETONLY = False
SPEAKABLE = False
SUBSET = None
TEXTCONTAINER = False
TEXTDELIMITER = None
XMLTAG = 'desc'

Method Details

__init__(doc, *args, **kwargs)

Required keyword arguments: * value=: The text content for the description (str or unicode)

__init__(doc, *args, **kwargs)

Required keyword arguments: * value=: The text content for the description (str or unicode)

classmethod accepts(Class, raiseexceptions=True, parentinstance=None)
add(child, *args, **kwargs)
classmethod addable(parent, set=None, raiseexceptions=True)

Tests whether a new element of this class can be added to the parent.

This method is mostly for internal use. This will use the OCCURRENCES property, but may be overidden by subclasses for more customised behaviour.

Parameters:
  • parent (AbstractElement) – The element that is being added to
  • set (str or None) – The set
  • raiseexceptions (bool) – Raise an exception if the element can’t be added?
Returns:

bool

Raises:

ValueError

addidsuffix(idsuffix, recursive=True)

Appends a suffix to this element’s ID, and optionally to all child IDs as well. There is sually no need to call this directly, invoked implicitly by copy()

addtoindex(norecurse=[])

Makes sure this element (and all subelements), are properly added to the index.

Mostly for internal use.

ancestor(*Classes)

Find the most immediate ancestor of the specified type, multiple classes may be specified.

Parameters:*Classes – The possible classes (AbstractElement or subclasses) to select from. Not instances!

Example:

paragraph = word.ancestor(folia.Paragraph)
ancestors(Class=None)

Generator yielding all ancestors of this element, effectively back-tracing its path to the root element. A tuple of multiple classes may be specified.

Parameters:*Class – The class or classes (AbstractElement or subclasses). Not instances!
Yields:elements (instances derived from AbstractElement)
append(child, *args, **kwargs)
context(size, placeholder=None, scope=None)

Returns this word in context, {size} words to the left, the current word, and {size} words to the right

copy(newdoc=None, idsuffix='')

Make a deep copy of this element and all its children.

Parameters:
  • newdoc (Document) – The document the copy should be associated with.
  • idsuffix (str or bool) – If set to a string, the ID of the copy will be append with this (prevents duplicate IDs when making copies for the same document). If set to True, a random suffix will be generated.
Returns:

a copy of the element

copychildren(newdoc=None, idsuffix='')

Generator creating a deep copy of the children of this element.

Invokes copy() on all children, parameters are the same.

count(Class, set=None, recursive=True, ignore=True, node=None)

Like AbstractElement.select(), but instead of returning the elements, it merely counts them.

Returns:int
deepvalidation()

Perform deep validation of this element.

Raises:DeepValidationError
description()

Obtain the description associated with the element.

Raises:NoSuchAnnotation if there is no associated description.
feat(subset)

Obtain the feature class value of the specific subset.

If a feature occurs multiple times, the values will be returned in a list.

Example:

sense = word.annotation(folia.Sense)
synset = sense.feat('synset')
Returns:str or list
findcorrectionhandling(cls)

Find the proper correctionhandling given a textclass by looking in the underlying corrections where it is reused

classmethod findreplaceables(parent, set=None, **kwargs)

Internal method to find replaceable elements. Auxiliary function used by AbstractElement.replace(). Can be overriden for more fine-grained control.

getindex(child, recursive=True, ignore=True)

Get the index at which an element occurs, recursive by default!

Returns:int
getmetadata(key=None)

Get the metadata that applies to this element, automatically inherited from parent elements

gettextdelimiter(retaintokenisation=False)

Return the text delimiter for this class.

Uses the TEXTDELIMITER attribute but may return a customised one instead.

hasphon(cls='current', strict=True, correctionhandling=1)

Does this element have phonetic content (of the specified class)

By default, and unlike phon(), this checks strictly, i.e. the element itself must have the phonetic content and it is not inherited from its children.

Parameters:
  • cls (str) – The class of the phonetic content to obtain, defaults to current.
  • strict (bool) – Set this if you are strictly interested in the phonetic content explicitly associated with the element, without recursing into children. Defaults to True.
  • correctionhandling – Specifies what phonetic content to check for when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current phonetic content. You can set this to CorrectionHandling.ORIGINAL if you want the phonetic content prior to correction, and CorrectionHandling.EITHER if you don’t care.
Returns:

bool

hastext(cls='current', strict=True, correctionhandling=1)

Does this element have text (of the specified class)

By default, and unlike text(), this checks strictly, i.e. the element itself must have the text and it is not inherited from its children.

Parameters:
  • cls (str) – The class of the text content to obtain, defaults to current.
  • strict (bool) – Set this if you are strictly interested in the text explicitly associated with the element, without recursing into children. Defaults to True.
  • correctionhandling – Specifies what text to check for when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current text. You can set this to CorrectionHandling.ORIGINAL if you want the text prior to correction, and CorrectionHandling.EITHER if you don’t care.
Returns:

bool

incorrection()

Is this element part of a correction? If it is, it returns the Correction element (evaluating to True), otherwise it returns None

insert(index, child, *args, **kwargs)
items(founditems=[])

Returns a depth-first flat list of all items below this element (not limited to AbstractElement)

json(attribs=None, recurse=True, ignorelist=False)

Serialises the FoLiA element and all its contents to a Python dictionary suitable for serialisation to JSON.

Example:

import json
json.dumps(word.json())
Returns:dict
leftcontext(size, placeholder=None, scope=None)

Returns the left context for an element, as a list. This method crosses sentence/paragraph boundaries by default, which can be restricted by setting scope

next(Class=True, scope=True, reverse=False)

Returns the next element, if it is of the specified type and if it does not cross the boundary of the defined scope. Returns None if no next element is found. Non-authoritative elements are never returned.

Parameters:
  • Class (*) – The class to select; any python class subclassed off ‘AbstractElement`, may also be a tuple of multiple classes. Set to True to constrain to the same class as that of the current instance, set to None to not constrain at all
  • scope (*) – A list of classes which are never crossed looking for a next element. Set to True to constrain to a default list of structure elements (Sentence,Paragraph,Division,Event, ListItem,Caption), set to None to not constrain at all.
originaltext(cls='original')

Alias for retrieving the original uncorrect text.

A call to text() with correctionhandling=CorrectionHandling.ORIGINAL

classmethod parsexml(node, doc, **kwargs)

Internal class method used for turning an XML element into an instance of the Class.

Parameters:
  • node - XML Element (*) –
  • doc - Document (*) –
Returns:

An instance of the current Class.

phon(cls='current', previousdelimiter='', strict=False, correctionhandling=1)

Get the phonetic representation associated with this element (of the specified class)

The phonetic content will be constructed from child-elements whereever possible, as they are more specific. If no phonetic content can be obtained from the children and the element has itself phonetic content associated with it, then that will be used.

Parameters:
  • cls (str) – The class of the phonetic content to obtain, defaults to current.
  • retaintokenisation (bool) – If set, the space attribute on words will be ignored, otherwise it will be adhered to and phonetic content will be detokenised as much as possible. Defaults to False.
  • previousdelimiter (str) – Can be set to a delimiter that was last outputed, useful when chaining calls to phon(). Defaults to an empty string.
  • strict (bool) – Set this if you are strictly interested in the phonetic content explicitly associated with the element, without recursing into children. Defaults to False.
  • correctionhandling – Specifies what phonetic content to retrieve when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current phonetic content. You can set this to CorrectionHandling.ORIGINAL if you want the phonetic content prior to correction, and CorrectionHandling.EITHER if you don’t care.

Example:

word.phon()
Returns:The phonetic content of the element (unicode instance in Python 2, str in Python 3)
Raises:NoSuchPhon – if no phonetic conent is found at all.

See also

phoncontent(): Retrieves the phonetic content as an element rather than a string text() textcontent()

phoncontent(cls='current', correctionhandling=1)

Get the phonetic content explicitly associated with this element (of the specified class).

Unlike phon(), this method does not recurse into child elements (with the sole exception of the Correction/New element), and it returns the PhonContent instance rather than the actual text!

Parameters:
  • cls (str) – The class of the phonetic content to obtain, defaults to current.
  • correctionhandling – Specifies what content to retrieve when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current content. You can set this to CorrectionHandling.ORIGINAL if you want the content prior to correction, and CorrectionHandling.EITHER if you don’t care.
Returns:

The phonetic content (PhonContent)

Raises:

NoSuchPhon if there is no phonetic content for the element

postappend()

This method will be called after an element is added to another and does some checks.

It can do extra checks and if necessary raise exceptions to prevent addition. By default makes sure the right document is associated.

This method is mostly for internal use.

previous(Class=True, scope=True)

Returns the previous element, if it is of the specified type and if it does not cross the boundary of the defined scope. Returns None if no next element is found. Non-authoritative elements are never returned.

Parameters:
  • Class (*) – The class to select; any python class subclassed off ‘AbstractElement`. Set to True to constrain to the same class as that of the current instance, set to None to not constrain at all
  • scope (*) – A list of classes which are never crossed looking for a next element. Set to True to constrain to a default list of structure elements (Sentence,Paragraph,Division,Event, ListItem,Caption), set to None to not constrain at all.
classmethod relaxng(includechildren=True, extraattribs=None, extraelements=None, origclass=None)

Returns a RelaxNG definition for this element (as an XML element (lxml.etree) rather than a string)

remove(child)

Removes the child element

replace(child, *args, **kwargs)

Appends a child element like append(), but replaces any existing child element of the same type and set. If no such child element exists, this will act the same as append()

Keyword Arguments:
 
  • alternative (bool) – If set to True, the replaced element will be made into an alternative. Simply use AbstractElement.append() if you want the added element
  • be an alternative. (to) –

See AbstractElement.append() for more information and all parameters.

resolveword(id)
rightcontext(size, placeholder=None, scope=None)

Returns the right context for an element, as a list. This method crosses sentence/paragraph boundaries by default, which can be restricted by setting scope

select(Class, set=None, recursive=True, ignore=True, node=None)

Select child elements of the specified class.

A further restriction can be made based on set.

Parameters:
  • Class (class) – The class to select; any python class (not instance) subclassed off AbstractElement
  • Set (str) – The set to match against, only elements pertaining to this set will be returned. If set to None (default), all elements regardless of set will be returned.
  • recursive (bool) – Select recursively? Descending into child elements? Defaults to True.
  • ignore – A list of Classes to ignore, if set to True instead of a list, all non-authoritative elements will be skipped (this is the default behaviour and corresponds to the following elements: Alternative, AlternativeLayer, Suggestion, and folia.Original. These elements and those contained within are never authorative. You may also include the boolean True as a member of a list, if you want to skip additional tags along the predefined non-authoritative ones.
  • node (*) – Reserved for internal usage, used in recursion.
Yields:

Elements (instances derived from AbstractElement)

Example:

for sense in text.select(folia.Sense, 'cornetto', True, [folia.Original, folia.Suggestion, folia.Alternative] ):
    ..
setdoc(newdoc)

Set a different document. Usually no need to call this directly, invoked implicitly by copy()

setdocument(doc)

Associate a document with this element.

Parameters:doc (Document) – A document

Each element must be associated with a FoLiA document.

setparents()

Correct all parent relations for elements within the scop. There is sually no need to call this directly, invoked implicitly by copy()

settext(text, cls='current')

Set the text for this element.

Parameters:
  • text (str) – The text
  • cls (str) – The class of the text, defaults to current (leave this unless you know what you are doing). There may be only one text content element of each class associated with the element.
speech_speaker()

Retrieves the speaker of the audio or video file associated with the element.

The source is inherited from ancestor elements if none is specified. For this reason, always use this method rather than access the src attribute directly.

Returns:str or None if not found
speech_src()

Retrieves the URL/filename of the audio or video file associated with the element.

The source is inherited from ancestor elements if none is specified. For this reason, always use this method rather than access the src attribute directly.

Returns:str or None if not found
stricttext(cls='current')

Alias for text() with strict=True

text(cls='current', retaintokenisation=False, previousdelimiter='', strict=False, correctionhandling=1, normalize_spaces=False)

Get the text associated with this element (of the specified class)

The text will be constructed from child-elements whereever possible, as they are more specific. If no text can be obtained from the children and the element has itself text associated with it, then that will be used.

Parameters:
  • cls (str) – The class of the text content to obtain, defaults to current.
  • retaintokenisation (bool) – If set, the space attribute on words will be ignored, otherwise it will be adhered to and text will be detokenised as much as possible. Defaults to False.
  • previousdelimiter (str) – Can be set to a delimiter that was last outputed, useful when chaining calls to text(). Defaults to an empty string.
  • strict (bool) – Set this iif you are strictly interested in the text explicitly associated with the element, without recursing into children. Defaults to False.
  • correctionhandling – Specifies what text to retrieve when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current text. You can set this to CorrectionHandling.ORIGINAL if you want the text prior to correction, and CorrectionHandling.EITHER if you don’t care.
  • normalize_spaces (bool) – Return the text with multiple spaces, linebreaks, tabs normalized to single spaces

Example:

word.text()
Returns:The text of the element (unicode instance in Python 2, str in Python 3)
Raises:NoSuchText – if no text is found at all.
textcontent(cls='current', correctionhandling=1)

Get the text content explicitly associated with this element (of the specified class).

Unlike text(), this method does not recurse into child elements (with the sole exception of the Correction/New element), and it returns the TextContent instance rather than the actual text!

Parameters:
  • cls (str) – The class of the text content to obtain, defaults to current.
  • correctionhandling – Specifies what content to retrieve when corrections are encountered. The default is CorrectionHandling.CURRENT, which will retrieve the corrected/current content. You can set this to CorrectionHandling.ORIGINAL if you want the content prior to correction, and CorrectionHandling.EITHER if you don’t care.
Returns:

The phonetic content (TextContent)

Raises:

NoSuchText if there is no text content for the element

textvalidation(warnonly=None)

Run text validation on this element. Checks whether any text redundancy is consistent and whether offsets are valid.

Parameters:warnonly (bool) – Warn only (True) or raise exceptions (False). If set to None then this value will be determined based on the document’s FoLiA version (Warn only before FoLiA v1.5)
Returns:bool
toktext(cls='current')

Alias for text() with retaintokenisation=True

updatetext()

Recompute textual value based on the text content of the children. Only supported on elements that are a TEXTCONTAINER

xml(attribs=None, elements=None, skipchildren=False)

Serialises the FoLiA element and all its contents to XML.

Arguments are mostly for internal use.

Returns:an lxml.etree.Element

See also

AbstractElement.xmlstring() - for direct string output

xmlstring(pretty_print=False)

Serialises this FoLiA element and all its contents to XML.

Returns:a string with XML representation for this element and all its children
Return type:str
__iter__()

Iterate over all children of this element.

Example:

for annotation in word:
    ...
__len__()

Returns the number of child elements under the current element.

__str__()

Alias for text()