Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,59 @@
|
|||
require "table2"
|
||||
|
||||
$define STX = "\x02"
|
||||
$define ETX = "\x03"
|
||||
|
||||
local function bwt(s)
|
||||
if s:find(STX, 1, true) or s:find(ETX, 1, true) then return nil end
|
||||
s = STX .. s .. ETX
|
||||
local len = #s
|
||||
local tbl = table.rep(len, "")
|
||||
tbl[1] = s
|
||||
for i = 2, len do tbl[i] = s:sub(i) .. s:sub(1, i - 1) end
|
||||
tbl:sort()
|
||||
local last_chars = table.rep(len, "")
|
||||
for i = 1, len do last_chars[i] = tbl[i][len] end
|
||||
return last_chars:concat()
|
||||
end
|
||||
|
||||
local function ibwt(r)
|
||||
local len = #r
|
||||
if len == 0 then return "" end
|
||||
local tbl = table.rep(len, "")
|
||||
for i = 1, len do
|
||||
for j = 1, len do tbl[j] = r[j].. tbl[j] end
|
||||
tbl:sort()
|
||||
end
|
||||
for tbl as row do
|
||||
if row:endswith(ETX) then return row:sub(2, -2) end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
local function make_printable(s)
|
||||
-- Substitute ^ for STX and | for ETX to print results.
|
||||
s = s:replace(STX, "^")
|
||||
return s:replace(ETX, "|")
|
||||
end
|
||||
|
||||
local tests = {
|
||||
"banana",
|
||||
"appellee",
|
||||
"dogwood",
|
||||
"TO BE OR NOT TO BE OR WANT TO BE OR NOT?",
|
||||
"SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES",
|
||||
"\x02ABC\x03"
|
||||
}
|
||||
for tests as test do
|
||||
print(make_printable(test))
|
||||
io.write(" --> ")
|
||||
local t = bwt(test)
|
||||
if !t then
|
||||
print("ERROR: String can't contain STX or ETX")
|
||||
t = ""
|
||||
else
|
||||
print(make_printable(t))
|
||||
end
|
||||
local r = ibwt(t)
|
||||
print($" --> {r}\n")
|
||||
end
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Burrows–Wheeler transform"
|
||||
file: %Burrows–Wheeler_transform.r3
|
||||
url: https://rosettacode.org/wiki/Burrows–Wheeler_transform
|
||||
]
|
||||
|
||||
bwt: function [
|
||||
"Burrows–Wheeler Transform"
|
||||
input [string!] "The original text to encode"
|
||||
][
|
||||
;; Add sentinel markers:
|
||||
;; STX (^(02)) at the front to mark the start
|
||||
;; ETX (^(03)) at the end to mark the terminus
|
||||
;; These ensure unambiguous reconstruction and a fixed sort position.
|
||||
input: rejoin [#"^(02)" input #"^(03)"]
|
||||
len: length? input ;; Length of the sentinel-marked string
|
||||
rotations: clear [] ;; Will hold all cyclic rotations of the string
|
||||
;; Generate all cyclic rotations:
|
||||
repeat i len [
|
||||
append rotations rejoin [
|
||||
copy at input i ;; from position i to end
|
||||
copy/part input i - 1 ;; then from start up to (i-1)
|
||||
]
|
||||
]
|
||||
;; Sort all rotations lexicographically
|
||||
rotations: sort rotations
|
||||
transformed: copy "" ;; This will be the BWT output (last column)
|
||||
;; Take the last character from each sorted rotation and accumulate
|
||||
foreach r rotations [
|
||||
append transformed last r
|
||||
]
|
||||
;; Return the "last column" as the BWT-transformed string
|
||||
transformed
|
||||
]
|
||||
|
||||
|
||||
ibwt: function [
|
||||
"Inverse Burrows–Wheeler Transform"
|
||||
input [string!] "The BWT-produced last column string"
|
||||
][
|
||||
;; Make a copy so we preserve the original argument
|
||||
input: copy input
|
||||
len: length? input ;; Number of rows/characters
|
||||
;; Initialise a table (block of strings), initially containing `len` empty strings
|
||||
table: make block! len
|
||||
loop len [append table copy ""]
|
||||
;; Rebuild rotation table iteratively:
|
||||
;; Each iteration:
|
||||
;; 1. Prepend each character of last column to corresponding row
|
||||
;; 2. Sort the rows
|
||||
repeat j len [
|
||||
repeat i len [
|
||||
insert table/:i input/:i ;; insert char from last column at row start
|
||||
]
|
||||
table: sort table ;; keep table lexicographically sorted
|
||||
]
|
||||
;; After len iterations, table contains all sorted rotations.
|
||||
;; The original rotation is the one starting with STX and ending with ETX.
|
||||
;; With STX as the smallest char, it will always be the first row (table/1).
|
||||
if table/1/1 == #"^(02)" [
|
||||
;; Skip STX, copy the rest minus ETX.
|
||||
;; `next table/1` skips first char (STX)
|
||||
;; `len - 2` excludes both sentinels
|
||||
copy/part next table/1 len - 2
|
||||
]
|
||||
]
|
||||
|
||||
; Example usage
|
||||
foreach text [
|
||||
"banana"
|
||||
"appellee"
|
||||
"abracadabra"
|
||||
"TO BE OR NOT TO BE OR WANT TO BE OR NOT?"
|
||||
"SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES"
|
||||
][
|
||||
print ["Original text: " mold text]
|
||||
print ["After transformation: " mold a: bwt text]
|
||||
print ["After inverse transformation:" mold b: ibwt a lf]
|
||||
assert [b == text]
|
||||
]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Apply Burrows–Wheeler transform
|
||||
|
||||
Bwt ← ⌅(⊣⍉⍆≡⌟↻⇡⊸⧻$"\x02_\x03"|↘₁↘₋₁⊢⊙◌⍥(⍆⊸≡˜⊂)⤙˜↯""⊸⧻)
|
||||
{"banana" "appellee" "abracadabra" "Unusual Uiua"}
|
||||
≡(&p$"_ > _ > _"⊸°Bwt⊸Bwt)
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
const tests = [
|
||||
"banana",
|
||||
"appellee",
|
||||
"dogwood",
|
||||
"TO BE OR NOT TO BE OR WANT TO BE OR NOT?",
|
||||
"SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES",
|
||||
"\x02ABC\x03"
|
||||
]
|
||||
|
||||
struct BWT {
|
||||
mut:
|
||||
stx string = "\x02"
|
||||
etx string = "\x03"
|
||||
}
|
||||
|
||||
fn (bwt BWT) bwt(sg string) !string {
|
||||
if sg.contains(bwt.stx) || sg.contains(bwt.etx) {
|
||||
return error("FormatException: String cannot contain STX or ETX")
|
||||
}
|
||||
ss := bwt.stx + sg + bwt.etx
|
||||
mut table := []string{}
|
||||
for ial in 0 .. ss.len {
|
||||
before := ss[ial..]
|
||||
after := ss[..ial]
|
||||
table << before + after
|
||||
}
|
||||
table.sort()
|
||||
return table.map(it[it.len - 1].ascii_str()).join("")
|
||||
}
|
||||
|
||||
fn (bwt BWT) ibwt(r string) string {
|
||||
len := r.len
|
||||
mut table := []string{len: len, init: ""}
|
||||
for _ in 0 .. len {
|
||||
for ial in 0 .. len {
|
||||
table[ial] = r[ial].ascii_str() + table[ial]
|
||||
}
|
||||
table.sort()
|
||||
}
|
||||
for row in table {
|
||||
if row.ends_with(bwt.etx) { return row[1..len - 1] }
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fn (bwt BWT) make_printable(sg string) string {
|
||||
return sg.replace(bwt.stx, "^").replace(bwt.etx, "|")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut bwt_inst := BWT{}
|
||||
mut tsg := ""
|
||||
for idx, test in tests {
|
||||
println(bwt_inst.make_printable(test))
|
||||
print(" --> ")
|
||||
tsg = bwt_inst.bwt(test) or {
|
||||
println("ERROR: {$err}")
|
||||
tsg = ""
|
||||
continue
|
||||
}
|
||||
println(bwt_inst.make_printable(tsg))
|
||||
rsg := bwt_inst.ibwt(tsg)
|
||||
if idx < tests.len { println(" --> $rsg\n") } else { println(" --> $rsg") }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue