@@ -102,31 +102,6 @@ sealed class GRE(open vararg val args: GRE) {
102102 is CAT -> (l.dv(σ) * r).let { dl -> if (l.nullable) dl + r.dv(σ) else dl }
103103 }
104104
105- // fun dv(σ: Int): GRE? = when (this) {
106- // is EPS -> null // ∂_σ(ε) = ∅
107- // is SET -> if (s[σ]) EPS() else null // ∂_σ({a}) = ε if σ = a, else ∅
108- // is CUP -> {
109- // val derivatives = args.mapNotNull { it.dv(σ) }
110- // if (derivatives.isEmpty()) null
111- // else derivatives.reduce { acc, next -> CUP(acc, next) }
112- // }
113- // is CAT -> {
114- // val dl = l.dv(σ) // Left derivative
115- // val leftPart = dl?.let { CAT(it, r) }
116- // if (l.nullable) {
117- // val dr = r.dv(σ) // Right derivative
118- // when {
119- // leftPart != null && dr != null -> CUP(leftPart, dr)
120- // leftPart != null -> leftPart
121- // dr != null -> dr
122- // else -> null
123- // }
124- // } else {
125- // leftPart
126- // }
127- // }
128- // }
129-
130105 val nullable by lazy { isNullable() }
131106
132107 // Check whether 'g' accepts the empty string ε.
@@ -140,86 +115,6 @@ sealed class GRE(open vararg val args: GRE) {
140115 operator fun plus (g : GRE ): GRE = CUP (this , g)
141116 operator fun times (g : GRE ): GRE = CAT (this , g)
142117
143- fun toDOTGraph (rankDir : String = "TB ", font : String = "Helvetica ", dedupLeaves : Boolean = true): String {
144- fun Int.toUnicodeSubscript (): String = when (this ) {
145- 0 -> " \u2080 "
146- 1 -> " \u2081 "
147- 2 -> " \u2082 "
148- 3 -> " \u2083 "
149- 4 -> " \u2084 "
150- 5 -> " \u2085 "
151- 6 -> " \u2086 "
152- 7 -> " \u2087 "
153- 8 -> " \u2088 "
154- 9 -> " \u2089 "
155- else -> throw IllegalArgumentException (" Input must be between 0 and 9" )
156- }
157-
158- fun KBitSet.labelize (): String =
159- (0 until n).mapNotNull { if (this [it]) " Σ${it.toUnicodeSubscript()} " else null }.joinToString(" ," , " {" , " }" )
160-
161- data class Key (val kind : String , val payload : String )
162-
163- val nodeId = mutableMapOf<Int , String >()
164- val nodeDecl = StringBuilder ()
165- val edgeDecl = StringBuilder ()
166- var nextId = 0
167-
168- fun newNodeId () = " n${nextId++ } "
169-
170- var i = 0
171- fun declareNodeA (label : String , shape : String = "circle", style : String = ", style=\"rounded\"", extra : String = ""): String =
172- nodeId.getOrPut(i++ ) {
173- val id = newNodeId()
174- nodeDecl.append(" $id [label=\" $label \" , shape=$shape $style $extra ];\n " )
175- id
176- }
177-
178- fun declareNodeB (key : Key , label : String , shape : String = "circle"): String =
179- nodeId.getOrPut(i++ ) {
180- val id = newNodeId()
181- nodeDecl.append(" $id [label=\" $label \" , shape=$shape , style=\" rounded\" ];\n " )
182- id
183- }
184-
185- fun visit (g : GRE ): String = when (g) {
186- is EPS -> declareNodeB(Key (" EPS" , " " ), " ε" , " plaintext" )
187-
188- is SET -> declareNodeA(g.s.labelize(), " box" , extra = " , width=0.5" )
189-
190- is CUP -> {
191- if (! isLeafCup()) {
192- val id = declareNodeB(Key (" CUP${g.hash()} " , " " ), " ∨" )
193- for (child in g.args) { edgeDecl.append(" $id -> ${visit(child)} ;\n " ) }
194- id
195- } else {
196- val q = g.toSet() as SET
197- val key = if (dedupLeaves) Key (" SET" , q.s.toString())
198- else Key (" SET${g.hashCode()} " , " " )
199- declareNodeB(key, q.s.labelize(), " box" )
200- }
201- }
202-
203- is CAT -> {
204- val id = declareNodeA(" ·" , " invhouse" , " ," , " width=0.5" )
205- val lId = visit(g.l); edgeDecl.append(" $id -> $lId ;\n " )
206- val rId = visit(g.r); edgeDecl.append(" $id -> $rId ;\n " )
207- id
208- }
209- }
210-
211- visit(this )
212-
213- return buildString {
214- appendLine(" strict digraph GRE {" )
215- appendLine(" rankdir=$rankDir ;" )
216- appendLine(" node [order=out];" )
217- append(nodeDecl)
218- append(edgeDecl)
219- appendLine(" }" )
220- }
221- }
222-
223118 fun flatunion (): GRE =
224119 if (this is CUP && args.all { it is CUP }) CUP (* args.flatMap { it.args.toList() }.toTypedArray())
225120 else this
@@ -286,7 +181,7 @@ fun repairWithGREAtDist(brokenStr: List<Σᐩ>, cfg: CFG, d: Int): Pair<GRE.CUP,
286181 val bindex = cfg.bindex
287182 val width = cfg.nonterminals.size
288183 val vindex = cfg.vindex
289- val ups = cfg.unitProductions
184+ val ups = cfg.grpUPs
290185 val t2vs = cfg.tmToVidx
291186 val maxBranch = vindex.maxOf { it.size }
292187 val startIdx = bindex[START_SYMBOL ]
@@ -353,7 +248,7 @@ fun repairWithGREAtDist(brokenStr: List<Σᐩ>, cfg: CFG, d: Int): Pair<GRE.CUP,
353248 .apply { s.set(tmm[σ]!! )/* ; dq[p][q].set(Aidx)*/ }
354249
355250 var maxChildren = 0
356- var location = - 1 to - 1
251+ // var location = -1 to -1
357252
358253 // 3) CYK + Floyd Warshall parsing
359254 for (dist in 1 .. < nStates) {
@@ -380,7 +275,7 @@ fun repairWithGREAtDist(brokenStr: List<Σᐩ>, cfg: CFG, d: Int): Pair<GRE.CUP,
380275 if (rhsPairs.isNotEmpty()) {
381276 if (list.size > maxChildren) {
382277 maxChildren = list.size
383- location = p to q
278+ // location = p to q
384279 }
385280 dp[p][q][Aidx ] = if (list.size == 1 ) list.first() else GRE .CUP (* list)
386281 }
@@ -403,7 +298,7 @@ fun repairWithGRE(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
403298 val bindex = cfg.bindex
404299 val width = cfg.nonterminals.size
405300 val vindex = cfg.vindex
406- val ups = cfg.unitProductions
301+ val ups = cfg.grpUPs
407302 val t2vs = cfg.tmToVidx
408303 val maxBranch = vindex.maxOf { it.size }
409304 val startIdx = bindex[START_SYMBOL ]
@@ -470,7 +365,7 @@ fun repairWithGRE(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
470365 .apply { s.set(tmm[σ]!! )/* ; dq[p][q].set(Aidx)*/ }
471366
472367 var maxChildren = 0
473- var location = - 1 to - 1
368+ // var location = -1 to -1
474369
475370 // 3) CYK + Floyd Warshall parsing
476371 for (dist in 1 .. < nStates) {
@@ -497,7 +392,7 @@ fun repairWithGRE(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
497392 if (rhsPairs.isNotEmpty()) {
498393 if (list.size > maxChildren) {
499394 maxChildren = list.size
500- location = p to q
395+ // location = p to q
501396 }
502397 dp[p][q][Aidx ] = if (list.size == 1 ) list.first() else GRE .CUP (* list)
503398 }
@@ -529,7 +424,7 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
529424 val bindex = cfg.bindex
530425 val width = cfg.nonterminals.size
531426 val vindex = cfg.vindex
532- val ups = cfg.unitProductions
427+ val ups = cfg.grpUPs
533428 val t2vs = cfg.tmToVidx
534429 val maxBranch = vindex.maxOf { it.size }
535430 val startIdx = bindex[START_SYMBOL ]
@@ -598,7 +493,7 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
598493 .apply { pause(); s.set(tmm[σ]!! )/* ; dq[p][q].set(Aidx)*/ }
599494
600495 var maxChildren = 0
601- var location = - 1 to - 1
496+ // var location = -1 to -1
602497
603498 // 3) CYK + Floyd Warshall parsing
604499 for (dist in 1 until nStates) {
@@ -626,7 +521,7 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
626521 if (rhsPairs.isNotEmpty()) {
627522 if (list.size > maxChildren) {
628523 maxChildren = list.size
629- location = p to q
524+ // location = p to q
630525 }
631526 dp[p][q][Aidx ] = if (list.size == 1 ) list.first() else GRE .CUP (* list)
632527 }
@@ -640,7 +535,8 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
640535 val allParses = levFSA.finalIdxs.mapNotNull { q -> dp[0 ][q][startIdx] }
641536
642537 println (" Parsing took ${timer.elapsedNow()} with |σ|=${brokenStr.size} , " +
643- " |Q|=$nStates , |G|=${cfg.size} , maxBranch=$maxBranch , |V|=$width , |Σ|=$tms , maxChildren=$maxChildren @$location " )
538+ // "|Q|=$nStates, |G|=${cfg.size}, maxBranch=$maxBranch, |V|=$width, |Σ|=$tms, maxChildren=$maxChildren@$location")
539+ " |Q|=$nStates , |G|=${cfg.size} , maxBranch=$maxBranch , |V|=$width , |Σ|=$tms " )
644540 // 5) Combine them under a single GRE
645541 return if (allParses.isEmpty()) null else GRE .CUP (* allParses.toTypedArray())
646542}
0 commit comments