527 lines
12 KiB
Plaintext
527 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"![Logo 1](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech1.jpg)\n",
|
|
"<div class=\"alert alert-block alert-info\">\n",
|
|
"<h1> Ekstrakcja informacji </h1>\n",
|
|
"<h2> 3. <i>Entropia</i> [ćwiczenia]</h2> \n",
|
|
"<h3> Jakub Pokrywka (2022)</h3>\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"![Logo 2](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech2.jpg)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Requirement already satisfied: dahuffman in /home/kuba/anaconda3/lib/python3.8/site-packages (0.4.1)\r\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!pip install dahuffman"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import random\n",
|
|
"from collections import Counter\n",
|
|
"from dahuffman import HuffmanCodec"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"NR_INDEKSU = 375985"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Wprowadzenie"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"tekst = 'Ala ma kota. Jarek ma psa'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"codec = HuffmanCodec.from_data(tekst)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Counter({'A': 1,\n",
|
|
" 'l': 1,\n",
|
|
" 'a': 6,\n",
|
|
" ' ': 5,\n",
|
|
" 'm': 2,\n",
|
|
" 'k': 2,\n",
|
|
" 'o': 1,\n",
|
|
" 't': 1,\n",
|
|
" '.': 1,\n",
|
|
" 'J': 1,\n",
|
|
" 'r': 1,\n",
|
|
" 'e': 1,\n",
|
|
" 'p': 1,\n",
|
|
" 's': 1})"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"Counter(tekst)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Bits Code Value Symbol\n",
|
|
" 2 00 0 ' '\n",
|
|
" 2 01 1 'a'\n",
|
|
" 4 1000 8 't'\n",
|
|
" 5 10010 18 _EOF\n",
|
|
" 5 10011 19 '.'\n",
|
|
" 5 10100 20 'A'\n",
|
|
" 5 10101 21 'J'\n",
|
|
" 5 10110 22 'e'\n",
|
|
" 5 10111 23 'l'\n",
|
|
" 4 1100 12 'k'\n",
|
|
" 4 1101 13 'm'\n",
|
|
" 5 11100 28 'o'\n",
|
|
" 5 11101 29 'p'\n",
|
|
" 5 11110 30 'r'\n",
|
|
" 5 11111 31 's'\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"codec.print_code_table()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{' ': (2, 0),\n",
|
|
" 'a': (2, 1),\n",
|
|
" 't': (4, 8),\n",
|
|
" _EOF: (5, 18),\n",
|
|
" '.': (5, 19),\n",
|
|
" 'A': (5, 20),\n",
|
|
" 'J': (5, 21),\n",
|
|
" 'e': (5, 22),\n",
|
|
" 'l': (5, 23),\n",
|
|
" 'k': (4, 12),\n",
|
|
" 'm': (4, 13),\n",
|
|
" 'o': (5, 28),\n",
|
|
" 'p': (5, 29),\n",
|
|
" 'r': (5, 30),\n",
|
|
" 's': (5, 31)}"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"codec.get_code_table()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"encoded = codec.encode(tekst)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'1010010111010011010100110011100100001100110010101011111010110110000110101001110111111011'"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"\"{:08b}\".format(int(encoded.hex(),16))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"A l a"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"101001 10111 01"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Ala ma kota. Jarek ma psa'"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"codec.decode(encoded)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"25"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"len(tekst)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"11"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"len(encoded)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Zadanie 1 ( 15 punktów)\n",
|
|
"\n",
|
|
"Weź teksty:\n",
|
|
"- z poprzednich zajęć (lub dowolny inny) w języku naturalnym i obetnij do długości 100_000 znaków\n",
|
|
"- wygenerowany losowo zgodnie z rozkładem jednostajnym dyskretnym z klasy [a-zA-Z0-9 ] o długości 100_000 znaków\n",
|
|
"- wygenerowany losowo zgodnie z rozkładem geometrycznym (wybierz p między 0.2 a 0.8) z klasy [a-zA-Z0-9 ] o długości 100_000 znaków\n",
|
|
"- wygenerowany losowo zgodnie z rozkładem jednostajnym dwupunktowym p=0.5 z klasy [01] o długości 100_000 znaków\n",
|
|
"- wygenerowany losowo zgodnie z rozkładem jednostajnym dwupunktowym p=0.9 z klasy [01] o długości 100_000 znaków\n",
|
|
"\n",
|
|
"Następnie dla każdego z tekstów trakując je po znakach:\n",
|
|
"- skompresuj plik za pomocą dowolnego progrmu (zip, tar lub inny)\n",
|
|
"- policz entropię\n",
|
|
"- wytrenuj kodek huffmana i zakoduj cały tekst\n",
|
|
"- zdekoduj pierwsze 3 znaki (jako zera i jedynki) wypisz je (z oddzieleniem na znaki)\n",
|
|
"- zakodowany tekst zapisz do pliku binarnego, zapisz również tablicę kodową\n",
|
|
"- porównaj wielkość pliku tekstowego, skompresowanego pliku tekstowego (zip, ...) oraz pliku skompresowanego hofmmanem (wraz z kodekiem)\n",
|
|
"\n",
|
|
"Uzupełnij poniższe tabelki oraz wnioski (conajmniej 5 zdań).\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### START ZADANIA"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Entropia\n",
|
|
" \n",
|
|
"| | Entropia |\n",
|
|
"| ----------- | ----------- |\n",
|
|
"| tekst w jęz. naturalnym | |\n",
|
|
"| losowy tekst (jednostajny) | |\n",
|
|
"| losowy tekst (geometryczny)| |\n",
|
|
"| losowy tekst (dwupunktowy 0.5) | |\n",
|
|
"| losowy tekst (dwupunktowy 0.9) | |\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Wielkości w bitach:\n",
|
|
" \n",
|
|
"| | Plik nieskompresowany | Plik skompresowany (zip, tar,.. ) | Plik zakodowany + tablica kodowa |\n",
|
|
"| ----------- | ----------- |-----------|----------- |\n",
|
|
"| tekst w jęz. naturalnym | | | |\n",
|
|
"| losowy tekst (jednostajny) | | | |\n",
|
|
"| losowy tekst (geometryczny)| | | |\n",
|
|
"| losowy tekst (dwupunktowy 0.5)| | | |\n",
|
|
"| losowy tekst (dwupunktowy 0.9)| | | |"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Wnioski:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### KONIEC ZADANIA"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Zadanie 2 (10 punktów)\n",
|
|
"\n",
|
|
"Powtórz kroki z zadania 1, tylko potraktuje wiadomości jako słowa (oddzielone spacją). Jeżeli występują więcej niż jedna spacja równocześnie- usuń je.\n",
|
|
" \n",
|
|
"Do wniosków dopisz koniecznie porównanie między kodowaniem hoffmana znaków i słów.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### START ZADANIA"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Entropia\n",
|
|
" \n",
|
|
"| | Entropia |\n",
|
|
"| ----------- | ----------- |\n",
|
|
"| tekst w jęz. naturalnym | |\n",
|
|
"| losowy tekst (dyskretny) | |\n",
|
|
"| losowy tekst (geometryczny)| |\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Wielkości w bitach:\n",
|
|
" \n",
|
|
"| | Plik nieskompresowany | Plik skompresowany (zip, tar,.. ) | Plik zakodowany + tablica kodowa |\n",
|
|
"| ----------- | ----------- |-----------|----------- |\n",
|
|
"| tekst w jęz. naturalnym | | | |\n",
|
|
"| losowy tekst (jednostajny) | | | |\n",
|
|
"| losowy tekst (geometryczny)| | | |"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Wnioski:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### KONIEC ZADANIA"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Zadanie 3 (20 punktów)\n",
|
|
"\n",
|
|
"stwórz ręcznie drzewo Huffmana (zrób rysunki na kartce i załącz je jako obrazek) oraz zakoduj poniższy tekst "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"random.seed(123)\n",
|
|
"\n",
|
|
"tekst = list('abcdefghijklmnoprst')\n",
|
|
"\n",
|
|
"random.shuffle(tekst)\n",
|
|
"\n",
|
|
"tekst = tekst[: 5 + random.randint(1,5)]\n",
|
|
"\n",
|
|
"tekst = [a*random.randint(1,4) for a in tekst]\n",
|
|
"\n",
|
|
"tekst = [item for sublist in tekst for item in sublist]\n",
|
|
"\n",
|
|
"''.join(tekst)\n",
|
|
"\n",
|
|
"random.shuffle(tekst)\n",
|
|
"\n",
|
|
"tekst = ''.join(tekst)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'ldddmpprphhopd'"
|
|
]
|
|
},
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tekst"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Start zadania"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Koniec zadania"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## WYKONANIE ZADAŃ\n",
|
|
"Zgodnie z instrukcją 01_Kodowanie_tekstu.ipynb"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"author": "Jakub Pokrywka",
|
|
"email": "kubapok@wmi.amu.edu.pl",
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"lang": "pl",
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.3"
|
|
},
|
|
"subtitle": "0.Informacje na temat przedmiotu[ćwiczenia]",
|
|
"title": "Ekstrakcja informacji",
|
|
"year": "2021"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|