chart
Chart
Bases: dict
A weighted chart data structure that extends dict with semiring operations.
The Chart class provides methods for semiring operations like addition and multiplication, as well as utilities for filtering, comparing, and manipulating weighted values.
Attributes:
| Name | Type | Description |
|---|---|---|
semiring |
The semiring that defines the weight operations |
Source code in genlm/grammar/chart.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
__add__(other)
Add two charts element-wise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another Chart to add to this one |
required |
Returns:
| Type | Description |
|---|---|
|
A new Chart containing the element-wise sum |
Source code in genlm/grammar/chart.py
__init__(semiring, vals=())
Initialize a Chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
semiring
|
The semiring for weight operations |
required | |
vals
|
Optional initial values for the chart |
()
|
__missing__(k)
__mul__(other)
Multiply two charts element-wise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another Chart to multiply with this one |
required |
Returns:
| Type | Description |
|---|---|
|
A new Chart containing the element-wise product |
Source code in genlm/grammar/chart.py
__repr__()
__str__(style_value=lambda k, v: str(v))
Return formatted string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style_value
|
Optional function to format values |
lambda k, v: str(v)
|
Returns:
| Type | Description |
|---|---|
|
Formatted string showing non-zero entries |
Source code in genlm/grammar/chart.py
argmax()
argmin()
assert_equal(want, *, domain=None, tol=1e-05, verbose=False, throw=True)
Assert that this Chart equals another within tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
want
|
The expected Chart or dict of values |
required | |
domain
|
Optional set of keys to check |
None
|
|
tol
|
Tolerance for floating point comparisons |
1e-05
|
|
verbose
|
Whether to print detailed comparison |
False
|
|
throw
|
Whether to raise AssertionError on mismatch |
True
|
Source code in genlm/grammar/chart.py
compare(other, *, domain=None)
Compare this Chart to another using pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another Chart or dict to compare against |
required | |
domain
|
Optional set of keys to compare |
None
|
Returns:
| Type | Description |
|---|---|
|
pandas DataFrame showing key-by-key comparison |
Source code in genlm/grammar/chart.py
copy()
filter(f)
Return a new Chart keeping only entries where f(key) is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Predicate function that takes a key and returns bool |
required |
Returns:
| Type | Description |
|---|---|
|
A new Chart containing only entries where f(key) is True |
Source code in genlm/grammar/chart.py
max()
metric(other)
Compute the maximum distance between this Chart and another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another Chart to compare against |
required |
Returns:
| Type | Description |
|---|---|
|
The maximum semiring metric between corresponding values |
Source code in genlm/grammar/chart.py
min()
normalize()
product(ks)
Compute the product of values for the given keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ks
|
Sequence of keys to multiply values for |
required |
Returns:
| Type | Description |
|---|---|
|
The product of values for the given keys |
Source code in genlm/grammar/chart.py
project(f)
Apply a function to keys, summing weights when transformed keys overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Function to transform keys |
required |
Returns:
| Type | Description |
|---|---|
|
A new Chart with transformed keys and summed weights |
Source code in genlm/grammar/chart.py
sort(**kwargs)
Return a new Chart with entries sorted by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Arguments passed to sorted() |
{}
|
Returns:
| Type | Description |
|---|---|
|
A new Chart with sorted entries |
Source code in genlm/grammar/chart.py
sort_descending()
spawn()
sum()
top(k)
Return a new Chart with the k largest values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
Number of top values to keep |
required |
Returns:
| Type | Description |
|---|---|
|
A new Chart containing only the k largest values |