Skip to content

Commit

Permalink
speed up slow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Apr 29, 2024
1 parent e453210 commit 89591fa
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import kotlin.time.TimeSource

infix fun FSA.intersectLevFSA(cfg: CFG) = cfg.intersectLevFSA(this)

infix fun CFG.intersectLevFSA(fsa: FSA): CFG = intersectLevFSAP(fsa)
fun CFG.intersectLevFSA(fsa: FSA, parikhMap: ParikhMap = this.parikhMap): CFG =
intersectLevFSAP(fsa, parikhMap)
// subgrammar(fsa.alphabet)
// .also { it.forEach { println("${it.LHS} -> ${it.RHS.joinToString(" ")}") } }

Expand All @@ -26,7 +27,7 @@ fun CFG.barHillelRepair(prompt: List<Σᐩ>, distance: Int) =

// http://www.cs.umd.edu/~gasarch/BLOGPAPERS/cfg.pdf#page=2
// https://browse.arxiv.org/pdf/2209.06809.pdf#page=5
private infix fun CFG.intersectLevFSAP(fsa: FSA): CFG {
private fun CFG.intersectLevFSAP(fsa: FSA, parikhMap: ParikhMap = this.parikhMap): CFG {
var clock = TimeSource.Monotonic.markNow()
val nts = mutableSetOf("START")
fun Σᐩ.isSyntheticNT() =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.hypergraph.kaliningraph.parsing

import Grammars
import Grammars.shortS2PParikhMap
import ai.hypergraph.kaliningraph.*
import ai.hypergraph.kaliningraph.automata.*
import kotlin.test.*
Expand Down Expand Up @@ -319,7 +320,7 @@ class BarHillelTest {
println(levBall.states.size)
// println(levBall.toDot())
// throw Exception("")
val intGram = gram.intersectLevFSA(levBall)
val intGram = gram.intersectLevFSA(levBall, shortS2PParikhMap)

val clock = TimeSource.Monotonic.markNow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ object Grammars {
T -> [ Q ]
""".trimIndent().parseCFG().noNonterminalStubs

val shortS2PParikhMap by lazy { ParikhMap(seq2parsePythonCFG, 20) }
val seq2parsePythonCFG: CFG = """
START -> Stmts_Or_Newlines
Stmts_Or_Newlines -> Stmt_Or_Newline | Stmt_Or_Newline Stmts_Or_Newlines
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.hypergraph.kaliningraph.automata

import Grammars
import Grammars.shortS2PParikhMap
import ai.hypergraph.kaliningraph.parsing.*
import net.jhoogland.jautomata.*
import net.jhoogland.jautomata.Automaton
Expand Down Expand Up @@ -38,7 +39,7 @@ class WFSATest {
fun testLBHRepair() {
val toRepair = "NAME : NEWLINE NAME = STRING NEWLINE NAME = NAME . NAME ( STRING ) NEWLINE"
val radius = 1
val pt = Grammars.seq2parsePythonCFG.makeLevPTree(toRepair, radius)
val pt = Grammars.seq2parsePythonCFG.makeLevPTree(toRepair, radius, shortS2PParikhMap)
val repairs = pt.sampleStrWithoutReplacement().distinct().take(100).toSet()
println("Found ${repairs.size} repairs by enumerating PTree")
measureTimedValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.hypergraph.kaliningraph.repair

import Grammars
import Grammars.shortS2PParikhMap
import ai.hypergraph.kaliningraph.parsing.*
import ai.hypergraph.kaliningraph.tokenizeByWhitespace
import ai.hypergraph.markovian.*
Expand Down Expand Up @@ -115,7 +116,7 @@ class ProbabilisticLBH {

val clock = TimeSource.Monotonic.markNow()
val levBall = makeLevFSA(source.tokenizeByWhitespace(), levDist)
val intGram = gram.jvmIntersectLevFSA(levBall)
val intGram = gram.jvmIntersectLevFSA(levBall, shortS2PParikhMap)
println("Finished ${intGram.size}-prod ∩-grammar in ${clock.elapsedNow()}")
val lbhSet = intGram.toPTree().sampleDirectlyWOR()
.takeWhile { clock.elapsedNow().inWholeSeconds < 30 }.collect(Collectors.toSet())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ class SATValiantTest {
""".trimIndent().parseCFG()

/*
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sat.SATValiantTest.testLevensheteinIntersection"
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sat.SATValiantTest.testLevenshteinIntersection"
*/
@Test
fun testLevensheteinIntersection() {
fun testLevenshteinIntersection() {
val cfg = sumCFG.noNonterminalStubs
val strWithParseErr = "1 + 2 + + +".tokenizeByWhitespace()
val dist = 2
Expand Down

0 comments on commit 89591fa

Please sign in to comment.