Plots constituency trees and dependency trees given by strings.
The code uses both the id-tree crate and the plotters crate. Primarily written for inputs like parsed syntactic trees, but can serve other inputs, such as mathematical expressions etc. The program first transforms the input to a conll / tree, then plots the structure, recursively. It is mostly suitable for short parsed sequences of up to 15-20 tokens. The program is a simple drawing program, plots strings that are already parsed. This is not a parser! I wrote this in order to get familiar with Rust and decided to upload it if it can help others.
See an example below.
The following example shows how to use the API in order to produce a png from a single parsed constituency string.
``` // Example parsed sentence: // (S (NP (det The) (N people)) (VP (V watch) (NP (det the) (N game))))
use parsedtoplot::Config; use parsedtoplot::String2Tree; use parsedtoplot::Tree2Plot; use parsedtoplot::String2StructureBuilder; use parsedtoplot::Structure2PlotBuilder;
let mut constituency = String::from("(S (NP (det The) (N people)) (VP (V watch) (NP (det the) (N game))))"); let mut string2tree: String2Tree = String2StructureBuilder::new(); string2tree.build(&mut constituency).unwrap(); // build the tree from the string let tree = string2tree.get_structure();
// build plot from tree and save Config::makeoutdir(&"Output".tostring()).unwrap(); let saveto: &str = "Output/constituencyplot.png"; let mut tree2plot: Tree2Plot = Structure2PlotBuilder::new(tree); tree2plot.build(saveto); ```
The following example shows how to use the API in order to produce a png from a single conll format.
``` // 0 The the det _ _ 1 det _ _ // 1 people people NOUN _ _ 2 nsubj _ _ // 2 watch watch VERB _ _ 2 ROOT _ _ // 3 the the DET _ _ 4 det _ _ // 4 game game NOUN _ _ 2 dobj _ _
use parsedtoplot::Config; use parsedtoplot::String2Conll; use parsedtoplot::Conll2Plot; use parsedtoplot::String2StructureBuilder; use parsedtoplot::Structure2PlotBuilder;
let mut dependency = [ "0 The the DET _ _ 1 det _ ", "1 people people NOUN _ _ 2 nsubj _ _", "2 watch watch VERB _ _ 2 ROOT _ _", "3 the the DET _ _ 4 det _ _", "4 game game NOUN _ _ 2 dobj _ _" ].map(|x| x.tostring()).to_vec();
let mut string2conll: String2Conll = String2StructureBuilder::new(); string2conll.build(&mut dependency).unwrap(); // build the conll from the vector of strings let conll = string2conll.get_structure();
// build plot from conll and save Config::makeoutdir(&"Output".tostring()).unwrap(); let saveto: &str = "Output/dependencyplot.png"; let mut conll2plot: Conll2Plot = Structure2PlotBuilder::new(conll); conll2plot.build(saveto); ```
You can use multiple inputs of the same type in a file, through the command-line, as follows:
cargo run INPUT_TYPE INPUT_FILE OUTPUT_PATH
when: * INPUTTYPE should be replaced with "c" for constituency or "d" for dependency. * INPUTFILE should be replaced with a path to a txt file with inputs. * OUTPUT_PATH should be replaced with a path to a requested output dir.
For example:
cargo run c constituencies.txt Output
Will save png images of constituency trees drawn for the inputs in constituencies.txt, in an Output dir.
``` use parsedtoplot::Config; use parsedtoplot::String2Tree; use parsedtoplot::Tree2Plot; use parsedtoplot::String2StructureBuilder; use parsedtoplot::Structure2PlotBuilder; use std::env;
// collect arguments from command line
let args: Vec
// run configuration protocol and inspectations
let sequences = match Config::new(&args) {
Ok(sequences) => Vec::
for (i, mut constituency) in sequences.into_iter().enumerate() {
println!("working on input number {} ...", i);
let save_to = &Config::get_out_file(&args[3], i.to_string().as_str());
// build tree from consituency
let mut string2tree: String2Tree = String2StructureBuilder::new();
string2tree.build(&mut constituency).unwrap();
let tree = string2tree.get_structure();
// build plot from tree
let mut tree2plot: Tree2Plot = Structure2PlotBuilder::new(tree);
tree2plot.build(save_to);
} ```
``` use parsedtoplot::Config; use parsedtoplot::String2Conll; use parsedtoplot::Conll2Plot; use parsedtoplot::String2StructureBuilder; use parsedtoplot::Structure2PlotBuilder; use std::env;
// collect arguments from command line
let args: Vec
// run configuration protocol and inspectations
let sequences = match Config::new(&args) {
Ok(sequences) => Vec::
for (i, mut dependency) in sequences.into_iter().enumerate() {
println!("working on input number {} ...", i);
let save_to = &Config::get_out_file(&args[3], i.to_string().as_str());
// build conll from string
let mut string2conll: String2Conll = String2StructureBuilder::new();
string2conll.build(&mut dependency).unwrap();
let conll = string2conll.get_structure();
// build plot from conll
let mut conll2plot: Conll2Plot = Structure2PlotBuilder::new(conll);
conll2plot.build(save_to);
} ```
Under MIT license.