Skip to content

Commit

Permalink
fix escape character
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Jun 30, 2024
1 parent 4a4f5b0 commit 933e5fd
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typealias CFG = Set<Production>
val Production.LHS: Σᐩ get() = first
val Production.RHS: List<Σᐩ> get() = second
// Not sure why this was added, but we don't have time for it in production
// second.let { if (it.size == 1) it.map(Σᐩ::stripEscapeChars) else it }
// second.let { if (it.size == 1 && 2 < it.first().length && it.first().first() == '`') it.map(Σᐩ::stripEscapeChars) else it }

/**
* "Freezes" the enclosed CFG, making it immutable and caching its hashCode for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,6 @@ private fun pd(i: Int, digits: Int) = i.toString().padStart(digits, '0')
(q_i,j−1 -s→ q_i,j)∈δ
*/

/*
Precision@All
=============
|σ|∈[0, 10): Top-1/total: 28 / 28 = 1.0
|σ|∈[10, 20): Top-1/total: 41 / 41 = 1.0
|σ|∈[20, 30): Top-1/total: 45 / 46 = 0.9782608695652174
|σ|∈[30, 40): Top-1/total: 41 / 41 = 1.0
|σ|∈[40, 50): Top-1/total: 9 / 11 = 0.8181818181818182
Δ(1)= Top-1/total: 57 / 58 = 0.9827586206896551
Δ(2)= Top-1/total: 57 / 58 = 0.9827586206896551
Δ(3)= Top-1/total: 50 / 51 = 0.9803921568627451
(|σ|∈[0, 10), Δ=1): Top-1/total: 11 / 11 = 1.0
(|σ|∈[0, 10), Δ=2): Top-1/total: 11 / 11 = 1.0
(|σ|∈[0, 10), Δ=3): Top-1/total: 6 / 6 = 1.0
(|σ|∈[10, 20), Δ=1): Top-1/total: 12 / 12 = 1.0
(|σ|∈[10, 20), Δ=2): Top-1/total: 11 / 11 = 1.0
(|σ|∈[10, 20), Δ=3): Top-1/total: 18 / 18 = 1.0
(|σ|∈[20, 30), Δ=1): Top-1/total: 18 / 18 = 1.0
(|σ|∈[20, 30), Δ=2): Top-1/total: 13 / 13 = 1.0
(|σ|∈[20, 30), Δ=3): Top-1/total: 14 / 15 = 0.9333333333333333
(|σ|∈[30, 40), Δ=1): Top-1/total: 11 / 11 = 1.0
(|σ|∈[30, 40), Δ=2): Top-1/total: 19 / 19 = 1.0
(|σ|∈[30, 40), Δ=3): Top-1/total: 11 / 11 = 1.0
(|σ|∈[40, 50), Δ=1): Top-1/total: 5 / 6 = 0.8333333333333334
(|σ|∈[40, 50), Δ=2): Top-1/total: 3 / 4 = 0.75
(|σ|∈[40, 50), Δ=3): Top-1/total: 1 / 1 = 1.0
*/

fun upArcs(str: List<Σᐩ>, dist: Int, digits: Int): TSA =
((0..str.size).toSet() * (1..dist).toSet())
// .filter { (i, _, s) -> str.size <= i || str[i] != s }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fun CFG.normalize(): CFG =
mutableListOf<CFG>().let { rewrites ->
addGlobalStartSymbol()
.expandOr()
.unescape()
.also { rewrites.add(it) } /** [originalForm] */
.eliminateParametricityFromLHS()
.also { rewrites.add(it) } /** [nonparametricForm] */
Expand Down Expand Up @@ -109,6 +110,9 @@ fun CFG.expandOr(): CFG =
}.map { prod.LHS to it }
}.toSet()

fun CFG.unescape(): CFG =
map { (l, r) -> l to r.map { it.stripEscapeChars() } }.toSet()

// Adds V -> εV | Vε to every unit production [V -> v] in CFG
// so that holes can be [optionally] elided by the SAT solver.
private fun CFG.addEpsilonProduction(): CFG =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ fun Σᐩ.parseCFG(
line.splitProd().let { it[0] to it[1].tokenizeByWhitespace() }
}.toSet().let { if (normalize) it.normalForm else it }

fun Σᐩ.stripEscapeChars(escapeSeq: Σᐩ = "`"): Σᐩ = replace(escapeSeq, "")
fun Σᐩ.stripEscapeChars(c: Char = '`'): Σᐩ =
if (first() == c && last() == c) drop(1).dropLast(1) else this

val PRODCFG = Regex("\\s*[^|]+\\s+->\\s+([^|]+\\s+\\|\\s+)*[^|]+\\s*")
fun Σᐩ.isValidProd() = lines().filter { "->" in it }.all { l -> l.matches(PRODCFG) }
Expand Down

0 comments on commit 933e5fd

Please sign in to comment.