Rule Systems¶
Composable structural rule systems for chart parsing. A RuleSystem encapsulates binary and unary inference rules as index tensors and supports merging (+) for hybrid grammars.
Each rule system optionally carries learnable weights, one log-weight per binary rule and one per unary rule. These weights are added to the chart combination scores during parsing. When a RuleSystem is passed to a ChartParser, its weights initialize the parser's nn.Parameter tensors (or fixed buffers if learnable_rule_weights=False). Weight-preserving merge: when two rule systems are combined with +, weights from both systems are preserved for their respective rules; duplicate rules keep the weight from the left operand.
The convenience functions ccg_rules and lambek_rules instantiate
rule schema presets over a given category system.
For custom grammars, compose the schema primitives directly via |.
rules
¶
Composable rule systems for chart parsing.
A RuleSystem encapsulates the structural inference rules that govern
a grammar -- binary combination rules and unary type-change rules -- as
index tensors suitable for CKY chart parsing. Rule systems are the
compositional unit: they can be merged (+), inspected, and passed
to any ChartParser.
The primitive rule schemas live in quivers.stochastic.schema,
which provides composable functors CategorySystem -> RuleSystem.
The convenience functions here (ccg_rules, lambek_rules)
instantiate schema presets over a given category system.
Categorical perspective
A rule system is a presentation of the arrows in a free category generated by a set of basic combinators. Merging two rule systems takes the coproduct of their generating sets.
RuleSystem
¶
Bases: Model
A composable set of structural rules for chart parsing.
| ATTRIBUTE | DESCRIPTION |
|---|---|
binary_rules |
Each inner tuple is
TYPE:
|
unary_rules |
Each inner tuple is
TYPE:
|
n_categories |
Total number of categories in the system.
TYPE:
|
description |
Human-readable label (e.g.
TYPE:
|
binary_weights |
Initial log-weights for binary rules.
TYPE:
|
unary_weights |
Initial log-weights for unary rules.
TYPE:
|
binary_tensors
¶
binary_tensors(device: device | None = None) -> tuple[Tensor, Tensor, Tensor]
Return binary rules as (results, lefts, rights) index tensors.
Source code in src/quivers/stochastic/_rule_system.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
binary_weight_tensor
¶
binary_weight_tensor(device: device | None = None) -> Tensor
Return initial binary rule weights as a float tensor.
Returns zeros when no explicit weights are set.
Source code in src/quivers/stochastic/_rule_system.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
unary_tensors
¶
unary_tensors(device: device | None = None) -> tuple[Tensor, Tensor] | None
Return unary rules as (results, inputs) index tensors, or None.
Source code in src/quivers/stochastic/_rule_system.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
unary_weight_tensor
¶
unary_weight_tensor(device: device | None = None) -> Tensor
Return initial unary rule weights as a float tensor.
Returns zeros when no explicit weights are set.
Source code in src/quivers/stochastic/_rule_system.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
__add__
¶
__add__(other: RuleSystem) -> RuleSystem
Merge two rule systems (deduplicated union).
Source code in src/quivers/stochastic/_rule_system.py
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 | |
ccg_rules
¶
ccg_rules(system: CategorySystem, *, enable_composition: bool = True, enable_crossed_composition: bool = True, enable_type_raising: bool = False, generalized_composition_depth: int = 1) -> RuleSystem
Build a CCG rule system from a category inventory.
| PARAMETER | DESCRIPTION |
|---|---|
system
|
The finite category inventory.
TYPE:
|
enable_composition
|
Enable harmonic composition (>B, <B).
TYPE:
|
enable_crossed_composition
|
Enable crossed composition (>Bx, <Bx).
TYPE:
|
enable_type_raising
|
Enable type raising (>T, <T).
TYPE:
|
generalized_composition_depth
|
Maximum depth for generalized composition (B^n).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The CCG rule system. |
Source code in src/quivers/stochastic/rules.py
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 | |
lambek_rules
¶
lambek_rules(system: CategorySystem, *, associative: bool = True, commutative: bool = False, enable_lifting: bool = True, enable_product: bool = True) -> RuleSystem
Build a Lambek calculus rule system from a category inventory.
| PARAMETER | DESCRIPTION |
|---|---|
system
|
The finite category inventory.
TYPE:
|
associative
|
Use associative Lambek calculus L (default True).
TYPE:
|
commutative
|
Use commutative product / LP calculus (default False).
TYPE:
|
enable_lifting
|
Enable Lambek lifting rules (adjunction units).
TYPE:
|
enable_product
|
Enable product introduction / projection.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The Lambek calculus rule system. |
Source code in src/quivers/stochastic/rules.py
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 | |
custom_rules
¶
custom_rules(binary: list[tuple[int, int, int]] | None = None, unary: list[tuple[int, int]] | None = None, n_categories: int = 0, description: str = 'custom') -> RuleSystem
Build a rule system from explicit rule triples.
| PARAMETER | DESCRIPTION |
|---|---|
binary
|
Binary rules as (result, left, right) triples.
TYPE:
|
unary
|
Unary rules as (result, input) pairs.
TYPE:
|
n_categories
|
Total number of categories.
TYPE:
|
description
|
Label for the rule system.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The custom rule system. |
Source code in src/quivers/stochastic/rules.py
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 | |