mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Compare commits
6 Commits
521d7a23fa
...
fe3a43c64b
Author | SHA1 | Date | |
---|---|---|---|
|
fe3a43c64b | ||
|
930c4d463f | ||
|
bad910e71c | ||
|
4359762495 | ||
|
435f4518c2 | ||
|
653f8e4d4f |
|
@ -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()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user