Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Burrows–Wheeler transform
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Sample implementation== This [[Python (programming language)|Python]] implementation sacrifices speed for simplicity: the program is short, but takes more than the linear time that would be desired in a practical implementation. It essentially does what the pseudocode section does. Using the [[C0 and C1 control codes#STX|STX/ETX control codes]] to mark the start and end of the text, and using <code>s[i:] + s[:i]</code> to construct the <code>i</code>th rotation of <code>s</code>, the forward transform takes the last character of each of the sorted rows: <syntaxhighlight lang="python"> from curses.ascii import STX, ETX def bwt(s: str, start=chr(STX), end=chr(ETX)) -> str: r""" Apply Burrows–Wheeler transform to input string. >>> bwt('BANANA') '\x03ANNB\x02AA' >>> bwt('BANANA', start='^', end='$') 'ANNB^AA$' >>> bwt('BANANA', start='%', end='$') 'A$NNB%AA' """ assert ( start not in s and end not in s ), "Input string cannot contain STX and ETX characters" s = f"{start}{s}{end}" # Add start and end of text marker # Table of rotations of string table = sorted(f"{s[i:]}{s[:i]}" for i, c in enumerate(s)) last_column = [row[-1:] for row in table] # Last characters of each row return "".join(last_column) # Convert list of characters into string </syntaxhighlight> The inverse transform repeatedly inserts <code>r</code> as the left column of the table and sorts the table. After the whole table is built, it returns the row that ends with ETX, minus the STX and ETX. <syntaxhighlight lang="python"> def inverse_bwt(r: str, start=chr(STX), end=chr(ETX)) -> str: r""" Apply inverse Burrows–Wheeler transform. >>> inverse_bwt('\x03ANNB\x02AA') 'BANANA' >>> inverse_bwt('ANNB^AA$', start='^', end='$') 'BANANA' >>> inverse_bwt('A$NNB%AA', start='%', end='$') 'BANANA' """ str_len = len(r) table = [""] * str_len # Make empty table for _ in range(str_len): table = sorted(rc + tc for rc, tc in zip(r, table)) # Add a column of r # Iterate over and check whether last character ends with ETX or not s = next((row for row in table if row.endswith(end)), "") # Retrieve data from array and get rid of start and end markers return s.rstrip(end).strip(start) </syntaxhighlight> Following implementation notes from Manzini, it is equivalent to use a simple [[null character]] suffix instead. The sorting should be done in [[colexicographic order]] (string read right-to-left), i.e. {{code|2=python|1=sorted(..., key=lambda s: s[::-1])}} in Python.<ref name=Manzini/> (The above control codes actually fail to satisfy EOF being the last character; the two codes are actually the ''first''. The rotation holds nevertheless.)
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Burrows–Wheeler transform
(section)
Add topic