使用 wordcloud by twitter 挖掘文本的非英文字母问题
Problems with non english letters using wordcloud by twitter mined text
我是 Whosebug 的新手,我一直在尽最大努力遵循指南。如果我遗漏了什么,请告诉我。
最近我一直在研究 R 中的文本挖掘;我是新手的东西。我一直在使用您可以在下面嵌套的代码中找到的包来执行此操作。但是,当 wordcloud 显示瑞典字母 å、ä 和 ö 时会出现问题。正如您在附图中看到的那样,这些点的位置有点奇怪。
Wordcloud image
我一直在尽我所能自己解决这个问题,但无论我一直在尝试什么,我似乎都无法让它发挥作用。
我尝试过的事情:
- 使用
Encoding(tweets) <- "UTF-8"
尝试将 tweets
设置为 UTF-8
- 使用
iconv(tweets, from = "UTF-8", to = "UTF-8", sub = "")
此外,定义语料库向量后的最后一部分代码是从tm-package的作者那里复制的。在其他人提到以语料库向量作为输入的 wordcloud 函数存在问题后,他将此列为解决方案。没有它,我在尝试创建 wordcloud 时收到一条错误消息。
#Get and load necessary packages:
install.packages("twitteR")
install.packages("ROAuth")
install.packages("wordcloud")
install.packages("tm")
library("tm")
library("wordcloud")
library("twitteR")
library("ROAuth")
#Authentication:
api_key <- "XXX"
api_secret <- "XXX"
access_token <- "XXX"
access_token_secret <- "XXX"
cred <- setup_twitter_oauth(api_key,api_secret,access_token,
access_token_secret)
#Extract tweets:
search.string <- "#svpol"
no.of.tweets <- 3200
tweets <- searchTwitter(search.string, n=no.of.tweets, since = "2017-01-01")
tweets.text <- sapply(tweets, function(x){x$getText()})
#Remove tweets that starts with "RT" (retweets):
tweets.text <- gsub("^\bRT", "", tweets.text)
#Remove tabs:
tweets.text <- gsub("[ |\t]{2,}", "", tweets.text)
#Remove usernames:
tweets.text <- gsub("@\w+", "", tweets.text)
tweets.text <- (tweets.text[!is.na(tweets.text)])
tweets.text <- gsub("\n", " ", tweets.text)
#Remove links:
tweets.text <- gsub("http[^[:space:]]*", "", tweets.text)
#Remove stopwords:
stopwords_swe <- c("är", "från", "än")
#Just a short example above, the real one is very large
tweets.text <- removeWords(tweets.text,stopwords_swe)
#Create corpus:
tweets.text.corpus <- Corpus(VectorSource(tweets.text))
#See notes in the longer text about the corpus vector
tweets.text.corpus <- tm_map(tweets.text.corpus,
content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')), mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, content_transformer(tolower), mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, removePunctuation, mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, function(x)removeWords(x,stopwords(kind = "en")), mc.cores=1)
wordcloud <- wordcloud(tweets.text.corpus, min.freq = 10,
max.words=300, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Set2"))
wordcloud
会非常高兴收到这方面的帮助!
首先将向量编码为 UTF-8-MAC
(因为我在 OSX),然后使用 gsub()
函数手动更改十六进制代码,设法解决了这个问题对于 å,ä,ö(我遇到问题的字母)到实际字母。例如 gsub("0xc3 0x85", "å", x)
、gsub("0xc3 0xa5", "å", x)
(区分大小写)。
最后将 tm_map()
函数的参数从 UTF-8-MAC
更改为 latin1
。这对我有用,希望将来其他人会发现它有用。
我是 Whosebug 的新手,我一直在尽最大努力遵循指南。如果我遗漏了什么,请告诉我。
最近我一直在研究 R 中的文本挖掘;我是新手的东西。我一直在使用您可以在下面嵌套的代码中找到的包来执行此操作。但是,当 wordcloud 显示瑞典字母 å、ä 和 ö 时会出现问题。正如您在附图中看到的那样,这些点的位置有点奇怪。
Wordcloud image
我一直在尽我所能自己解决这个问题,但无论我一直在尝试什么,我似乎都无法让它发挥作用。
我尝试过的事情:
- 使用
Encoding(tweets) <- "UTF-8"
尝试将tweets
设置为 UTF-8 - 使用
iconv(tweets, from = "UTF-8", to = "UTF-8", sub = "")
此外,定义语料库向量后的最后一部分代码是从tm-package的作者那里复制的。在其他人提到以语料库向量作为输入的 wordcloud 函数存在问题后,他将此列为解决方案。没有它,我在尝试创建 wordcloud 时收到一条错误消息。
#Get and load necessary packages:
install.packages("twitteR")
install.packages("ROAuth")
install.packages("wordcloud")
install.packages("tm")
library("tm")
library("wordcloud")
library("twitteR")
library("ROAuth")
#Authentication:
api_key <- "XXX"
api_secret <- "XXX"
access_token <- "XXX"
access_token_secret <- "XXX"
cred <- setup_twitter_oauth(api_key,api_secret,access_token,
access_token_secret)
#Extract tweets:
search.string <- "#svpol"
no.of.tweets <- 3200
tweets <- searchTwitter(search.string, n=no.of.tweets, since = "2017-01-01")
tweets.text <- sapply(tweets, function(x){x$getText()})
#Remove tweets that starts with "RT" (retweets):
tweets.text <- gsub("^\bRT", "", tweets.text)
#Remove tabs:
tweets.text <- gsub("[ |\t]{2,}", "", tweets.text)
#Remove usernames:
tweets.text <- gsub("@\w+", "", tweets.text)
tweets.text <- (tweets.text[!is.na(tweets.text)])
tweets.text <- gsub("\n", " ", tweets.text)
#Remove links:
tweets.text <- gsub("http[^[:space:]]*", "", tweets.text)
#Remove stopwords:
stopwords_swe <- c("är", "från", "än")
#Just a short example above, the real one is very large
tweets.text <- removeWords(tweets.text,stopwords_swe)
#Create corpus:
tweets.text.corpus <- Corpus(VectorSource(tweets.text))
#See notes in the longer text about the corpus vector
tweets.text.corpus <- tm_map(tweets.text.corpus,
content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')), mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, content_transformer(tolower), mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, removePunctuation, mc.cores=1)
tweets.text.corpus <- tm_map(tweets.text.corpus, function(x)removeWords(x,stopwords(kind = "en")), mc.cores=1)
wordcloud <- wordcloud(tweets.text.corpus, min.freq = 10,
max.words=300, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Set2"))
wordcloud
会非常高兴收到这方面的帮助!
首先将向量编码为 UTF-8-MAC
(因为我在 OSX),然后使用 gsub()
函数手动更改十六进制代码,设法解决了这个问题对于 å,ä,ö(我遇到问题的字母)到实际字母。例如 gsub("0xc3 0x85", "å", x)
、gsub("0xc3 0xa5", "å", x)
(区分大小写)。
最后将 tm_map()
函数的参数从 UTF-8-MAC
更改为 latin1
。这对我有用,希望将来其他人会发现它有用。