- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
注意:这不是 LZW 压缩的正确用法。我只是在玩弄它。
在一次传递中,是否可以同时更新字典中元素的频率计数?
import sys
from collections import defaultdict
import re
# The silliest string!
inputString = "this is the first sentence in this book the first sentence is really the most interesting the first sentence is always first"
inputString = inputString.lower().split()
StringTable = defaultdict(int)
FreqTable = defaultdict(int)
def DoPass():
global inputString
global StringTable
global FreqTable
print ""
print "INPUT STRING:"
print inputString
CODE = 256
STRING = inputString[0]
output = []
StringTable[STRING] = CODE
CODE += 1
total = len(inputString)
for i in range(1, total):
WORD = inputString[i]
if STRING + " " + WORD in StringTable:
STRING += " " + WORD
else:
if STRING in StringTable:
output.append(str(StringTable[STRING]))
else:
output.append(STRING)
StringTable[STRING + " " + WORD] = CODE
CODE += 1
STRING = WORD
StringTable[STRING] = CODE
CODE += 1
output.append(str(StringTable[STRING]))
print ""
print "OUTPUT STRING:"
print output
print ""
print "Dictionary Built..."
for i in sorted(StringTable.keys(), key=lambda x: len(x)):
print i, StringTable[i]
print ""
print "Frequencies:"
for i in sorted(FreqTable.keys(), key=lambda x: len(x)):
print i, FreqTable[i]
def main():
DoPass()
if __name__ == "__main__":
main()
INPUT STRING:
['this', 'is', 'the', 'first', 'sentence', 'in', 'this', 'book', 'the', 'first', 'sentence', 'is', 'really', 'the', 'most', 'interesting', 'the', 'first', 'sent
ence', 'is', 'always', 'first']
OUTPUT STRING:
['256', 'is', 'the', 'first', 'sentence', 'in', '256', 'book', '259', 'sentence', 'is', 'really', 'the', 'most', 'interesting', '265', 'is', 'always', '275']
Dictionary Built...
this 256
first 275
is the 258
in this 262
this is 257
book the 264
the most 269
this book 263
is always 273
is really 267
the first 259
really the 268
sentence in 261
sentence is 266
always first 274
first sentence 260
interesting the 271
most interesting 270
the first sentence 265
the first sentence is 272
Frequencies:
#### I am trying to fill this
我想用它找到的任何模式的频率计数填充 FreqTable
。出于明显的原因,我没有将我的方法放在这里 - 它不起作用并且它给了我错误的计数。关于这是否可能的任何建议都会很棒。
最佳答案
不确定是否理解您的问题。如果您只需要频率表,那么这应该很简单:每次找到一个模式时,您只需将 +1 添加到它的频率计数。所以真正的问题应该是找到规律。
如果你想保持模式排序,这也应该很容易,因为你一直保持表格排序,它最终是一个插入排序操作,这是非常快的。
现在,找到正确的模式是另一回事。你需要一棵树,或者一个哈希表,然后是树,或者列表,或者其他什么,来找到你最好的匹配序列。这就是使此类算法执行起来更加复杂的原因。
显然,对于非常小的数据集,“简单”搜索(一项一项地测试所有条目)可能会给出一些结果。但随着数据集的扩大,搜索成本将变得令人望而却步。
关于python - 在 LZW 中压缩时计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8042332/
LZW 压缩算法在压缩后增加了位大小: 这是压缩函数的代码: // compression void compress(FILE *inputFile, FILE *outputFile) {
我正在寻找可以压缩和解压缩word文档的c#中的LZW压缩算法。我在谷歌上搜索过它,但它没有给我我需要的答案。任何人都可以帮助我获得它的代码并让我了解如何在我的项目中真正实现 LZW。 最佳答案 有一
我试图让这段代码正常工作,但当我尝试编码时,它似乎无法正常工作。我有一个 60 字节的文本文件。我对其进行编码,输出文件为 100 字节。当我解码该文件时,它变成了 65 字节。它解码正确,但文件大小
我很难理解 LZW 算法。我正在检查维基百科 (http://en.wikipedia.org/w/index.php?title=Lempel-Ziv-Welch&oldid=245292660)
好吧,我必须使用 LZW 算法制作一个 PPM 图像压缩器,我理解该算法和代码并实现了一个字符串版本(在 Java 中,用于测试)。 最大的问题是压缩,因为如果我有: 输入:ABCDABCDABCDA
我目前正在将 LZW 压缩和解压缩方法从 FFmpeg 源代码实现到我的项目。我偶然发现的是输出缓冲区的大小(将存储压缩数据的地方)需要大于我们要压缩的输入缓冲区的大小。这与压缩本身不矛盾吗? 下一部
我找到了 LZW 算法的实现,我想知道如何将其输出(一个 int 列表)表示为字节数组。 我曾尝试使用一个字节,但在长输入的情况下,字典有超过 256 个条目,因此我无法转换。 然后我尝试添加一个额外
我正在编写一个 C 库来将 SDL_Surfaces 导出为各种格式作为练习,到目前为止,我已经搞定了 BMP、TGA 和 PCX 格式。现在我正在制作 GIF格式,我觉得我已经非常接近让它发挥作用了
我正在尝试实现LZW压缩和解压缩技术。我的程序将任何文件作为输入流并将其读入字节数组。然后,它对其应用压缩算法,并在字符串变量中返回编码字节。 然后我应用解压缩算法返回原始数字。 现在为了检索原始文件
我有一个 LZW 算法 - private void start(int maxNumBits) throws IOException{ System.out.println("Beginni
谁能告诉我如何用 c 语言构建 LZW 压缩树?是不是像 结构树{ 短下一个[255]; } 最佳答案 LZW 的树更像是一本字典。每个条目由一个代码(索引)和一个字符组成。树在逻辑上用所有字符初始化
我希望更改 LZW 压缩器,使其能够在 LZW 编码文件中搜索单词并查找该搜索词的匹配数。例如,如果我的文件用作 Prompt:>lzw "searchterm" encoded_file.lzw 3
我做了一些研究,但没有什么真正与我的问题有关...... 我实际上正在尝试为学校编写 LZW 压缩代码,并且我需要一个函数来检查某个元素是否在我的字典中。 但是,当我调用这个函数时,它尝试访问我的字典
这里是函数本身。我在那里遇到了段错误,因为显然我无法将字符串分配给数组中的该值。 clang/gcc 都给我一个警告。 Clang 的好一点,它是“期望 char 分配 char *”。我不知道还有什
我有以下形式的字符串(大约 1-5Kb): FF,A3V,X7Y,aA4,.... lzw 非常好地压缩这些,但包含土耳其字符。然后将它们提交到 MySQL 数据库。 有时 MySQL 可能会“播放”
我正在编写一个通用的 LZW 解码器 c++ 程序,但我无法找到有关所用代码字长度(以位为单位)的文档。我发现一些文章说代码字长 12 位,而其他人说 16 位,还有一些文章说使用可变位长度。那是哪一
所以我正在尝试用 C++ 制作一个简单的文件压缩器/解压缩器(我的目标不是尽可能获得最好的结果)但我真的被卡住了,因为我充满了疑问所以他们在这里: 我应该使用哪种字典(我正在使用 map )?我应该存
CGPDFScanner 会解压缩它遇到的任何 LZW 压缩吗?还是我要为此负责? [我假设它确实如此,因为我正在使用大量的 Tj 运算符等] 最佳答案 是的,CGPDFScanner 将解压缩它找到
当我使用长度为 256 的符号表(字典)时,我的 LZW 压缩工作正常,编码器和解码器都使用 256 并且一切正常但是当我将这个数字增加到例如 512、1024、4096 时,解码文件输出是与第一个输
注意:这不是 LZW 压缩的正确用法。我只是在玩弄它。 问题 在一次传递中,是否可以同时更新字典中元素的频率计数? 我的实现 import sys from collections import de
我是一名优秀的程序员,十分优秀!