CS101 Lecture 13: Text Representation and Data Compression John Magee 15 July 2013 1 Overview/Questions – How do computers store text information? – Why do some characters show up as � s on my browser? – What is compression, and why is it important? – How can data be compressed? 2 1
Binary Representations Recall: a single bit can be either a 0 or a 1 What if you need to represent more than 2 choices? n bits can represent 2 n possible combinations 3 Representing Text There are finite number of characters to represent, so list them all and assign each a binary pattern. Character set A list of characters and the binary codes used to represent each one. Computer manufacturers agreed to standardize in the early 1960s. 4 2
The ASCII Character Set ASCII stands for American Standard Code for Information Interchange ASCII originally used seven bits to represent each character, allowing for 128 unique characters Later extended ASCII evolved so that all eight bits were used. 5 The ASCII Character Set (7 bits) 6 3
The Extended ASCII Character Set 7 Can't You Take a Joke? :-) Carnegie Mellon professor Scott E. Fahlman Proposed ASCII emoticons, Sept. 19, 1982. Source: http://www.wired.com/science/discoveries/news/2008/09/dayintech_0919 8 4
ASCII Art Text-based systems had no graphics… …so people created art and graphics out of ASCII text! 9 The Unicode Character Set Extended ASCII is not enough for international use. Unicode uses 16 bits per character How many characters can UNICODE represent? Unicode is a superset of ASCII. The first 256 characters correspond exactly to the extended ASCII character set 10 5
The Unicode Character Set 11 Recall: Morse Code Invented by Samuel Morse for the telegraph in 1840s 12 6
Text Compression Problem: Assigning 16 bits to each character in a document uses a heck of a lot of space. We need ways to store and transmit text efficiently. Why? Common compression techniques: keyword encoding run-length encoding Huffman encoding 13 Keyword Encoding Replace frequently used words with a single character 14 7
Keyword Encoding Example Given the following paragraph, We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. 15 Keyword Encoding Example The encoded paragraph is We hold # truths to be self-evident, $ all men are created equal, $ ~y are endowed by ~ir Creator with certain unalienable Rights, $ among # are Life, Liberty + ~ pursuit of Happiness. — $ to secure # rights, Governments are instituted among Men, deriving ~ir just powers from ~ consent of ~ governed, — $ whenever any Form of Government becomes destructive of # ends, it is ~ Right of ~ People to alter or to abolish it, + to institute new Government, laying its foundation on such principles + organizing its powers in such form, ^ to ~m shall seem most likely to effect ~ir Safety + Happiness. 16 8
Keyword Encoding Compression ratio The size of the compressed data divided by the size of the original data (0 < c.r. <= 1) What did we save? Original paragraph: 656 characters Encoded paragraph: 596 characters Characters saved: 60 characters Compression ratio: 596/656 = 0.9085 Could we use this substitution chart for all text? 17 Run-Length Encoding Consider a single character which is repeated over and over again in a long sequence. Replace a repeated sequence with – a flag character – repeated character – number of repetitions Example: *n8 – * is the flag character – n is the repeated character – 8 is the number of times n is repeated 18 9
Run-Length Encoding Example Original text bbbbbbbbjjjkllqqqqqq+++++ Encoded text *b8jjjkll*q6*+5 (Why isn't l encoded? J?) The compression ratio is 15/25 or .6 Encoded text *x4*p4l*k7 Original text xxxxpppplkkkkkkk This type of repetition isn’t very helpful for English text; can you think of a situation where it might be helpful? 19 Huffman Encoding Why should the character “X" and "z" take up the same number of bits as "e" or " "? Huffman codes use variable-length bit strings to represent each character. More frequently used letters have shorter strings to represent them. 20 10
Huffman Encoding Example ballboard would be 1010001001001010110001111011 compression ratio is 28/72 or 0.39 as compared to ASCII Try to encode roadbed 21 Huffman Encoding Prefix Property No character's bit string is the prefix of any other character's bit string. To decode look for match left to right, bit by bit record letter when a match is found begin next character where you left off 22 11
Huffman Encoding Example Try it! Decode 1011111001010 23 Huffman Encoding The technique for creating codes guarantees the prefix property of the codes. There is no single “Huffman code” -- each depends on the application. Two types of Huffman codes: – general, based on use of letters in English, Spanish, …. – specialized, based on text itself or specific types of text 24 12
Take-Away Points – Character Sets, ASCII, Unicode – Data Compression – Key encoding – Run-length encoding – Huffman encoding 25 Student To Dos – Readings: Reed ch 5, pp 83-95 – HW03 (HTML) due Monday 11:59pm. – HW04 (Alice) due Thursday 11:59pm. – HW05 (Networking) to be posted. Practice, Practice, Practice! 26 13
Recommend
More recommend