forked from AastroLePetitRobot/Huffman-Coding
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHuffman.py
More file actions
95 lines (88 loc) · 3 KB
/
Huffman.py
File metadata and controls
95 lines (88 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from queue import PriorityQueue
# la on creer une node , elle deviendra plus tard l'arbre
class Node:
data = ' '
frequence=0
leftChild=None
rightChild=None
def __init__(self,data,frequence,leftChild=None,rightChild=None):
self.data=data
self.frequence=frequence
self.leftChild=leftChild
self.rightChild=rightChild
def __lt__(self,other):
return self.frequence < other.frequence
def __repr__(self):
return "Noeud : de valeur ({}), de fréquence ({}), avec comme leftChild(),avec comme rightChild ()\n".format(self.data,self.frequence) #self.leftChild.frequence,self.rightChild.frequence)
liste = PriorityQueue()
def init_liste(string):
tableauVerification=[]
boolean=True
boolean1=True
for val in str(string):
print (val)
if not val in str(tableauVerification):#on verifie l'élément de la chaine n'est pas déjà présent pour pas la remettre dans la liste
tableauVerification.append([val,string.count(val)])
liste.put(Node(val,string.count(val)))
elif (val==' ' and boolean==True):
liste.put(Node(val,string.count(val)))
tableauVerification.append([val,string.count(val)])
boolean=False
elif (val==',' and boolean1==True):
liste.put(Node(val,string.count(val)))
tableauVerification.append([val,string.count(val)])
boolean1=False
return liste
def creationArbre(liste):
while liste.qsize()>1:
noeudGauche=liste.get()
noeudDroit=liste.get()
frequence=noeudGauche.frequence + noeudDroit.frequence
noeud=Node(None,frequence,noeudGauche,noeudDroit)
liste.put(noeud)
return noeud
tableHachage={}
def creationTableHachage(val,code,tableHachage):
if val.leftChild==None and val.rightChild==None:
tableHachage.update({val.data : code})
return tableHachage
if val.leftChild!=None:
creationTableHachage(val.leftChild,code+"0",tableHachage)
if val.rightChild!=None:
creationTableHachage(val.rightChild,code+"1",tableHachage)
def encodage(string,tableHachage):
chaineCoder=""
for i in string:
chaineCoder=chaineCoder+str(tableHachage[i])
return chaineCoder
i=0
def decodage(string,noeud):
global i
if noeud.leftChild==None and noeud.rightChild==None:
char=noeud.data
return char
if string[i]=="0":
i+=1
return decodage(string,noeud.leftChild)
elif string[i]=="1":
i+=1
return decodage(string,noeud.rightChild)
f = open('texteEncode.txt', 'r')
string = f.read()
f.close()
liste=init_liste(string)
noeud= creationArbre(liste)
print(noeud)
creationTableHachage(noeud,"",tableHachage)
print (tableHachage)
encode=encodage(string,tableHachage)
print (encode)
f = open('texteEncode.txt', 'w')
f.write(str(encode))
f.close()
string = encodage(string,tableHachage)
stringc=""
while i<len(string):
stringc+=decodage(string,noeud)
print(stringc)
f.close()