def program(): j = 0 option = input( "Wpisz opcję(1 - Drzewo Huffmana + Kodowanie Prufera lub 2 - Dekodowanie Prufera lub 3 - Wyjście): " ) if option == "1": while True: fileWithInputData = input("Plik z tekstem wejściowym: ") if os.path.exists(fileWithInputData): file_object = open(fileWithInputData, "r") break else: print("Plik nie istnieje, podaj inną scieżkę!") continue word = file_object.read() dictionary = {} for c in word: if (c in dictionary): dictionary[c] += 1 else: dictionary[c] = 1 code, tree = Huffman_code(dictionary) nameOfGraphFile = input( "Ścieżka do grafu wyjściowego - bez rozszerzenia: ") set_globvar_to_zero() with open(nameOfGraphFile + ".dot", 'w') as f: f.write('digraph G {\n') f.write(draw_tree(tree, 0)) f.write('}') subprocess.call('dot -Tpng ' + nameOfGraphFile + '.dot -o ' + nameOfGraphFile + '.png', shell=True) j = 0 a = Prufer.to_prufer(graphStructure, len(graphStructure) + 1) nameOfFile = input("Ścieżka do pliku wyjsciowego: ") file = open(nameOfFile, "w") first = "0" second = "" third = "" for x in a: second = second + str(x) + " " for c in listValue: third = third + str(c) + " " lines = [first, "\n", second, "\n", third] file.writelines(lines) file.close() f = Image.open(nameOfGraphFile + ".png").show() program() elif option == "2": while True: fileWithInputDataMode2 = input( "Ścieżka do pliku z kodem Prufera: ") if os.path.exists(fileWithInputDataMode2): file_object = open(fileWithInputDataMode2, "r") break else: print("Plik nie istnieje, podaj inną scieżkę!") continue pruferVert = [] pruferLet = [] for i, line in enumerate(file_object): if i == 1: pruferNumbers = line.split(" ") for z in pruferNumbers: pruferVert.append(z) elif i == 2: pruferLetters = line.split(" ") for z in pruferLetters: pruferLet.append(z) elif i > 2: break for line in pruferVert: if line == " " or line == '\n': pruferVert.remove(line) for line in pruferLetters: if line == " " or line == '\n' or line == '': pruferLetters.remove(line) #print(pruferLetters) #print(pruferVert) Prufer_list = [int(i) for i in pruferVert] firstPrufferLetter = pruferLetters[0] a = Prufer.to_tree(Prufer_list) newStr = "digraph G {\n" numberLetterDictionary = [] for x in a: numberLetterDictionary.append(str(x[0])) for x in a: newStr += str(x[0]) + " -> " newStr += str(x[1]) + "\n" if str(x[1]) not in numberLetterDictionary: newStr += '%s [label="%s %s", fontsize=16, fontcolor=blue, width=2, shape=box];\n' % ( str(x[1]), str(x[1]), firstPrufferLetter) + "\n" try: pruferLetters.pop(0) firstPrufferLetter = pruferLetters[0] except IndexError: pass newStr += "}" nameOfGraphFilePrufer = input( "Podaj sciezke gdzie ma powstac graf - bez rozszerzenia: ") with open(nameOfGraphFilePrufer + ".dot", 'w') as f: f.write(newStr) subprocess.call('dot -Tpng ' + nameOfGraphFilePrufer + '.dot -o ' + nameOfGraphFilePrufer + '.png', shell=True) f = Image.open(nameOfGraphFilePrufer + ".png").show() program() elif option == "3": sys.exit() else: print("Coś poszło nie tak, proszę wybrać 1,2 lub 3") program()
def menu(modeOption): if modeOption == "1": while True: fileWithInputDataForMode1 = input("Podaj scieżkę do pliku z tekstem wejsciowym: ") if os.path.exists(fileWithInputDataForMode1): file_object = open(fileWithInputDataForMode1, "r") print("\n# =====================================================") print("# Wybrałeś plik: " +os.path.basename(fileWithInputDataForMode1)) print("# =====================================================\n") break else: print ("Plik "+ os.path.basename(fileWithInputDataForMode1)+" nie istnieje, podaj inną scieżkę!") continue readFromFile = file_object.read() codes = {} for z in readFromFile: if z == "\n": z = " " if( z in codes): codes[z] += 1 else: codes[z] = 1 code, tree = Huffman_code(codes) nameOfGraphFileForMode1 = input("Podaj sciezke w której ma powstać graf(no extension): ") print("\n# =====================================================") print("# Graf powstał w pliku: " +os.path.basename(nameOfGraphFileForMode1) + ".jpeg") print("# =====================================================\n") with open(nameOfGraphFileForMode1 + ".dot",'w') as f: f.write('digraph G {\n') f.write(draw_tree(tree, 0)) f.write('}') subprocess.call('dot -Tjpeg '+nameOfGraphFileForMode1+'.dot -o '+nameOfGraphFileForMode1+'.jpeg', shell=True) a = Prufer.to_prufer(listOfEdges,len(listOfEdges)+1) fileWithOutputDataForMode1 = input("Podaj sciezke do pliku wyjściowego: ") print("\n# =====================================================") print("# Powstały plik wyjściowy to: " +os.path.basename(fileWithOutputDataForMode1)) print("# =====================================================\n") outputFileForMode1 = open(fileWithOutputDataForMode1, "w") firstLine = "0" secondLine = "" thirdLine = "" for x in a: secondLine = secondLine + str(x) + " " for c in listValue: thirdLine = thirdLine + str(c) + " " lines = [firstLine,"\n",secondLine,"\n",thirdLine] outputFileForMode1.writelines(lines) outputFileForMode1.close() with open("tempFile.txt",'wb') as tF: pickle.dump(listOfLetters, tF) f = Image.open(nameOfGraphFileForMode1+".jpeg").show() os.system(fileWithOutputDataForMode1) elif modeOption == "2": while True: fileWithInputDataMode2 = input("Podaj ścieżkę do pliku zawierającego kod Prufera: ") if os.path.exists(fileWithInputDataMode2): file_object = open(fileWithInputDataMode2, "r") print("\n# =====================================================") print("# Wybrałeś plik: " +os.path.basename(fileWithInputDataMode2)) print("# =====================================================\n") break else: print ("Plik "+ os.path.basename(fileWithInputDataMode2)+" nie istnieje, podaj inną scieżkę!") continue pruferVert = [] for a, b in enumerate(file_object): if a == 1: c = b.split(" ") for d in c: pruferVert.append(d) elif a > 1: break for x in pruferVert: if x == " " or x == '\n': pruferVert.remove(x) while True: try: Prufer_list = [int(i) for i in pruferVert] break except ValueError: print ("Oops! Zły format pliku wejściowego "+ os.path.basename(fileWithInputDataMode2)+". Zamykanie programu...") sys.exit() pruferTree = Prufer.to_tree(Prufer_list) if os.path.exists('tempFile.txt'): with open ('tempFile.txt', 'rb') as fp: itemlist = pickle.load(fp) else: itemlist={} newStr = "digraph G {\n" for x in pruferTree: if itemlist.get(x[0]): newStr +='%s [label="%s\n%s", fontcolor=black, fontsize=14];\n'%(str(x[0]), str(x[0]), itemlist.get(x[0])) elif itemlist.get(x[1]): newStr +='%s [label="%s\n%s", fontcolor=black, fontsize=14];\n'%(str(x[1]), str(x[1]), itemlist.get(x[1])) newStr += str(x[0]) + " -> " newStr += str(x[1]) + "\n" newStr+="}" nameOfGraphFileMode2= input("Podaj sciezke gdzie ma powstac graf(no extension): ") print("\n# =====================================================") print("# Graf powstał w pliku: " +os.path.basename(nameOfGraphFileMode2) + ".jpeg") print("# =====================================================\n") with open(nameOfGraphFileMode2 + ".dot",'w') as f: f.write(newStr) subprocess.call('dot -Tjpeg '+nameOfGraphFileMode2+'.dot -o '+nameOfGraphFileMode2+'.jpeg', shell=True) f = Image.open(nameOfGraphFileMode2+".jpeg").show() elif modeOption == '3': os.system('cls') sys.exit() else: print("\n# =====================================================") print("# BŁĄD") print("# PODAJ POPRAWNĄ WARTOŚĆ Z MENU") print("# =====================================================\n") print("1. Kodowanie na podstawie gotowego pliku tekstowego ") print("2. Dekodowanie na podstawie pliku z kodem Prufera") print("3. Zamknij program\n") print("# =====================================================\n") newVal = input(">> ") menu(newVal)