install.packages("httr")
install.packages("jsonlite")
#install.packages("dplyr")
install.packages("plyr")
library(httr)
library(jsonlite)
library(dplyr)
library(plyr)
headers <- c(`Authorization` = sprintf('Bearer %s', Sys.getenv("TWITTER_BEARER")))
RecentSearchURL <- 'https://api.twitter.com/2/tweets/search/recent'
params <- list(query = "(chatGPT OR OpenAI)(-is:retweet)", tweet.fields = "author_id,created_at,public_metrics", max_results = 20)
res <- httr::GET(url = RecentSearchURL, httr::add_headers(.headers = headers), query = params)
res
obj <- httr::content(res, as = "text")
#prettify(obj)
tweetData <- fromJSON(obj, flatten = TRUE) %>% as.data.frame()
#tmp <- fromJSON(obj, flatten = TRUE)
#as.data.frame(tmp)
params <- list(query = "(chatGPT OR OpenAI)(lang:en)(-is:retweet)", tweet.fields = "author_id,created_at,public_metrics", max_results = 20)
res <- httr::GET(url = RecentSearchURL, httr::add_headers(.headers = headers), query = params)
obj <- httr::content(res, as = "text")
tweetData <- fromJSON(obj, flatten = TRUE) %>% as.data.frame()
as_tibble(head(tweetData))
params <- list(query = "(chatGPT OR OpenAI)(lang:ja)(-is:retweet)", tweet.fields = "author_id,created_at,public_metrics", max_results = 20)
res <- httr::GET(url = RecentSearchURL, httr::add_headers(.headers = headers), query = params)
obj <- httr::content(res, as = "text")
tweetData <- fromJSON(obj, flatten = TRUE) %>% as.data.frame()
as_tibble(head(tweetData))
tweetText <- gsub("http\\S+", "", tweetData$data.text)
write(tweetText, "recent_tweets_ja.txt")
path_home <- system("echo $HOME",intern=T)
lib_path <- paste(path_home,"/usr/local/lib/libmecab.so.2", sep="")
dyn.load(lib_path)
library(RMeCab)
tweetsDF <- docDF("recent_tweets_ja.txt", type=1)
file_name = ./recent_tweets_ja.txt opened
number of extracted terms = 282
now making a data frame. wait a while!
sub_tweetsDF <- tweetsDF %>% filter(POS1 %in% c("名詞", "形容詞", "副詞"), POS2 %in% c("一般", "固有名詞","自立"))
head(sub_tweetsDF)
library(wordcloud2)
sub_tweetsDF[,c(1,4)]
wordcloud2(sub_tweetsDF[,c(1,4)], size = 0.8, shape = 'pentagon')
params <- list(query = "(chatGPT OR OpenAI)(lang:en)(-is:retweet)", tweet.fields = "author_id,created_at,public_metrics", start_time = "2023-01-08T00:00:00Z", end_time = "2023-01-09T00:00:00Z", max_results = 20)
httr::GET(url = RecentSearchURL, httr::add_headers(.headers = headers), query = params) %>% httr::content(., as = "text") %>% fromJSON(., flatten = TRUE) %>% as.data.frame() -> tweetData2
as_tibble(head(tweetData2))
params <- list(query = "(chatGPT OR OpenAI)(lang:en)(-is:retweet)", tweet.fields = "author_id,created_at,public_metrics", start_time = "2023-01-10T00:00:00Z", end_time = "2023-01-12T00:00:00Z", max_results = 20)
httr::GET(url = RecentSearchURL, httr::add_headers(.headers = headers), query = params) %>% httr::content(., as = "text") %>% fromJSON(., flatten = TRUE) %>% as.data.frame() -> tweetData3
as_tibble(head(tweetData3))
library("wordcloud")
Loading required package: RColorBrewer
#colnames(tweetData2)
gsub("http\\S+", "", tweetData2$data.text) %>% gsub("(\\n|>)", "", .) %>% strsplit(.,"[[:space:]]|[[:punct:]]") %>% unlist %>% .[. != ""] %>% table -> wordLst2
wordcloud(names(wordLst2), wordLst2)