Compare commits

...

7 Commits

Author SHA1 Message Date
Lukas Olenyi
1ec326ba0f
Merge fe3a43c64b into f3f32ae3ca 2024-11-21 09:30:02 +00:00
Lukas Olenyi
fe3a43c64b ruff fixes 2024-11-21 10:29:55 +01:00
pre-commit-ci[bot]
930c4d463f [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-11-21 09:08:39 +00:00
Lukas Olenyi
bad910e71c fixed last issues with ruff 2024-11-21 10:07:01 +01:00
pre-commit-ci[bot]
4359762495 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-11-21 08:48:54 +00:00
Lukas Olenyi
435f4518c2 trying to pass ruff tests 2024-11-21 09:48:26 +01:00
Lukas Olenyi
653f8e4d4f trying to make the code pass ruff auto review 2024-11-21 09:48:26 +01:00

View File

@ -1,4 +1,5 @@
from __future__ import annotations
import sys
from collections import defaultdict
@ -7,7 +8,8 @@ from collections import defaultdict
class PPMNode:
def __init__(self) -> None:
# Initialize a PPMNode with a dictionary for child nodes and a count of total occurrences
# Initialize a PPMNode with a dictionary for child nodes
# and a count of total occurrences
self.counts: dict[str, PPMNode] = defaultdict(PPMNode)
self.total: int = 0
@ -43,7 +45,8 @@ class PPM:
self.update_model(context, symbol)
# Encode the symbol based on the current context
compressed_output.append(self.encode_symbol(context, symbol))
# Update the context by appending the symbol, keeping it within the specified order
# Update the context by appending the symbol,
# keeping it within the specified order
context = (context + symbol)[-self.order :] # Keep the context within order
return compressed_output
@ -92,7 +95,8 @@ class PPM:
else:
return None # Return None if the context is not found
# Iterate through the children of the node to find the symbol matching the given probability
# Iterate through the children of the node to
# find the symbol matching the given probability
for symbol, child in node.counts.items():
if child.total / node.total == prob:
return symbol # Return the symbol if the probability matches
@ -101,7 +105,7 @@ class PPM:
def read_file(file_path: str) -> str:
"""Read the entire file and return its content as a string."""
with open(file_path, "r") as f:
with open(file_path) as f:
return f.read()