cky
CKYLM
Bases: LM
Probabilistic Context-Free Grammar Language Model.
Uses CKY parsing algorithm and prefix grammar transformation for efficient inference over context-free grammars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
CFG
|
The context-free grammar to use as the language model |
required |
**kwargs
|
Additional arguments passed to IncrementalCKY |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
cfg |
CFG
|
The original context-free grammar |
pfg |
CFG
|
The prefix grammar in CNF form used for incremental parsing |
model |
IncrementalCKY
|
The incremental CKY parser for computing probabilities |
Source code in genlm/grammar/parse/cky.py
__init__(cfg, **kwargs)
Initialize a CKY-based language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
CFG
|
The context-free grammar to use as the language model. Will be converted to CNF and prefix form for incremental parsing. |
required |
**kwargs
|
Additional arguments passed to IncrementalCKY parser initialization |
{}
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If EOS token not in grammar vocabulary |
Source code in genlm/grammar/parse/cky.py
clear_cache()
from_string(x, semiring=Float, **kwargs)
classmethod
Create a CKYLM from a grammar string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str
|
String representation of the grammar |
required |
semiring
|
Semiring to use for weights (default: Float) |
Float
|
|
**kwargs
|
Additional arguments for grammar normalization |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
CKYLM |
A new language model instance |
Source code in genlm/grammar/parse/cky.py
p_next(context)
Compute probability distribution over next tokens given a context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Sequence of tokens representing the prefix |
required |
Returns:
| Type | Description |
|---|---|
|
Normalized probability distribution over possible next tokens |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If context contains tokens not in vocabulary |
Source code in genlm/grammar/parse/cky.py
IncrementalCKY
An incremental CKY parser implementation for Context-Free Grammars (CFG).
This parser maintains a chart cache for efficient incremental parsing and supports weight computations for next-token predictions.
Source code in genlm/grammar/parse/cky.py
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 | |
__init__(cfg)
Initialize an incremental CKY parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
CFG
|
The context-free grammar |
required |
Source code in genlm/grammar/parse/cky.py
chart(prefix)
Get the parsing chart for a given prefix, computing it if not cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
The sequence of tokens to parse |
required |
Returns:
| Type | Description |
|---|---|
|
The CKY chart for the prefix |
Source code in genlm/grammar/parse/cky.py
extend_chart(chart, prefix)
An O(N²) time algorithm that extends the parsing chart with the last token of the prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart
|
The current CKY chart |
required | |
prefix
|
The sequence of tokens including the new token |
required |
Returns:
| Type | Description |
|---|---|
|
A new chart column incorporating the last token |
Source code in genlm/grammar/parse/cky.py
next_token_weights(chart, prefix)
Compute the total weight for each possible next token following the prefix.
An O(N²) time algorithm that calculates the total weight of a each next-token
extension of prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chart
|
The current CKY chart |
required | |
prefix
|
The current sequence of tokens |
required |
Returns:
| Type | Description |
|---|---|
|
(Chart) Dictionary mapping possible next tokens to their weights # XXX |
Source code in genlm/grammar/parse/cky.py
p_next(prefix)
Compute the weights for all possible next tokens given a prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
The current sequence of tokens |
required |
Returns:
| Type | Description |
|---|---|
|
Dictionary mapping possible next tokens to their weights |