Trie Usage

Other Trie Packages

The rtrie package provides a few simple functions to support tries representing lists of words. Other R packages on CRAN provide trie implementations with different characteristics including:

Also see various packages related to trees in general.

Trie Creation

A list of words can be created programatically or read from a data file or other source.

  words<-read.delim(
    file=system.file("extdata", "dictionary.txt", package = "rtrie"), 
    header=F, sep = " ",
    stringsAsFactors = FALSE)
  class(words)

In this case a data frame was created from a file. Creating a trie involves a simple function call to the char_tree function. A trie is created from a vector passed as an argument (in this case column V1 from the data frame).

trie <- char_tree(words$V1, 'X')

To get an idea of the performance, the file in question contained r length(words$V1) words. The microbenchmark library can be used for timing.

  library(rtrie)
  library(microbenchmark)
  timings <- microbenchmark(
    trie <- char_tree(words$V1, 'X'), 
    times=1
  )

  print(paste(timings$time / 1000000000, 'seconds'))

Match the Beginning of Words

What are words that start with "stu".

cat(matching_words('stu',trie))

Test if a Character String is a Word

Is "stunt" a word?

cat(is_word('stunt',trie))

How about "stu"?

cat(is_word('stu',trie))


Try the rtrie package in your browser

Any scripts or data that you put into this service are public.

rtrie documentation built on May 29, 2017, 11:26 p.m.