-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathREADME.Rmd
228 lines (168 loc) Β· 6.88 KB
/
README.Rmd
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>", cache = TRUE)
library(textfeatures)
library(magrittr)
options(width = 100)
skimrskim <- function(x) {
skimr::skim(x[-1]) %>%
dplyr::filter(stat %in% c("p0", "p25", "p50", "p75", "p100", "hist", "n")) %>%
dplyr::select(-value, -level, -type) %>%
tidyr::spread(stat, formatted) %>%
dplyr::select(variable, `min` = p0, `25%` = p25, `mid` = p50, `75%` = p75, `max` = p100, hist) %>%
knitr::kable()
}
```
# π· textfeatures π·<img src="man/figures/logo.png" width="160px" align="right" />
[![Build status](https://travis-ci.org/mkearney/textfeatures.svg?branch=master)](https://travis-ci.org/mkearney/textfeatures)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/mkearney/textfeatures?branch=master&svg=true)](https://ci.appveyor.com/project/mkearney/textfeatures)
[![CRAN status](https://www.r-pkg.org/badges/version/textfeatures)](https://cran.r-project.org/package=textfeatures)
[![Coverage Status](https://codecov.io/gh/mkearney/textfeatures/branch/master/graph/badge.svg)](https://codecov.io/gh/mkearney/textfeatures?branch=master)
[![DOI](https://zenodo.org/badge/123046986.svg)](https://zenodo.org/badge/latestdoi/123046986)
![Downloads](https://cranlogs.r-pkg.org/badges/textfeatures)
![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/textfeatures)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
> Easily extract useful features from character objects.
## Install
Install from CRAN.
```{r cran, eval=FALSE}
## download from CRAN
install.packages("textfeatures")
```
Or install the development version from Github.
```{r github, eval=FALSE}
## install from github
devtools::install_github("mkearney/textfeatures")
```
## Usage
### `textfeatures()`
Input a character vector.
```{r chr}
## vector of some text
x <- c(
"this is A!\t sEntence https://github.com about #rstats @github",
"and another sentence here", "THe following list:\n- one\n- two\n- three\nOkay!?!"
)
## get text features
textfeatures(x, verbose = FALSE)
```
Or input a data frame with a column named `text`.
```{r df}
## data frame with rstats tweets
rt <- rtweet::search_tweets("rstats", n = 2000, verbose = FALSE)
## get text features
tf <- textfeatures(rt, verbose = FALSE)
## preview data
tf
```
Compare across multiple authors.
```{r news, eval = FALSE}
## data frame tweets from multiple news media accounts
news <- rtweet::get_timelines(
c("cnn", "nytimes", "foxnews", "latimes", "washingtonpost"),
n = 2000)
## get text features (including ests for 20 word dims) for all observations
news_features <- textfeatures(news, word_dims = 20, verbose = FALSE)
```
```{r news_features, echo = FALSE, eval = FALSE}
## override id with screen names
news_features$user_id <- news$screen_name
## load the tidyverse
suppressPackageStartupMessages(library(tidyverse))
## convert to long (tidy) form and plot
p <- news_features %>%
scale_count() %>%
scale_standard() %>%
group_by(user_id) %>%
summarise_if(is.numeric, mean) %>%
gather(var, val, -user_id) %>%
arrange(-val) %>%
mutate(var = factor(var, levels = unique(var)),
user_id = paste0("@", user_id)) %>%
ggplot(aes(x = var, y = val, fill = user_id)) +
geom_col(width = .15, fill = "#000000bb") +
geom_point(size = 2.5, shape = 21) +
tfse::theme_mwk(light = "#ffffff") +
facet_wrap(~ user_id, nrow = 1) +
coord_flip() +
theme(legend.position = "none",
axis.text = element_text(colour = "black"),
axis.text.x = element_text(size = rel(.7)),
plot.title = element_text(face = "bold", size = rel(1.6)),
panel.grid.major = element_line(colour = "#333333", size = rel(.05)),
panel.grid.minor = element_line(colour = "#333333", size = rel(.025))) +
labs(y = NULL, x = NULL,
title = "{textfeatures}: Extract Features from Text",
subtitle = "Features extracted from text of the most recent 2,000 tweets posted by each news media account")
## save plot
ggsave("tools/readme/readme.png", p, width = 9, height = 6, units = "in")
```
<p style='align:center'><img src='tools/readme/readme.png' max-width="600px" /></p>
## Fast version
If you're looking for something faster try setting `sentiment = FALSE` and `word2vec = 0`.
```{r fast}
## get non-substantive text features
textfeatures(rt, sentiment = FALSE, word_dims = 0, verbose = FALSE)
```
## Example: NASA meta data
Extract text features from NASA meta data:
```{r, include=FALSE}
if (!file.exists(".nasa.rds")) {
## read NASA meta data
nasa <- jsonlite::fromJSON("https://data.nasa.gov/data.json")
## identify non-public or restricted data sets
nonpub <- grepl("Not publicly available|must register",
nasa$data$rights, ignore.case = TRUE) |
nasa$dataset$accessLevel %in% c("restricted public", "non-public")
## create data frame with ID, description (name it "text"), and nonpub
nd <- data.frame(text = nasa$dataset$description, nonpub = nonpub,
stringsAsFactors = FALSE)
## drop duplicates (truncate text to ensure more distinct obs)
nd <- nd[!duplicated(tolower(substr(nd$text, 1, 100))), ]
## filter via sampling to create equal number of pub/nonpub
nd <- nd[c(sample(which(!nd$nonpub), sum(nd$nonpub)), which(nd$nonpub)), ]
saveRDS(nd, ".nasa.rds")
} else {
nd <- readRDS(".nasa.rds")
}
```
```{r nd, eval=FALSE}
## read NASA meta data
nasa <- jsonlite::fromJSON("https://data.nasa.gov/data.json")
## identify non-public or restricted data sets
nonpub <- grepl("Not publicly available|must register",
nasa$data$rights, ignore.case = TRUE) |
nasa$dataset$accessLevel %in% c("restricted public", "non-public")
## create data frame with ID, description (name it "text"), and nonpub
nd <- data.frame(text = nasa$dataset$description, nonpub = nonpub,
stringsAsFactors = FALSE)
## drop duplicates (truncate text to ensure more distinct obs)
nd <- nd[!duplicated(tolower(substr(nd$text, 1, 100))), ]
## filter via sampling to create equal number of pub/nonpub
nd <- nd[c(sample(which(!nd$nonpub), sum(nd$nonpub)), which(nd$nonpub)), ]
```
```{r nasafinal}
## get text features
nasa_tf <- textfeatures(nd, word_dims = 20, normalize = FALSE, verbose = FALSE)
## drop columns with little to no variance
min_var <- function(x, min = 1) {
is_num <- vapply(x, is.numeric, logical(1))
non_num <- names(x)[!is_num]
yminvar <- names(x[is_num])[vapply(x[is_num], function(.x) stats::var(.x,
na.rm = TRUE) >= min, logical(1))]
x[c(non_num, yminvar)]
}
nasa_tf <- min_var(nasa_tf)
## view summary
skimrskim(nasa_tf)
## add nonpub variable
nasa_tf$nonpub <- nd$nonpub
## run model predicting whether data is restricted
m1 <- glm(nonpub ~ ., data = nasa_tf[-1], family = binomial)
## view model summary
summary(m1)
## how accurate was the model?
table(predict(m1, type = "response") > .5, nasa_tf$nonpub)
```