dkl9 commited on 2025-202 21:09:02
Showing 1 changed files, with 3 additions and 1 deletions.
| ... | ... |
@@ -19,7 +19,7 @@ def minima(options, key): |
| 19 | 19 |
def cost(token_child: tuple[str, "Trie"]) -> tuple: |
| 20 | 20 |
token, child = token_child |
| 21 | 21 |
weight = child.deep_weight |
| 22 |
- return child.deep_usage / weight, -weight, len(token), token |
|
| 22 |
+ return child.deep_usage / weight, -weight, child.min_depth, len(token), token |
|
| 23 | 23 |
|
| 24 | 24 |
class Trie: |
| 25 | 25 |
def __init__(self, weight: int = 0): |
| ... | ... |
@@ -28,6 +28,7 @@ class Trie: |
| 28 | 28 |
self.deep_weight: int = weight |
| 29 | 29 |
self.usage: int = 0 |
| 30 | 30 |
self.deep_usage: int = 0 |
| 31 |
+ self.min_depth: int = 0 if weight else 999999 |
|
| 31 | 32 |
|
| 32 | 33 |
def __str__(self) -> str: |
| 33 | 34 |
s = f"[{self.usage}/{self.weight}]"
|
| ... | ... |
@@ -37,6 +38,7 @@ class Trie: |
| 37 | 38 |
|
| 38 | 39 |
def add(self, word: list[str], weight: int = 1): |
| 39 | 40 |
self.deep_weight += weight |
| 41 |
+ self.min_depth = min(self.min_depth, sum(len(t) for t in word)) |
|
| 40 | 42 |
if word: |
| 41 | 43 |
self.children[word[0]].add(word[1:], weight) |
| 42 | 44 |
else: |
| 43 | 45 |