Chart Semirings¶
One chart algorithm answers different questions under different semirings (Goodman, 1999). The Boolean semiring asks whether a derivation exists, the log semiring adds the probabilities of alternatives in log space (the inside weight), the Viterbi semiring keeps the best derivation's weight, and the counting semiring counts derivations. In log space, multiplication is addition and summation is log-sum-exp.
semiring
¶
Semiring abstractions for parameterizing chart parsing.
A semiring (S, ⊕, ⊗, 0, 1) provides the algebraic structure for chart parsing (Goodman, 1999). Different semirings yield different parsing algorithms from the same CKY skeleton:
- LogProbSemiring: marginal log-probability (logsumexp / +)
- ViterbiSemiring: best-derivation log-probability (max / +)
- BooleanSemiring: recognition (or / and)
- CountingSemiring: derivation counting (+ / ×)
Each semiring defines:
times(a, b): multiplicative combination (⊗)plus(scores, dim): additive aggregation (⊕) over a dimensionzero: additive identity (0): score for impossible derivationsone: multiplicative identity (1): score for trivial derivations
Relation to the enrichment algebras
A semiring is the structure that makes the chart's sum over
derivations of products of rule weights well defined and independent
of evaluation order (Goodman 1999). A chart semiring is a member of
the one hierarchy of quivers.core.algebras: ChartSemiring is a
quivers.core.algebras.Semiring, and each chart semiring is the core
algebra of the same name read through the chart vocabulary, so its
times, plus, plus_pair, zero and one are the core
tensor_op, join, join_pair, zero and unit. A sum
over no terms is the zero, a sum of impossible log weights has
derivative zero, and a best-derivation sum gives the whole derivative
of a tie to its first operand in every operation.
LogProbSemiring is the log semiring
([-∞, ∞), logsumexp, +, -∞, 0), isomorphic to the probability
semiring ([0, ∞), +, ·) through exp. Its addition is not
idempotent (logsumexp(a, a) = a + log 2), so it is not a lattice
join and the log semiring is not a quantale; ViterbiSemiring and
BooleanSemiring are idempotent.
References
Goodman, J. (1999). Semiring parsing. Computational Linguistics, 25(4), 573-606. ACL Anthology J99-4004
ChartSemiring
¶
Bases: Semiring
A commutative semiring read through the chart vocabulary.
The chart names of the operations are the core operations of the
quivers.core.algebras.Semiring it is: times is tensor_op,
plus is join, plus_pair is join_pair and one is
unit. All operations are differentiable where the carrier
allows, to support gradient-based learning.
log_weights
class-attribute
¶
Whether the elements are log weights, so a learned log weight is one.
A rule weight learned as a log weight enters a semiring whose elements are log weights as it is; a semiring of other elements has no element it stands for, and the rule contributes its unit.
one
property
¶
Multiplicative identity, the score of a trivial derivation.
| RETURNS | DESCRIPTION |
|---|---|
float
|
The core |
times
¶
Multiplicative combination (⊗), the core tensor_op.
Combines scores from two chart cells being joined by a rule.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Left operand scores.
TYPE:
|
b
|
Right operand scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Combined scores. |
Source code in src/quivers/stochastic/semiring.py
plus
¶
Additive aggregation (⊕) over a dimension, the core join.
Marginalizes over multiple derivations of the same span.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Aggregated scores, the zero over no terms. |
Source code in src/quivers/stochastic/semiring.py
plus_pair
¶
Binary additive combination (⊕), the core join_pair.
Used for combining a cell with unary rule contributions.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
First operand.
TYPE:
|
b
|
Second operand.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Combined scores. |
Source code in src/quivers/stochastic/semiring.py
LogProbSemiring
¶
Bases: ChartSemiring, LogProbAlgebra
Log-probability semiring: (logsumexp, +, -∞, 0).
This is the default semiring for marginal parsing. times is
addition in log-space (= multiplication of probabilities) and
plus is logsumexp (= addition of probabilities in log-space).
ViterbiSemiring
¶
Bases: ChartSemiring, MaxPlusAlgebra
Viterbi semiring: (max, +, -∞, 0).
Finds the single best derivation. plus takes the max over
derivations rather than summing, and gives the whole derivative of
a tie to the first best operand. Useful for Viterbi parsing and
best-first decoding.
BooleanSemiring
¶
Bases: ChartSemiring, BooleanAlgebra
Boolean semiring: (or, and, 0, 1).
For recognition only: determines whether a parse exists without computing scores. Values are 0.0 (false) and 1.0 (true).
CountingSemiring
¶
Bases: ChartSemiring, CountingAlgebra
Counting semiring: (+, ×, 0, 1).
Counts the number of derivations. plus sums counts and
times multiplies them (product rule for independent choices).
TropicalSemiring
¶
Bases: ChartSemiring, TropicalAlgebra
Tropical semiring: (min, +, ∞, 0) over the nonnegative reals.
Finds the least total cost of a derivation.
GodelSemiring
¶
Bases: ChartSemiring, GodelAlgebra
Gödel semiring: (max, min, 0, 1) over the unit interval.
Scores a derivation by its weakest weight and an item by its best derivation.
MarkovSemiring
¶
chart_semiring
¶
chart_semiring(member: Semiring) -> ChartSemiring
The chart semiring that is a shipped algebra read through the chart vocabulary.
| PARAMETER | DESCRIPTION |
|---|---|
member
|
An algebra of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ChartSemiring
|
The chart semiring whose class is that algebra's class. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no chart semiring is that algebra. |
Source code in src/quivers/stochastic/semiring.py
Semiring singletons¶
Stochastic morphisms: Markov kernels over finite sets.
This module provides the probabilistic layer for quivers, implementing the category FinStoch of finite sets and stochastic maps (Markov kernels). This is the Kleisli category of the Giry monad restricted to finite sets, where:
- Objects are finite sets (same as the base category).
- Morphisms A -> B are stochastic matrices: tensors of shape (|A|, |B|) whose rows sum to 1.
- Composition is standard matrix multiplication.
- Identity is the Kronecker delta.
The package also re-exports MarkovAlgebra and its singleton
MARKOV from quivers.core.algebras, the algebra these morphisms
compose under.
Submodules
morphisms : StochasticMorphism, CategoricalMorphism families : Discretized distribution families transforms : condition, mix, factor, normalize queries : prob, marginal_prob, expectation giry : GiryMonad, FinStoch categories : Category types including atoms, slashes, products, unit, modals semiring : Chart semiring abstractions (LogProb, Viterbi, Boolean, Counting) schema : Composable rule schemas (functors CategorySystem -> RuleSystem) rules : Rule systems and the CCG / Lambek presets span : Span-based CKY components (LexicalAxiom, BinarySpanDeduction, etc.) parsers, ccg, lambek : Chart parsers inside : The inside algorithm over a rule system agenda : The agenda-driven weighted-deduction engine indexed : Charts indexed on the argument positions rule joins look up joins : Worst-case optimal joins and variable elimination for rule bodies planning : Plans for queries over rule presentations forest : Packed forests of derivations and folds over them recursion : Linear cyclic systems solved through the algebra's closure tries : Tries of relations and the intersection step of Generic Join productions : A grammar's productions grouped by left-hand side effect_lifts : Effect-lifted rule schemas for deduction systems deduction : Abstract weighted deductive system framework and its operations stdlib : Pre-registered deduction systems
LOG_PROB
module-attribute
¶
LOG_PROB = LogProbSemiring()
The log-probability semiring singleton, the default for chart parsing.
VITERBI
module-attribute
¶
VITERBI = ViterbiSemiring()
The Viterbi (max-product in log space) semiring singleton.
BOOLEAN
module-attribute
¶
BOOLEAN = BooleanSemiring()
The Boolean (or-and) semiring singleton, for recognition.
COUNTING
module-attribute
¶
COUNTING = CountingSemiring()
The counting (sum-product over naturals) semiring singleton.
TROPICAL
module-attribute
¶
TROPICAL = TropicalSemiring()
The tropical (min-plus) semiring singleton.
SUM_PRODUCT
module-attribute
¶
SUM_PRODUCT = MarkovSemiring()
The probability (sum-product over the nonnegative reals) semiring singleton.
CHART_SEMIRINGS
module-attribute
¶
CHART_SEMIRINGS: tuple[ChartSemiring, ...] = (LOG_PROB, VITERBI, BOOLEAN, COUNTING, TROPICAL, GODEL, SUM_PRODUCT)
The chart semirings, one per shipped algebra a deduction may run in.
Cited sources¶
Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.