Skip to content

Commit

Permalink
first kotlin example
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 10, 2023
1 parent 9cb6496 commit 84ec91e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.io.File
import kotlin.time.*

/*
./gradlew kotlinStatementRepair 2>&1 | grep -v "Parser error:" | grep -v "LATEX"
./gradlew kotlinStatementRepair 2>&1 | grep -v "Parser error:"
*/

Expand Down Expand Up @@ -41,10 +42,12 @@ fun main() {

println("\nTop 100 repairs:\n")
it.take(100).forEach {
println("Δ=${levenshtein(prompt, it) - 1} repair: ${prettyDiffNoFrills(prompt, it)}")
println("Δ=${levenshtein(prompt, it)} repair: ${prettyDiffNoFrills(prompt, it)}")
println("(LATEX) Δ=${levenshtein(prompt, it)} repair: ${latexDiffSingleLOC(prompt, it)}")
}

println("Found ${it.size} valid repairs in ${elapsed}ms, or roughly ${it.size / (elapsed/1000.0)} repairs per second.")
println("Found ${it.size} valid repairs in ${elapsed}ms, or roughly " +
"${(it.size / (elapsed/1000.0)).toString().take(5)} repairs per second.")
println("Original string was ${if (contained) "#${it.indexOf(original)}" else "NOT"} in repair proposals!\n")
}
}
Expand Down Expand Up @@ -109,6 +112,7 @@ fun parallelRepairKotlinStatement(
val levDiff = levenshtein(prompt, it) - 1
if (levDiff < bestRepair) {
println("Δ=$levDiff repair: ${prettyDiffNoFrills(prompt, it)}")
println("(LATEX) Δ=$levDiff repair: ${latexDiffSingleLOC(prompt, it)}")
bestRepair = levDiff
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fun repairPythonStatement(
coarsen = String::coarsenAsPython,
uncoarsen = String::uncoarsenAsPython,
synthesizer = satRepair(clock), // Enumerative search
diagnostic = { println("Δ=${ levenshtein( prompt, it ) - 1 } repair: ${prettyDiffNoFrills(prompt, it)}") },
diagnostic = { println("Δ=${levenshtein(prompt, it) - 1} repair: ${prettyDiffNoFrills(prompt, it)}") },
filter = { isValidPython() },
)

Expand Down
28 changes: 22 additions & 6 deletions src/main/kotlin/edu/mcgill/cstk/utils/StringUtils.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package edu.mcgill.cstk.utils

import ai.hypergraph.kaliningraph.parsing.tokenizeByWhitespace
import ai.hypergraph.kaliningraph.types.cc
import com.github.difflib.text.*
import com.github.difflib.text.DiffRow.Tag.*
import edu.mcgill.cstk.disk.*
import edu.mcgill.cstk.experiments.probing.embeddingServer
import edu.mcgill.cstk.experiments.repair.isValidKotlin
import edu.mcgill.cstk.experiments.repair.defaultTokenizer
import info.debatty.java.stringsimilarity.interfaces.MetricStringDistance
import me.vovak.antlr.parser.*
import net.sf.extjwnl.data.PointerUtils.*
import net.sf.extjwnl.dictionary.Dictionary
import org.antlr.v4.runtime.*
import org.apache.commons.lang3.StringUtils
import org.jetbrains.kotlin.lexer.*
import org.jetbrains.kotlin.spec.grammar.tools.tokenizeKotlinCode
import spoon.Launcher
import java.io.File
import java.net.*
Expand Down Expand Up @@ -339,18 +339,18 @@ $summary
\subsection{Original}
\begin{lstlisting}[language=java]
${diffString(original, synthetic).first}
${latexDiffMultilineStrings(original, synthetic).first}
\end{lstlisting}
\subsection{Synthetic}
\begin{lstlisting}[language=java]
${diffString(original, synthetic).second}
${latexDiffMultilineStrings(original, synthetic).second}
\end{lstlisting}
\subsection{Variant}
\begin{lstlisting}[language=java]
${diffString(original, variant).second}
${latexDiffMultilineStrings(original, variant).second}
\end{lstlisting}
\subsection{Comment}
Expand All @@ -366,7 +366,7 @@ $discrepancy
%--------
""".trimIndent().also { println(it) }

fun diffString(old: String, new: String) =
fun latexDiffMultilineStrings(old: String, new: String) =
DiffRowGenerator.create()
.showInlineDiffs(true)
.ignoreWhiteSpaces(true)
Expand Down Expand Up @@ -415,6 +415,22 @@ fun String.visibleLen() =
.replace(ANSI_GREEN_BACKGROUND,"")
.replace(ANSI_RESET,"").length

fun latexDiffSingleLOC(original: String, new: String) =
DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.newTag { l -> if(l) "(*@<begin>" else "<end>@*)" }
.build()
.generateDiffRows(original.tokenizeByWhitespace(), new.tokenizeByWhitespace())
.joinToString(" ") {
when (it.tag) {
INSERT -> it.newLine.replace("<begin>", "\\hlgreen{").replace("<end>", "}")
CHANGE -> it.newLine.replace("<begin>", "\\hlorange{").replace("<end>", "}")
DELETE -> "\\hlred{${List(it.oldLine.length){ " " }.joinToString("")}}"
else -> it.newLine.replace("<begin>", "").replace("<end>", "")
}
}.replace("&lt;", "<").replace("&gt;", ">")

// Just print the new line with ASCII colors but no border
fun prettyDiffNoFrills(original: String, new: String) =
DiffRowGenerator.create()
Expand Down

0 comments on commit 84ec91e

Please sign in to comment.