Skip to content

Commit

Permalink
prune top left and bottom right of L-NFA when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 26, 2024
1 parent 19322b3 commit ab5d42a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ fun pruneInactiveRules(cfg: CFG): CFG =
TODO("Identify and prune all nonterminals t generating" +
"a finite language rooted at t and disjoint from the upward closure.")

fun CFG.maxParsableFragment(tokens: List<String>, padRight: Int = 3): Int =
(1..tokens.size).first { i ->
val blocked = tokens.mapIndexed { j, t -> if (j < i) t else "_" } + List(padRight) { "_" }
fun CFG.maxParsableFragment(tokens: List<String>, pad: Int = 3): Pair<Int, Int> =
((1..tokens.size).firstOrNull { i ->
val blocked =
tokens.mapIndexed { j, t -> if (j < i) t else "_" } + List(pad) { "_" }
// println(blocked)
blocked !in language
}
} ?: tokens.size) to ((2..tokens.size).firstOrNull { i ->
val blocked = List(pad) { "_" } +
tokens.mapIndexed { j, t -> if (tokens.size - i < j) t else "_" }
// println(blocked)
blocked !in language
}?.let { tokens.size - it } ?: 0)

// REL ⊂ CFL ⊂ CJL
operator fun REL.contains(s: Σᐩ): Bln = s in reg.asCFG.language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun makeLevFSA(
str: List<Σᐩ>,
dist: Int,
digits: Int = (str.size * dist).toString().length,
lastGoodIndex: Int = str.size
bounds: Pair<Int, Int> = str.size to 0
): FSA =
(upArcs(str, dist, digits) +
diagArcs(str, dist, digits) +
Expand All @@ -71,12 +71,12 @@ fun makeLevFSA(
.also {
println("Levenshtein-${str.size}x$dist automaton had ${it.size} arcs initially!")
}.filter { arc ->
arc.first.unpackCoordinates().let { (i, j) -> 0 < j || i <= lastGoodIndex + 1 }
listOf(arc.first.unpackCoordinates(), arc.third.unpackCoordinates())
.all { (i, j) -> (0 < j || i <= bounds.first) && (j < dist || i >= bounds.second - 2) }
}
.let { Q ->
val initialStates = setOf("q_" + pd(0, digits).let { "$it/$it" })


val finalStates = mutableSetOf<String>()
Q.states.forEach {
val (i, j) = it.unpackCoordinates()
Expand Down

0 comments on commit ab5d42a

Please sign in to comment.