Skip to content

Commit

Permalink
revert unit rule simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Jul 13, 2024
1 parent b4e1ab3 commit 2cf1371
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,26 @@ fun CFG.intersectLevFSAP(fsa: FSA, parikhMap: ParikhMap = this.parikhMap): CFG {
println("Constructing ∩-grammar took: ${clock.elapsedNow()}")
clock = TimeSource.Monotonic.markNow()
return (initFinal + binaryProds + unitProds).toSet().postProcess()
.expandNonterminalStubs(origCFG = this@intersectLevFSAP)
.postProcess()
// .expandNonterminalStubs(origCFG = this@intersectLevFSAP)
.also { println("Bar-Hillel construction took ${clock.elapsedNow()}") }
}

// For every production A → σ in P, for every (p, σ, q) ∈ Q × Σ × Q
// such that δ(p, σ) = q we have the production [p, A, q] → σ in P′.
fun CFG.unitProdRules(fsa: FSA): List<Pair<String, List<Σᐩ>>> =
// (unitProductions * fsa.nominalize().flattenedTriples)
// .filter { (_, σ: Σᐩ, arc) -> (arc.π2)(σ) }
// .map { (A, σ, arc) -> "[${arc.π1}~$A~${arc.π3}]" to listOf(σ) }
(unitProductions * fsa.Q).mapNotNull { (A, σ, arc) ->
if (arc.π2.startsWith("[!=]") && σ != arc.π2.drop(4)) {
// if ((bimap.UNITS[A]!! - arc.π2.drop(4)).isEmpty()) null
// else "[${arc.π1}~$A~${arc.π3}]" to listOf("<$A[!=]$σ>")
"[${arc.π1}~$A~${arc.π3}]" to listOf("<$A>")
}
else if (arc.π2.startsWith("[.*]")) "[${arc.π1}~$A~${arc.π3}]" to listOf("<$A>")
else if (arc.π2 == σ) "[${arc.π1}~$A~${arc.π3}]" to listOf(σ)
else null
}
(unitProductions * fsa.nominalize().flattenedTriples)
.filter { (_, σ: Σᐩ, arc) -> (arc.π2)(σ) }
.map { (A, σ, arc) -> "[${arc.π1}~$A~${arc.π3}]" to listOf(σ) }
// (unitProductions * fsa.Q).mapNotNull { (A, σ, arc) ->
// if (arc.π2.startsWith("[!=]") && σ != arc.π2.drop(4)) {
//// if ((bimap.UNITS[A]!! - arc.π2.drop(4)).isEmpty()) null
//// else "[${arc.π1}~$A~${arc.π3}]" to listOf("<$A[!=]$σ>")
// "[${arc.π1}~$A~${arc.π3}]" to listOf("<$A>")
// }
// else if (arc.π2.startsWith("[.*]")) "[${arc.π1}~$A~${arc.π3}]" to listOf("<$A>")
// else if (arc.π2 == σ) "[${arc.π1}~$A~${arc.π3}]" to listOf(σ)
// else null
// }

fun CFG.expandNonterminalStubs(origCFG: CFG) = flatMap {
// println("FM: $it / ${it.RHS.first()} / ${it.RHS.first().isNonterminalStub()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fun CFG.jvmIntersectLevFSAP(fsa: FSA,
.collect(Collectors.toSet())
.also { println("Eliminated ${totalProds - it.size} extra productions before normalization") }
.jvmPostProcess(clock)
.expandNonterminalStubs(origCFG = this@jvmIntersectLevFSAP)
// .expandNonterminalStubs(origCFG = this@jvmIntersectLevFSAP)
// .jdvpNew()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import net.jhoogland.jautomata.*
import net.jhoogland.jautomata.operations.Concatenation
import net.jhoogland.jautomata.semirings.RealSemiring
import java.io.File
import kotlin.random.Random
import kotlin.test.*
import kotlin.time.measureTimedValue

Expand Down Expand Up @@ -83,7 +82,8 @@ class WFSATest {
val toRepair = "NAME : NEWLINE NAME = STRING NEWLINE NAME = NAME . NAME ( STRING ) NEWLINE"
val groundTr = "+ NAME : True NEWLINE NAME = STRING NEWLINE NAME = NAME . NAME ( STRING ) NEWLINE"
val radius = 2
val pt = Grammars.seq2parsePythonCFG.makeLevPTree(toRepair, radius, shortS2PParikhMap)
val gram = Grammars.seq2parsePythonCFG.run { jvmIntersectLevFSAP(makeLevFSA(toRepair, radius), parikhMap) }
val pt = gram.toPTree()

println("Total trees: " + pt.totalTrees.toString())
val maxResults = 10_000
Expand Down

0 comments on commit 2cf1371

Please sign in to comment.