mixle.models.grammar moduleΒΆ

Probabilistic context-free grammar fitting and parse inspection helpers.

The module wraps induced heterogeneous PCFG estimators with fit diagnostics, likelihood evaluation, Viterbi parse reconstruction, and rule-table extraction for small grammar-learning experiments.

class GrammarLearningResult(model, history, validation_history=None)[source]

Bases: FitResult[HeterogeneousPCFGDistribution]

Fitted PCFG plus training and optional validation log-likelihood history.

Parameters:
class PCFGParseNode(label, span, log_prob, rule_index, rule_type, children=(), value=None)[source]

Bases: object

Node in a Viterbi parse tree.

Parameters:
leaves()[source]

Return terminal observations under this node.

Return type:

list[Any]

fit_induced_pcfg(data, terminal_estimators, max_nonterminals, initial_model=None, vdata=None, max_its=10, init_p=1.0, seed=None, terminal_rule_mass=0.5, rule_pseudo_count=1.0e-3, prune_threshold=0.0, min_rule_prob=0.0, start='S', name=None)[source]

Fit an induced heterogeneous PCFG and track train/validation likelihoods.

Parameters:
Return type:

GrammarLearningResult

pcfg_log_likelihood(model, data)[source]

Return total PCFG log likelihood on raw sequences.

Parameters:
Return type:

float

viterbi_parse(model, sequence)[source]

Return the maximum-probability CKY parse under a heterogeneous PCFG.

Parameters:
  • model (HeterogeneousPCFGDistribution)

  • sequence (Sequence[Any])

Return type:

PCFGParseNode

grammar_rule_table(model)[source]

Return a flat, inspectable rule table for learned PCFGs.

Parameters:

model (HeterogeneousPCFGDistribution)

Return type:

list[dict[str, Any]]