Spans and Trees
A small Python library for converting between XML trees and a span-based structure. This can be useful for extracting sections of text from XML documents and doing special things with some of the tags.
The three functions are tree_to_spans, spans_to_tree and spans_to_passages for converting between an ElementTree element and text with a list of spans. Examples are shown below.
Install
pip install spans-and-trees
tree_to_spans
First create a little example XML tree to convert.
import xml.etree.ElementTree as ET
xmlstring = "<doc><title>Important document</title><contents>Empty</contents></doc>"
root = ET.ElementTree(ET.fromstring(xmlstring)).getroot()
Then use the tree_to_spans function to convert the XML document into the text content with spans.
from spans_and_trees import tree_to_spans
text, spans = tree_to_spans(root)
print(text) # Important documentEmpty
print(spans) # [(0, 18, 'title', {}), (18, 5, 'contents', {})]
The format of the spans are a tuple of length 4. The element contents are:
- The start location of the span
- The length of the span
- The tag of the span
- A dictionary of the attributes of the span.
spans_to_tree
Now we create a dummy document with a block of text and a span at particular offset.
from spans_and_trees import spans_to_tree
text = 'The quick brown fox jumped over the lazy dog'
spans = [ (10,5,'colour',{'dummy_attrib':'5'}) ] # The span starts at 10, has length of 5, is a 'colour' tag and has a dummy attribute.
root = spans_to_tree(text, spans)
print(type(root)) # <class 'xml.etree.ElementTree.Element'>
We can check the XML tree that has been created:
xmlstr = ET.tostring(root)
print(xmlstr) # b'<tree>The quick <colour dummy_attrib="5">brown</colour> fox jumped over the lazy dog</tree>'
The root element's tag defaults to "tree", since spans don't record a tag for the whole document (tree_to_spans doesn't include one either). Pass root_tag to use something else, e.g. spans_to_tree(text, spans, root_tag="doc").
spans_to_passages
spans_to_passages takes the text/spans output of tree_to_spans and splits it into a list of text passages, one per split_tags element (e.g. p, sec), dropping the content of any ignore_tags element (e.g. table), and attaching any keep_tags spans (e.g. bold) that fall within each passage.
from spans_and_trees import tree_to_spans, spans_to_passages
xmlstring = """
<article>
<sec>
<title>Introduction</title>
<p>This is <bold>important</bold> background text.</p>
<table-wrap>Some table content we want to ignore.</table-wrap>
<p>A second paragraph follows.</p>
</sec>
</article>
"""
root = ET.ElementTree(ET.fromstring(xmlstring)).getroot()
text, spans = tree_to_spans(root)
passages = spans_to_passages(text, spans, ignore_tags={'table-wrap'}, split_tags={'title','p'}, keep_tags={'bold'})
for p in passages:
print(p)
# {'start': 5, 'end': 17, 'text': 'Introduction', 'spans': []}
# {'start': 20, 'end': 54, 'text': 'This is important background text.', 'spans': [(8, 9, 'bold', {})]}
# {'start': 97, 'end': 124, 'text': 'A second paragraph follows.', 'spans': []}
Each passage's start/end are offsets into the original text; each attached span's offsets are relative to the passage's own text.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spans_and_trees-0.3.1.tar.gz.
File metadata
- Download URL: spans_and_trees-0.3.1.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1a3370f6d9bf3bf4e0c002597f703dce196fd57a5f431595351786b0b25dc90
|
|
| MD5 |
869b2a123eabe4f6f30f246519a43651
|
|
| BLAKE2b-256 |
221c27c8115a2ea0590583b9d3ffa818ef9cdd7666fbba646a9ebab626589316
|
Provenance
The following attestation bundles were made for spans_and_trees-0.3.1.tar.gz:
Publisher:
publish.yml on jakelever/spans_and_trees
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spans_and_trees-0.3.1.tar.gz -
Subject digest:
a1a3370f6d9bf3bf4e0c002597f703dce196fd57a5f431595351786b0b25dc90 - Sigstore transparency entry: 2546458181
- Sigstore integration time:
-
Permalink:
jakelever/spans_and_trees@bec075414f75af851775aa4a4e0869b9a5d8a820 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/jakelever
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bec075414f75af851775aa4a4e0869b9a5d8a820 -
Trigger Event:
release
-
Statement type:
File details
Details for the file spans_and_trees-0.3.1-py3-none-any.whl.
File metadata
- Download URL: spans_and_trees-0.3.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68243f4a73a1d2414cb846e79fb9108abf71e1b549573f24d893a9895defebe4
|
|
| MD5 |
c72153ace43791a8dd2f9fde4a0707e3
|
|
| BLAKE2b-256 |
8cdc204e4498ff151b1ad498c6991ea43045536fde04736ac721cc637a86180a
|
Provenance
The following attestation bundles were made for spans_and_trees-0.3.1-py3-none-any.whl:
Publisher:
publish.yml on jakelever/spans_and_trees
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spans_and_trees-0.3.1-py3-none-any.whl -
Subject digest:
68243f4a73a1d2414cb846e79fb9108abf71e1b549573f24d893a9895defebe4 - Sigstore transparency entry: 2546458611
- Sigstore integration time:
-
Permalink:
jakelever/spans_and_trees@bec075414f75af851775aa4a4e0869b9a5d8a820 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/jakelever
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bec075414f75af851775aa4a4e0869b9a5d8a820 -
Trigger Event:
release
-
Statement type: