这是 Lempel-Ziv-Welch 压缩的伪代码。
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 中对此进行编码,但它并没有真正起作用。这是我在 Python 中以 LZW 函数为模型的代码,但在第 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)
我真的很想得到一些帮助,或者让我的代码运行,或者帮助我在 Lua 中编写 LZW 压缩。非常感谢!