Now I will introduce a way to compress a text. When we are confronted with numerous data, and the data has a similar structure, we can take advantage of the feature to improve the performance of compression. In most of times, we could take the method to compress a text as its feature of data structure.

  we classify the method named dictionary method into two categories. One is static dictionary method, and the other is auto or dynamic dictionary method.

Now I plan to describe the first shortly with a routine example.

  if we have much information about a structure of a text , it is available to take the static dictionary method. We could use many ways to implement the method varying with occasions, but a way named double letters code is popular with programmers.

  To make it clearer, I prefer to take a simple example to explain the method, as follows.

  Now there is a signal composed by five letters, that is 'a', 'b', 'c', 'd' and 'r'. Then we get a dictionary accroding to our signal knowledge. The dictionary is

code letter
000 a
001 b
010 c
011 d
100 r
101 ab
110 ac
111 ad

  Then I will code a sequence that is 'abracadabra'.

  At first, the coder will read the first of two letters, which are 'ab'. After that, the coder have to find if the pair of letters is in our dictionary. If it does,  the coder will return the letters's code and read the next letters. otherwise it will return the first letter's code and read the following letter. In this example, the coder will find the code in the dictionary, and return '101'. Following the step, the coder reads 'ra', but it cann't find the value of our dictionary by key 'ra'. So it have to return the code of 'r' that is '100', and read the letter 'c' following 'a' to compose of a new pair of letters  that is 'ac'. The coder return '110'. Then read 'ad', return '110'. ...

  The output is '101100110111101100000'.

  The routine written by python is as follows.  

 def getCodeDict():
codeDict = {}
codeDict['a'] = ''
codeDict['b'] = ''
codeDict['c'] = ''
codeDict['d'] = ''
codeDict['r'] = ''
codeDict['ab'] = ''
codeDict['ac'] = ''
codeDict['ad'] = ''
return codeDict def compress(code):
print('start to compress')
result = ''
codeDict = getCodeDict()
offset = 2
unCodedCode = code
while unCodedCode != '':
targetCode = unCodedCode[0 : 2]
if targetCode in codeDict:
#find a pair of letters, and move two steps
result = result + codeDict[targetCode]
offset = 2
else :
#not find a pair of letters, and move only one step
result = result + codeDict[targetCode[0]]
offset = 1
unCodedCode = unCodedCode[offset : ]
print('complete to compress')
return result if __name__=='__main__':
signals = 'abracadabra'
result = compress(signals)
print(result)

static dictionary methods of text compression的更多相关文章

  1. Effective Java 01 Consider static factory methods instead of constructors

    Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Ra ...

  2. public static void speckOnWin7(string text),在win7中读文字

    public static void speckOnWin7(string text) {    //洪丰写的,转载请注明 try { string lsSource = ""; ...

  3. Effective Java - Item 1: Consider static factory methods instead of constructors

    考虑使用静态工厂方法来替代构造方法, 这样的做的好处有四点. 1. 更好的表意 有的构造方法实际上有特殊的含义, 使用静态工厂方法能更好的表达出他的意思. 例如 BigInteger(int, int ...

  4. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...

  5. Effective Java P2 Item1 Consider static factory methods instead of constructors

    获得一个类的实例的传统方法是公共的构造方法,还可以提供一个公共的静态工厂方法(一个返回值为该类实例的简单静态方法), 例如Boolean(boolean 的封装类) public static Boo ...

  6. C#学习笔记-数据的传递(公共变量)以及Dictionary

    看的代码越多,写的代码越多,就越是享受这些字符,终于渐渐懂得了那种传闻中的成就感,特别是自己从看不懂然后一步一步学,一个代码一个代码地敲,最后哪怕只是完成了一个小功能,也都是特别自豪的!这种自豪不用告 ...

  7. Dictionary<k,v>键值对的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dict ...

  8. Convert HTML to Text(转载)

    原文地址:http://www.blackbeltcoder.com/Articles/strings/convert-html-to-text  Download Source Code Intro ...

  9. C#将URL中的参数转换成字典Dictionary<string, string>

    /// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, ...

随机推荐

  1. Mac OS 10.12 - 如何关闭Rootless机制?

    一,进入恢复模式(Recovery):具体操作方法参见下面这篇博客: http://www.cnblogs.com/sunylat/p/6414697.html 二,关闭Rootless机制 1,选择 ...

  2. jzoj5906

    我們首先發現,每一條邊都至少走1次,因為我們必須走到每一個節點按按鈕 如果我們不走一個節點,說明這個節點已經有傳送門了,但是必須走到這個節點開傳送門,矛盾 然後我們發現,每一條邊至多經過2次 如果我們 ...

  3. robot_framework Authorization 解决登录超时问题(token)

    写rf的接口时,遇到总是报错提示: 登录超时 解决过程: 1 . 通过对同一个接口进行手机抓包对比,发现该接口请求时,多了Authorization,需要HTTP Basic Authenticati ...

  4. flask-mysqldb安装时EnvironmentError: mysql_config not found

    安装时候的日志如下: sh: : mysql_config: not found Traceback (most recent call last): File , in <module> ...

  5. 【8】JMicro微服务-JMicro ZKUI

    ZKUI是一个开源项目,是一个查看,修改ZK数据非常方便的工具.JMicro基于ZK做服务治理,配置管理,因此使用ZKUI会提供非常大的方便. Github地址:https://github.com/ ...

  6. POJ 1006

    #include<iostream>#include<stdio.h>using namespace std;int main(){    //freopen("ac ...

  7. Seqlist L 与 Seqlist *L的区别

     Seqlist L结构体变量,SeqlistInsert之后不可以带回新的插入数据.  Seqlist *L 是传结构体指针 用SeqlistInsert之后可以有新的插入.

  8. 自己搞了20万张图片100个分类,tensorflow训练23万次后。。。。。。

    自己搞了20万张图片100个分类,tensorflow训练23万次后...... 我自己把训练用的一张图片,弄乱之后做了一个预测 100个汉字,20多万张图片,tensorflow CNN训练23万次 ...

  9. pointer-events/H5页面在iphone6 plus的微信上出现闪退

    一.pointer-events 1.元素加上pointer-events:none后,在js中加点击事件不好使 原因:pointer-events:none关闭所有点击事件,包括js总的 解决:删掉 ...

  10. 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式

    1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...