Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Right python parser for method label extraction #127

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/main/kotlin/astminer/cli/LabelExtractors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import astminer.parse.antlr.python.PythonMethodSplitter
import astminer.parse.cpp.FuzzyMethodSplitter
import astminer.parse.cpp.FuzzyNode
import astminer.parse.java.GumTreeJavaNode
import astminer.parse.java.GumTreeMethodSplitter
import astminer.parse.java.GumTreeJavaMethodSplitter
import astminer.parse.python.GumTreePythonMethodSplitter
import astminer.parse.python.GumTreePythonNode
import java.io.File


Expand Down Expand Up @@ -42,7 +44,8 @@ abstract class FileLabelExtractor : LabelExtractor {

abstract class MethodLabelExtractor(
open val filterPredicates: Collection<MethodFilterPredicate> = emptyList(),
open val javaParser: String = "gumtree"
open val javaParser: String = "gumtree",
open val pythonParser: String = "antlr"
) : LabelExtractor {

override fun toLabeledData(
Expand All @@ -61,7 +64,7 @@ abstract class MethodLabelExtractor(
"java" -> {
when (javaParser) {
"gumtree" -> {
val methodSplitter = GumTreeMethodSplitter()
val methodSplitter = GumTreeJavaMethodSplitter()
methodSplitter.splitIntoMethods(root as GumTreeJavaNode)
}
"antlr" -> {
Expand All @@ -74,8 +77,19 @@ abstract class MethodLabelExtractor(
}
}
"py" -> {
val methodSplitter = PythonMethodSplitter()
methodSplitter.splitIntoMethods(root as SimpleNode)
when (pythonParser) {
"gumtree" -> {
val methodSplitter = GumTreePythonMethodSplitter()
methodSplitter.splitIntoMethods(root as GumTreePythonNode)
}
"antlr" -> {
val methodSplitter = PythonMethodSplitter()
methodSplitter.splitIntoMethods(root as SimpleNode)
}
else -> {
throw UnsupportedOperationException("Unsupported parser $pythonParser")
}
}
}
"js" -> {
val methodSplitter = JavaScriptMethodSplitter()
Expand Down Expand Up @@ -111,8 +125,9 @@ class FolderExtractor : FileLabelExtractor() {
class MethodNameExtractor(
val hideMethodNames: Boolean = false,
override val filterPredicates: Collection<MethodFilterPredicate> = emptyList(),
override val javaParser: String = "gumtree"
) : MethodLabelExtractor(filterPredicates, javaParser) {
override val javaParser: String = "gumtree",
override val pythonParser: String = "antlr"
) : MethodLabelExtractor(filterPredicates, javaParser, pythonParser) {

override fun <T : Node> extractLabel(methodInfo: MethodInfo<T>, filePath: String): String? {
val methodNameNode = methodInfo.method.nameNode ?: return null
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/astminer/examples/AllJavaMethods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import astminer.common.model.LabeledPathContexts
import astminer.common.model.MethodInfo
import astminer.parse.java.GumTreeJavaNode
import astminer.parse.java.GumTreeJavaParser
import astminer.parse.java.GumTreeMethodSplitter
import astminer.parse.java.GumTreeJavaMethodSplitter
import astminer.paths.PathMiner
import astminer.paths.PathRetrievalSettings
import astminer.paths.CsvPathStorage
Expand Down Expand Up @@ -34,7 +34,7 @@ fun allJavaMethods() {
val fileNode = GumTreeJavaParser().parseInputStream(file.inputStream()) ?: return@forFilesWithSuffix

//extract method nodes
val methodNodes = GumTreeMethodSplitter().splitIntoMethods(fileNode)
val methodNodes = GumTreeJavaMethodSplitter().splitIntoMethods(fileNode)

methodNodes.forEach { methodInfo ->
//Retrieve paths from every node individually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import astminer.common.preOrder

private fun GumTreeJavaNode.isTypeNode() = getTypeLabel().endsWith("Type")

class GumTreeMethodSplitter : TreeMethodSplitter<GumTreeJavaNode> {
class GumTreeJavaMethodSplitter : TreeMethodSplitter<GumTreeJavaNode> {

companion object {
private object TypeLabels {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ private fun createTree(filename: String): GumTreeJavaNode {
}

private fun createAndSplitTree(filename: String): Collection<MethodInfo<GumTreeJavaNode>> {
return GumTreeMethodSplitter().splitIntoMethods(createTree(filename))
return GumTreeJavaMethodSplitter().splitIntoMethods(createTree(filename))
}

class GumTreeMethodSplitterTest {
class GumTreeJavaMethodSplitterTest {
@Test
fun testMethodExtraction1() {
val methodInfos = createAndSplitTree("src/test/resources/gumTreeMethodSplitter/1.java")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class GumTreeMethodSplitterTest {
class GumTreeJavaMethodSplitterTest {
private fun parse(filename: String): GumTreePythonNode? =
GumTreePythonParser().parseInputStream(File(filename).inputStream())

Expand Down