2
2
import sys
3
3
from collections import defaultdict
4
4
5
- #Description for the ppm algorithm can be found at https://en.wikipedia.org/wiki/Prediction_by_partial_matching
5
+ # Description for the ppm algorithm can be found at https://en.wikipedia.org/wiki/Prediction_by_partial_matching
6
+
6
7
7
8
class PPMNode :
8
9
def __init__ (self ) -> None :
@@ -43,7 +44,7 @@ def compress(self, data: str) -> list[float]:
43
44
# Encode the symbol based on the current context
44
45
compressed_output .append (self .encode_symbol (context , symbol ))
45
46
# Update the context by appending the symbol, keeping it within the specified order
46
- context = (context + symbol )[- self .order :] # Keep the context within order
47
+ context = (context + symbol )[- self .order :] # Keep the context within order
47
48
48
49
return compressed_output
49
50
@@ -73,11 +74,13 @@ def decompress(self, compressed_data: list[float]) -> str:
73
74
if symbol :
74
75
decompressed_output .append (symbol )
75
76
# Update the context with the newly decoded symbol
76
- context = (context + symbol )[- self .order :] # Keep the context within order
77
+ context = (context + symbol )[
78
+ - self .order :
79
+ ] # Keep the context within order
77
80
else :
78
81
break # Stop if a symbol cannot be found
79
82
80
- return '' .join (decompressed_output ) # Join the list into a single string
83
+ return "" .join (decompressed_output ) # Join the list into a single string
81
84
82
85
def decode_symbol (self , context : str , prob : float ) -> str | None :
83
86
# Decode a symbol from the given context based on the probability
@@ -98,7 +101,7 @@ def decode_symbol(self, context: str, prob: float) -> str | None:
98
101
99
102
def read_file (file_path : str ) -> str :
100
103
"""Read the entire file and return its content as a string."""
101
- with open (file_path , 'r' ) as f :
104
+ with open (file_path , "r" ) as f :
102
105
return f .read ()
103
106
104
107
@@ -123,4 +126,4 @@ def ppm(file_path: str) -> None:
123
126
sys .exit (1 )
124
127
125
128
# Call the ppm function with the provided file path
126
- ppm (sys .argv [1 ])
129
+ ppm (sys .argv [1 ])
0 commit comments