| 
									
										
										
										
											2022-12-07 15:32:56 +01:00
										 |  |  | #!/bin/python | 
					
						
							|  |  |  | """ Compression
 | 
					
						
							|  |  |  | Usage: | 
					
						
							|  |  |  |     compression.py (-c|-d) -t <type> --in <input> [--out <output>] | 
					
						
							|  |  |  |     compression.py -h | --help | 
					
						
							|  |  |  |     compression.py --version | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Options: | 
					
						
							|  |  |  |     -h --help   Show this screen | 
					
						
							|  |  |  |     --version   Show Version | 
					
						
							|  |  |  |     -c          Compress | 
					
						
							|  |  |  |     -d          Decompress | 
					
						
							| 
									
										
										
										
											2023-02-14 17:45:04 +01:00
										 |  |  |     -t          Choose Compression Type (currently supported: lz, rle) | 
					
						
							| 
									
										
										
										
											2022-12-07 15:32:56 +01:00
										 |  |  |     --in        Input file | 
					
						
							|  |  |  |     --out       Output file | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 17:45:04 +01:00
										 |  |  | def read_file(filename:str) -> str: | 
					
						
							| 
									
										
										
										
											2022-12-07 15:32:56 +01:00
										 |  |  |     try: | 
					
						
							|  |  |  |         with open(filename) as file: | 
					
						
							| 
									
										
										
										
											2023-02-14 17:45:04 +01:00
										 |  |  |             return file.read() | 
					
						
							| 
									
										
										
										
											2022-12-07 15:32:56 +01:00
										 |  |  |     except e: | 
					
						
							|  |  |  |         print(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def lz_compression(text:str): | 
					
						
							|  |  |  |     """compress all duplicate word
 | 
					
						
							|  |  |  |     put each word into a list and make make a string of all the occurence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :text: string to compress | 
					
						
							|  |  |  |     :returns: tuple with (list of word, decoding string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     splitted_text = text.split() | 
					
						
							| 
									
										
										
										
											2023-02-14 17:45:04 +01:00
										 |  |  |     word_list = list() | 
					
						
							|  |  |  |     key = dict() | 
					
						
							|  |  |  |     key_value = 0 | 
					
						
							|  |  |  |     for v in splitted_text: | 
					
						
							|  |  |  |         if v not in key.keys(): | 
					
						
							|  |  |  |             key[v] = key_value  | 
					
						
							|  |  |  |             key_value += 1 | 
					
						
							|  |  |  |         word_list.append(key[v]) | 
					
						
							|  |  |  |     return word_list, key | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def save_lz(filename, word_list, key_list): | 
					
						
							|  |  |  |     with open(filename, 'w') as file: | 
					
						
							|  |  |  |         word_list = map(str, word_list) | 
					
						
							|  |  |  |         file.writelines(' '.join(word_list)) | 
					
						
							|  |  |  |         file.writelines(' '.join(list(key_list.items()))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def lz_decompression(text:str): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rle_compression(text:str): | 
					
						
							|  |  |  |     """compress all duplicate word
 | 
					
						
							|  |  |  |     put each word into a list and make make a string of all the occurence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :text: string to compress | 
					
						
							|  |  |  |     :returns: tuple with (list of word, decoding string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     splitted_text = text.split() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rle_decompression(text:str): | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2022-12-07 15:32:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     from docopt import docopt | 
					
						
							|  |  |  |     argument = docopt(__doc__, version="V1") | 
					
						
							| 
									
										
										
										
											2023-02-14 17:45:04 +01:00
										 |  |  |     print(argument) | 
					
						
							|  |  |  |     if argument['<type>'].lower() == 'lz': | 
					
						
							|  |  |  |         if argument['-d']: | 
					
						
							|  |  |  |             result = lz_decompression(read_file(argument['<input>'])) | 
					
						
							|  |  |  |             print(result) | 
					
						
							|  |  |  |         if argument['-c']: | 
					
						
							|  |  |  |             w_list, k_list = lz_compression(read_file(argument['<input>'])) | 
					
						
							|  |  |  |             save_lz('test.Ltxt', w_list, k_list) | 
					
						
							|  |  |  |     elif argument['<type>'].lower() == 'rle': | 
					
						
							|  |  |  |         if argument['-d']: | 
					
						
							|  |  |  |             result = rle_decompression(read_file(argument['<input>'])) | 
					
						
							|  |  |  |             print(result) | 
					
						
							|  |  |  |         if argument['-c']: | 
					
						
							|  |  |  |             result = rle_compression(read_file(argument['<input>'])) | 
					
						
							|  |  |  |             print(result) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise TypeError("choose a type between lz and rle") |