これが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圧縮をコーディングするのを手伝ってもらいたいです。どうもありがとう!