This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
clean_tweets.R
92 lines (83 loc) · 2.64 KB
/
clean_tweets.R
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
#' Clean text of tweets
#'
#' Removes from the text, users mentions, hashtags, urls and media.
#' Some urls or other text might remain if it is not recognized as an entity by
#' the API. `r lifecycle::badge("deprecated")`
#' @param x Tweets
#' @param clean Type of elements to be removed.
#' @seealso [`rtweet-deprecated`]
#' @return A vector with the text without the entities selected
#' @export
clean_tweets <- function(x, clean = c("users", "hashtags", "urls", "media")) {
if (is.character(x)) {
abort("You should provide tweets with all the users and hashtags information",
call = current_call())
}
tweets <- nrow(x)
final_text <- vector(mode = "character", length = tweets)
clean <- match.arg(clean, several.ok = TRUE)
for (tweet_n in seq_len(tweets)) {
text <- x$full_text[tweet_n]
start <- vector("numeric")
end <- vector("numeric")
if ("users" %in% clean) {
i <- x$entities[[tweet_n]][["user_mentions"]][["indices"]]
if (!is.null(i)) {
start <- c(start, i_type(i))
end <- c(end, i_type(i, type = "end"))
}
}
if ("hashtags" %in% clean) {
hashtags <- x$entities[[tweet_n]][["hashtags"]][["indices"]]
if (!is.null(hashtags)) {
i <- as.data.frame(do.call(rbind, hashtags))
start <- c(start, i_type(i))
end <- c(end, i_type(i, type = "end"))
}
}
if ("media" %in% clean) {
media <- x$entities[[tweet_n]][["media"]][["indices"]]
if (!is.null(media)) {
i <- as.data.frame(do.call(rbind, media))
start <- c(start, i_type(i))
end <- c(end, i_type(i, type = "end"))
}
}
if ("urls" %in% clean) {
i <- x$entities[[tweet_n]][["urls"]][["indices"]]
if (!is.null(i)) {
start <- c(start, i_type(i))
end <- c(end, i_type(i, type = "end"))
}
}
nothing <- start != 0
start <- start[nothing]
end <- end[nothing]
if (length(start) < 1) {
final_text[tweet_n] <- text
} else {
# message(tweet_n)
final_text[tweet_n] <- repaste(text, start, end)
}
}
# Remove tags about retweets
gsub("RT : ", final_text, replacement = "", fixed = TRUE)
}
i_type <- function(x, type = "start") {
if (length(x) == 1 && is.na(x)) {
return(0)
}
x[[type]]
}
repaste <- function(text, start, stop) {
stopifnot(length(text) == 1)
text <- strsplit(text, "")[[1]]
remove <- vector(mode = "numeric")
# Account for index starting at 0 in Twitter API output
start <- start + 1
for (s_p in seq_along(start)) {
position_str <- seq(from = start[s_p], to = stop[s_p])
remove <- c(remove, position_str)
}
paste(text[-remove], collapse = "")
}