Chris Bail
Duke University
website: https://www.chrisbail.net
github: https://github.com/cbail
Twitter: https://www.twitter.com/chris_bail
The textnets
package provides the following functions:
1) preparing texts for network analysis
2) creating text networks
3) visualizing text networks
4) detecting themes or “topics” within text networks
library(devtools)
install_github("cbail/textnets")
library(textnets)
data(sotu)
sotu_first_speeches <- sotu %>% group_by(president) %>% slice(1L)
prepped_sotu <- PrepText(sotu_first_speeches, groupvar = "president", textvar = "sotu_text", node_type = "groups", tokenizer = "words", pos = "nouns", remove_stop_words = TRUE, compound_nouns = TRUE)
sotu_text_network <- CreateTextnet(prepped_sotu)
VisTextNet(sotu_text_network, label_degree_cut = 0)
library(htmlwidgets)
vis <- VisTextNetD3(sotu_text_network,
height=300,
width=400,
bound=FALSE,
zoom=FALSE,
charge=-30)
saveWidget(vis, "sotu_textnet.html")
VisTextNet(sotu_text_network, alpha=.1, label_degree_cut = 2)
sotu_communities <- TextCommunities(sotu_text_network)
head(sotu_communities)
group modularity_class
1 Abraham Lincoln 3
2 Andrew Jackson 3
3 Andrew Johnson 3
4 Barack Obama 2
5 Benjamin Harrison 3
6 Calvin Coolidge 1
top_words_modularity_classes <- InterpretText(sotu_text_network, prepped_sotu)
head(top_words_modularity_classes, 10)
# A tibble: 10 x 2
# Groups: modularity_class [2]
modularity_class lemma
<chr> <chr>
1 2 recovery plan
2 1 consolidation
3 1 child labor
4 1 exploitation
5 1 readjustment
6 1 recovery
7 1 recovery act
8 1 restoration
9 1 structure
10 2 drug
text_centrality <- TextCentrality(sotu_text_network)