[][src]Trait lumberjack::TreeOps

pub trait TreeOps {
    fn annotate_parent_tag(&mut self, feature_name: &str) -> Result<(), Error>;
fn insert_intermediate<F>(&mut self, match_fn: F) -> Result<(), Error>
    where
        F: Fn(&Tree, NodeIndex) -> Option<String>
;
fn filter_nonterminals<F>(&mut self, match_fn: F) -> Result<(), Error>
    where
        F: Fn(&Tree, NodeIndex) -> bool
;
fn reattach_terminals<F>(&mut self, attachment: NodeIndex, match_fn: F)
    where
        F: Fn(&Tree, NodeIndex) -> bool
; }

Trait specifying methods to modify trees.

Required methods

fn annotate_parent_tag(&mut self, feature_name: &str) -> Result<(), Error>

Annotate the parent tag as a feature.

Annotates the tag of each terminal's parent as a feature.

Returns Error if the tree contains Terminals without a parent node.

fn insert_intermediate<F>(&mut self, match_fn: F) -> Result<(), Error> where
    F: Fn(&Tree, NodeIndex) -> Option<String>, 

Insert an intermediate node above terminals.

If a terminal is not dominated by a node with label matched by tag_set a new non-terminal node is inserted above with a specified label. Runs of terminals whose parent node is not matched by tag_set are collected under a single new node.

Returns Error if the tree contains Terminals without a parent node.

E.g.:

use lumberjack::{Tree, TreeOps};

/// Inserts NonTerminal with label `"INSERT"` above Terminals.
///
/// All Terminals that are dominated by a node matched by match_fn will have a NonTerminal
/// with label `"INSERT"` inserted above.
fn do_insertion(tree: &mut Tree) {
    tree.insert_intermediate(|tree, parent_idx| {
        if tree[parent_idx].label() == "label" {
            Some("INSERT".to_string())
        } else {
            None
        }
    }).unwrap();
}

fn filter_nonterminals<F>(&mut self, match_fn: F) -> Result<(), Error> where
    F: Fn(&Tree, NodeIndex) -> bool

Remove non-terminals not matched by the match function.

The root node will never be removed. Root node is determined by the tree::is_root() method. Detached material is re-attached above the removed node.

E.g.:

use lumberjack::{Tree, TreeOps};

/// Remove all nonterminals from the tree that don't have a feature `"key"`.
fn do_filtering(tree: &mut Tree) {
    tree.filter_nonterminals(|tree, nonterminal_idx| {
        let nt = tree[nonterminal_idx].nonterminal().unwrap();
        if let Some(features) = nt.features() {
             features.get_val("key").is_none()
        } else {
            false
        }
    }).unwrap();
}

fn reattach_terminals<F>(&mut self, attachment: NodeIndex, match_fn: F) where
    F: Fn(&Tree, NodeIndex) -> bool

Reattach terminals matched by the match function.

The method iterates over the terminals in no particular order and reattaches those terminals for which match_fn returns true to the attachment point given in attachment.

This method will remove NonTerminal nodes if all Terminals below it are reattached. This includes but is not limited to unary chains.

Panics if the attachment point is the index of a Terminal.

E.g.:

use lumberjack::{Tree, TreeOps};

/// Reattach all terminals.
///
/// Reattach terminals to the root that are attached to a parent without feature `"key"`.
fn do_reattachment(tree: &mut Tree) {
    let root = tree.root();
    tree.reattach_terminals(root, |tree, terminal_idx| {
        if let Some((parent, _)) = tree.parent(terminal_idx) {
            let parent_nt = tree[parent].nonterminal().unwrap();
            if let Some(features) = parent_nt.features() {
                 return features.get_val("key").is_none()
            }
        }
        false
    });
}
Loading content...

Implementors

impl TreeOps for Tree[src]

Loading content...