# Create a phrase net with R

Does anyone know of an R package or have a way to create phrase nets like this?

I hope this makes sense. I kind of threw it together, but it seems that it is what you want to do. I grabbed some test from the above bounty hyperlink. It will show the words that come after a certain word as well the ratio of times that these outcomes happened. This will do nothing for the visualization, although I'm sure it would not be impossible to create. It should do most of the background math.

library(tau)

x <- tokenize("Questions must be at least 2 days old to be eligible for a bounty. There can only be 1 active bounty per question at any given time. Users must have at least 75 reputation to offer a bounty, and may only have a maximum of 3 active bounties at any given time. The bounty period lasts 7 days. Bounties must have a minimum duration of at least 1 day. After the bounty ends, there is a grace period of 24 hours to manually award the bounty. If you do not award your bounty within 7 days (plus the grace period), the highest voted answer created after the bounty started with at least 2 upvotes will be awarded half the bounty amount. If there's no answer meeting that criteria, the bounty is not awarded to anyone. If the bounty was started by the question owner, and the question owner accepts an answer during the bounty period, and the bounty expires without an explicit award – we assume the bounty owner liked the answer they accepted and award it the full bounty amount at the time of bounty expiration. In any case, you will always give up the amount of reputation specified in the bounty, so if you start a bounty, be sure to follow up and award your bounty to the best answer! As an additional bonus, bounty awards are immune to the daily reputation cap and community wiki mode.")

#the number of tokens in the string
n <- length(x)

list <- NULL

count <- 1

#this will remove spaces, list is new string with no spaces
for (i in 1:n) {
if (x[i] != " ") {
list[count] <- x[i]
count <- count + 1
}
}

#the unique words in the string
y <- unique(list)

#number of tokens in the string
n <- length(list)
#number of distinct tokens
m <- length(y)

#assign tokens to values
ind <- NULL
val <- NULL
#make vector of numbers in place of tokens
for (i in 1:m) {
ind[i] <- i
for (j in 1:n) {
if (y[i] == list[j]) {
val[j] = i
}
}
}

d <- array(0, c(m, m))

#this finds the number of count of the word after the current word
for (i in 1:(n-1)) {
d[val[i], val[i+1]] <- d[val[i], val[i+1]] + 1
}

#pick a word
word <- 4

#show the word
y[word]
#[1] "at"

#the words that follow
y[which(d[word,] > 0)]
#[1] "least" "any"   "the"

#the prob of words that follow
d[word,which(d[word,]>0)]/sum(d[word,])
#[1] 0.5714286 0.2857143 0.1428571

• This is making some great strides towrds a plot that looks closer to the above. It's actually the plotting/visualization of this I'm struggling with. The plot is almost like a wordcloud (size = frequency) and the arrows are similar to a sociogram in network analysis, but the arrows convey meaning in that they are a stronger link. I think the work you did will be useful in plotting the arrows. I'm actually not too familiar with network analysis and visualization so I need a lot of help here. – Tyler Rinker Feb 17 '12 at 18:04
• Add this to the end to get a graph. It will be plain though, you will probably want to filter the lower ranking words out and only use those with a larger support. dd <- t(d) library(diagram) plotmat(dd[1:10, 1:10], box.size = 0.05, name = y[1:10], lwd = 2 * dd[1:10,]) – darrelkj Feb 19 '12 at 5:48
• @ darrelkj This seems to be limited to 10 words but I think with a bit of work with connecting it to sociograms or something like that we'd have a pretty polished function. I'm marking this response as correct. darrelkj after this much work you should put the finishing touches on this and throw it into a package. If you do let us know. Thanks for your help. – Tyler Rinker Feb 19 '12 at 7:31
• It is not limited to 10, I just did not want to use the whole array. The ten used here are poorly chosen as well. – darrelkj Feb 19 '12 at 17:22
• I stand corrected. I had made an error in the code when i tried it and hence got an out of bounds error. You are quite correct. – Tyler Rinker Feb 19 '12 at 17:28

You can create phrase nets with Many Eyes, which is kind of the "official" home of this visualization technique. There you can upload your data (which is probably some body of text), choose "Phrase Net" as the visualization technique, and get what you're looking for.

In fact, your illustration comes from the Phrase Net page on Many Eyes.

• Yes I realize this but I was hoping to do it in R because of the flexibility. You could alter all sorts of parameters to better represent the data that you can't with Many Eyes. – Tyler Rinker Feb 17 '12 at 17:04

You can use package igraph to make and plot a graph, with control over all of the aspects. The graph and Rgraphviz packages work together to define and plot graphs. Both options provide a lot of control. (graphviz is also a standalone package, where you could use all kinds of software to generate the graph and have graphviz display it.)

Of course, you need to process your data into a graph, doing something like @darrelkj suggests.