I want to display a graph for a parsed sentence using graphviz. So I have a class in which the sentence is parsed and returns a Png file:
fun createDependencyTree(sentence: String): File {
// Load the parser model
val lp = LexicalizedParser.loadModel("spanishPCFG.ser.gz")
// Tokenize the sentence
val tokenizerFactory: TokenizerFactory<CoreLabel> =
PTBTokenizer.factory(CoreLabelTokenFactory(), "")
val wordList = tokenizerFactory.getTokenizer(StringReader(sentence)).tokenize()
// Parse the sentence
val parseTree = lp.apply(wordList)
// Get grammatical structure and typed dependencies
val tlp = PennTreebankLanguagePack()
val gsf: GrammaticalStructureFactory = tlp.grammaticalStructureFactory()
val gs = gsf.newGrammaticalStructure(parseTree)
val dependencies: Collection<TypedDependency> = gs.typedDependenciesCCprocessed(true)
// Generate DOT format from dependencies
val dotGraph = dependencies.joinToString("\n") { it.toString() }
// Save DOT format to a file
val dotFile = File("dependency_tree.dot")
dotFile.writeText(dotGraph)
// Convert DOT to PNG using Graphviz
val pngFile = File("dependency_tree.png")
Graphviz.fromFile(dotFile)
.render(Format.PNG)
.toFile(pngFile)
return pngFile
}
Then, I've got a JetpackCompose clas in which the pngFile is displayed in a column:
@Composable
fun secondScreenText(navController: NavHostController, text : String?) {
val scaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
topBar = {
TopAppBar(
title = { },
backgroundColor = Color(0xFFFFFFFF),
contentColor = Color(0xFF000000),
elevation = 0.dp,
navigationIcon = {
IconButton(onClick = {
if (text!! != "") {
navController.popBackStack()
navController.navigate(AppScreens.FirstScreen.route)
}
}) {
Icon(Icons.Default.ArrowBackIos, contentDescription = null)
}
})
}
) {
Box(
contentAlignment = Alignment.CenterStart, modifier = Modifier
.fillMaxSize()
.background(Color(0xFFFFFFFF))
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceEvenly,
modifier = Modifier
.fillMaxSize()
.padding(start = 40.dp, end = 40.dp, top = 40.dp)
) {
Text(
text = "Análisis",
fontFamily = Inter,
fontSize = 28.sp,
modifier = Modifier.weight(0.25f)
)
Box(
modifier = Modifier
.fillMaxSize()
.background(Color(0XFFF0F0F0), shape = RoundedCornerShape(25.dp))
.weight(1f)
.padding(start = 20.dp, end = 20.dp, top = 20.dp),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
AutoResizedText(text = text!!, modifier = Modifier.weight(1f), maxLines =
val pngFile = createDependencyTree(text)
Image(
painter = rememberImagePainter(data = pngFile.toURI().toURL()),
contentDescription = null,
modifier = Modifier.size(200.dp)
)
}
}
}
}
}
}
The problem I've got is that when opening the app and parsing the sentence, instead of displaying the png it crashes with this error
java.lang.IllegalArgumentException: No head rule defined for sentence using class edu.stanford.nlp.trees.UniversalSemanticHeadFinder in (sentence...)
I would appreciate any help.