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.
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'))
What are words that start with "stu".
cat(matching_words('stu',trie))
Is "stunt" a word?
cat(is_word('stunt',trie))
How about "stu"?
cat(is_word('stu',trie))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.