From ab94c409ea0bafd02df1ded3c39b68f56a9ed09f Mon Sep 17 00:00:00 2001 From: breandan Date: Sat, 18 Jan 2025 18:07:53 -0500 Subject: [PATCH] incorporate matrix LBH into experimental pipeline --- .../ai/hypergraph/kaliningraph/automata/FSA.kt | 5 +---- .../ai/hypergraph/kaliningraph/parsing/SeqValiant.kt | 4 ++++ .../hypergraph/kaliningraph/parsing/JVMBarHillel.kt | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt index a203eb61..bb9b0748 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt @@ -172,10 +172,7 @@ open class FSA constructor(open val Q: TSA, open val init: Set<Σᐩ>, open val fun LED(cfg: CFG, brokeToks: Σᐩ): Int = (1 until (2 * MAX_RADIUS)).firstOrNull { FSA.nonemptyLevInt(brokeToks, cfg, it) } ?: (2 * MAX_RADIUS) - fun intersectPTree(brokenStr: Σᐩ, cfg: CFG, radius: Int): PTree? { - // 1) Build the Levenshtein automaton (acyclic) - val levFSA = makeLevFSA(brokenStr, radius) - + fun intersectPTree(brokenStr: Σᐩ, cfg: CFG, radius: Int, levFSA: FSA = makeLevFSA(brokenStr, radius)): PTree? { val nStates = levFSA.numStates val startIdx = cfg.bindex[START_SYMBOL] diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt index ab9a0d30..6357e012 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt @@ -41,6 +41,10 @@ class PTree constructor(val root: String = ".ε", val branches: List<Π2A // TODO: Use weighted choice mechanism val shuffledBranches by lazy { branches.shuffled().sortedBy { "ε" !in it.first.root + it.second.root } } + val totalProds by lazy { + if (branches.isEmpty()) 1 + else branches.map { (l, r) -> l.totalTrees + r.totalTrees }.reduce { acc, it -> acc + it } + } val totalTreesStr by lazy { totalTrees.toString() } val totalTrees: BigInteger by lazy { if (branches.isEmpty()) BigInteger.ONE diff --git a/src/jvmMain/kotlin/ai/hypergraph/kaliningraph/parsing/JVMBarHillel.kt b/src/jvmMain/kotlin/ai/hypergraph/kaliningraph/parsing/JVMBarHillel.kt index 99b0cd23..1190b166 100644 --- a/src/jvmMain/kotlin/ai/hypergraph/kaliningraph/parsing/JVMBarHillel.kt +++ b/src/jvmMain/kotlin/ai/hypergraph/kaliningraph/parsing/JVMBarHillel.kt @@ -88,6 +88,18 @@ fun PTree.sampleWithPCFG( .asStream() } +fun PTree.sampleDirectlyWR( + cores: Int = NUM_CORES, + stoppingCriterion: () -> Boolean = { true } +): Stream = + (0.. + sampleWRGD() + .map { it.removeEpsilon() } + .takeWhile { stoppingCriterion() } + .distinct() + .asStream() + } + fun PTree.sampleDirectlyWOR( cores: Int = NUM_CORES, stoppingCriterion: () -> Boolean = { true }