nltk.corpus.reader.xmldocs module¶
Corpus reader for corpora whose documents are xml files.
(note – not named ‘xml’ to avoid conflicting w/ standard xml package)
- class nltk.corpus.reader.xmldocs.XMLCorpusReader[source]¶
Bases:
CorpusReader
Corpus reader for corpora whose documents are xml files.
Note that the
XMLCorpusReader
constructor does not take anencoding
argument, because the unicode encoding is specified by the XML files themselves. See the XML specs for more info.- __init__(root, fileids, wrap_etree=False)[source]¶
- Parameters
root (PathPointer or str) – A path pointer identifying the root directory for this corpus. If a string is specified, then it will be converted to a
PathPointer
automatically.fileids – A list of the files that make up this corpus. This list can either be specified explicitly, as a list of strings; or implicitly, as a regular expression over file paths. The absolute path for each file will be constructed by joining the reader’s root to each file name.
encoding –
The default unicode encoding for the files that make up the corpus. The value of
encoding
can be any of the following:A string:
encoding
is the encoding name for all files.A dictionary:
encoding[file_id]
is the encoding name for the file whose identifier isfile_id
. Iffile_id
is not inencoding
, then the file contents will be processed using non-unicode byte strings.A list:
encoding
should be a list of(regexp, encoding)
tuples. The encoding for a file whose identifier isfile_id
will be theencoding
value for the first tuple whoseregexp
matches thefile_id
. If no tuple’sregexp
matches thefile_id
, the file contents will be processed using non-unicode byte strings.None: the file contents of all files will be processed using non-unicode byte strings.
tagset – The name of the tagset used by this corpus, to be used for normalizing or converting the POS tags returned by the
tagged_...()
methods.
- words(fileid=None)[source]¶
Returns all of the words and punctuation symbols in the specified file that were in text nodes – ie, tags are ignored. Like the xml() method, fileid can only specify one file.
- Returns
the given file’s text nodes as a list of words and punctuation symbols
- Return type
list(str)
- class nltk.corpus.reader.xmldocs.XMLCorpusView[source]¶
Bases:
StreamBackedCorpusView
A corpus view that selects out specified elements from an XML file, and provides a flat list-like interface for accessing them. (Note:
XMLCorpusView
is not used byXMLCorpusReader
itself, but may be used by subclasses ofXMLCorpusReader
.)Every XML corpus view has a “tag specification”, indicating what XML elements should be included in the view; and each (non-nested) element that matches this specification corresponds to one item in the view. Tag specifications are regular expressions over tag paths, where a tag path is a list of element tag names, separated by ‘/’, indicating the ancestry of the element. Some examples:
'foo'
: A top-level element whose tag isfoo
.'foo/bar'
: An element whose tag isbar
and whose parent is a top-level element whose tag isfoo
.'.*/foo'
: An element whose tag isfoo
, appearing anywhere in the xml tree.'.*/(foo|bar)'
: An wlement whose tag isfoo
orbar
, appearing anywhere in the xml tree.
The view items are generated from the selected XML elements via the method
handle_elt()
. By default, this method returns the element as-is (i.e., as an ElementTree object); but it can be overridden, either via subclassing or via theelt_handler
constructor parameter.- __init__(fileid, tagspec, elt_handler=None)[source]¶
Create a new corpus view based on a specified XML file.
Note that the
XMLCorpusView
constructor does not take anencoding
argument, because the unicode encoding is specified by the XML files themselves.- Parameters
tagspec (str) – A tag specification, indicating what XML elements should be included in the view. Each non-nested element that matches this specification corresponds to one item in the view.
elt_handler –
A function used to transform each element to a value for the view. If no handler is specified, then
self.handle_elt()
is called, which returns the element as an ElementTree object. The signature of elt_handler is:elt_handler(elt, tagspec) -> value
- handle_elt(elt, context)[source]¶
Convert an element into an appropriate value for inclusion in the view. Unless overridden by a subclass or by the
elt_handler
constructor argument, this method simply returnselt
.- Returns
The view value corresponding to
elt
.- Parameters
elt (ElementTree) – The element that should be converted.
context (str) – A string composed of element tags separated by forward slashes, indicating the XML context of the given element. For example, the string
'foo/bar/baz'
indicates that the element is abaz
element whose parent is abar
element and whose grandparent is a top-levelfoo
element.