Tree: Tree objects

Description Usage Arguments Details Examples

View source: R/tree.R

Description

Creation and manipulation of tree objects.

Usage

1
2
3
4
5
6
Tree(value, children = list())
## S3 method for class 'Tree'
format(x, width = 0.9 * getOption("width"), indent = 0,
       brackets = c("(", ")"), ...)
Tree_parse(x, brackets = c("(", ")"))
Tree_apply(x, f, recursive = FALSE)

Arguments

value

a (non-tree) node value of the tree.

children

a list giving the children of the tree.

x

a tree object for the format() method and Tree_apply(); a character string for Tree_parse().

width

a positive integer giving the target column for a single-line nested bracketting.

indent

a non-negative integer giving the indentation used for formatting.

brackets

a character vector of length two giving the pair of opening and closing brackets to be employed for formatting or parsing.

...

further arguments passed to or from other methods.

f

a function to be applied to the children nodes.

recursive

a logical indicating whether to apply f recursively to the children of the children and so forth.

Details

Trees give hierarchical groupings of leaves and subtrees, starting from the root node of the tree. In natural language processing, the syntactic structure of sentences is typically represented by parse trees (e.g., https://en.wikipedia.org/wiki/Concrete_syntax_tree) and displayed using nested brackettings.

The tree objects in package NLP are patterned after the ones in NLTK (https://www.nltk.org), and primarily designed for representing parse trees. A tree object consists of the value of the root node and its children as a list of leaves and subtrees, where the leaves are elements with arbitrary non-tree values (and not subtrees with no children). The value and children can be extracted via $ subscripting using names value and children, respectively.

There is a format() method for tree objects: this first tries a nested bracketting in a single line of the given width, and if this is not possible, produces a nested indented bracketting. The print() method uses the format() method, and hence its arguments to control the formatting.

Tree_parse() reads nested brackettings into a tree object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
x <- Tree(1, list(2, Tree(3, list(4)), 5))
format(x)
x$value
x$children

p <- Tree("VP",
          list(Tree("V",
                    list("saw")),
               Tree("NP",
                    list("him"))))
p <- Tree("S",
          list(Tree("NP",
                    list("I")),
               p))
p
## Force nested indented bracketting:
print(p, width = 10)

s <- "(S (NP I) (VP (V saw) (NP him)))"
p <- Tree_parse(s)
p

## Extract the leaves by recursively traversing the children and
## recording the non-tree ones:
Tree_leaf_gatherer <-
function()
{
    v <- list()
    list(update =
         function(e) if(!inherits(e, "Tree")) v <<- c(v, list(e)),
         value = function() v,
         reset = function() { v <<- list() })
}
g <- Tree_leaf_gatherer()
y <- Tree_apply(p, g$update, recursive = TRUE)
g$value()

Example output

[1] "(1 2 (3 4) 5)"
[1] 1
[[1]]
[1] 2

[[2]]
(3 4)

[[3]]
[1] 5

(S (NP I) (VP (V saw) (NP him)))
(S
  (NP I)
  (VP
    (V
      saw)
    (NP
      him)))
(S (NP I) (VP (V saw) (NP him)))
[[1]]
[1] "I"

[[2]]
[1] "saw"

[[3]]
[1] "him"

NLP documentation built on Oct. 23, 2020, 6:18 p.m.

Related to Tree in NLP...