Вот псевдокод для сжатия Лемпеля-Зива-Уэлча.
pattern = get input character
while ( not end-of-file ) {
K = get input character
if ( <<pattern, K>> is NOT in
the string table ){
output the code for pattern
add <<pattern, K>> to the string table
pattern = K
}
else { pattern = <<pattern, K>> }
}
output the code for pattern
output EOF_CODE
Я пытаюсь закодировать это в Lua, но на самом деле это не работает. Вот код, который я смоделировал на основе функции LZW в Python, но я получаю сообщение об ошибке «попытка вызвать строковое значение» в строке 8.
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for c in uncompressed do
-- while c is in the function compress
local wc = w + c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
-- Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
-- Output the code for w.
if w then
dictionary[w] = ""
end
end
return dictionary
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print (compressed)
Мне бы очень хотелось, чтобы кто-нибудь помог мне запустить мой код или помог мне закодировать сжатие LZW в Lua. Большое спасибо!