R,utf8过滤后还是奇怪的字符
R, after utf8 filtering still weird characters
我是 R 的新手,但我需要对推文进行一些文本挖掘。我正在尝试清理语料库,以便只有 UTF8 字符。我使用下面的函数来过滤掉非 UTF 字符。
#setup with own twitter key's and access tokens
library(twitteR)
library(tm)
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
keyword = "#circulatieplan"
sinceDate = "2017-3-1"
tweets = searchTwitter(keyword,n = 300,lang = 'nl',since = sinceDate)
tweets_df = twListToDF(tweets)
tweets_df
View(tweets_df)
text = tweets_df$text
corpus = Corpus(VectorSource(text))
corpus <- tm_map(corpus, content_transformer(function(x) iconv(enc2utf8(x), sub = "byte")))
corpus_clean <- tm_map(corpus, tolower)
之后我尝试将其全部设为小写,但随后出现一些输入错误。
Error in FUN(content(x), ...) : invalid input 'Elke Sleurs gehoord op de radio. Dan viel Siegi precies nog mee. #schizo ������������' in 'utf
8towcs'
我的猜测是过滤不完美,函数无法将“�”更改为小写。
我不太清楚 utf 过滤的工作原理及其含义。有没有更好的功能或者如何解决这个错误。
编辑:
查看原始数据后,我发现有些推文包含超过 2 个字节的 utf 字符。
包含此问题的推文的推文 ID:858280532039397379
数据:
"Elke Sleurs gehoord op de radio. Dan viel Siegi precies nog mee. #schizo \xed\xa0\xbd\xed\xb8\xb3\xed\xa0\xbd\xed\xb9\x84 #gent #circulatieplan",
然后我尝试用正则表达式删除所有这些,但没有成功。正则表达式是否错误或您不能在语料库对象上使用正则表达式?
corpus <- tm_map(corpus, content_transformer(function(x) gsub(x, pattern = "(\)\w+", replacement = "")))
我找到了过滤表情的方法。经过大量搜索后,我发现有一个函数可以在编码之间转换字符向量。 iconv documentation
...
text = tweets_df$text
# remove emoticons
text <- sapply(text,function(row) iconv(row, "latin1", "ASCII", sub=""))
corpus = Corpus(VectorSource(text))
...
我是 R 的新手,但我需要对推文进行一些文本挖掘。我正在尝试清理语料库,以便只有 UTF8 字符。我使用下面的函数来过滤掉非 UTF 字符。
#setup with own twitter key's and access tokens
library(twitteR)
library(tm)
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
keyword = "#circulatieplan"
sinceDate = "2017-3-1"
tweets = searchTwitter(keyword,n = 300,lang = 'nl',since = sinceDate)
tweets_df = twListToDF(tweets)
tweets_df
View(tweets_df)
text = tweets_df$text
corpus = Corpus(VectorSource(text))
corpus <- tm_map(corpus, content_transformer(function(x) iconv(enc2utf8(x), sub = "byte")))
corpus_clean <- tm_map(corpus, tolower)
之后我尝试将其全部设为小写,但随后出现一些输入错误。
Error in FUN(content(x), ...) : invalid input 'Elke Sleurs gehoord op de radio. Dan viel Siegi precies nog mee. #schizo ������������' in 'utf
8towcs'
我的猜测是过滤不完美,函数无法将“�”更改为小写。
我不太清楚 utf 过滤的工作原理及其含义。有没有更好的功能或者如何解决这个错误。
编辑:
查看原始数据后,我发现有些推文包含超过 2 个字节的 utf 字符。
包含此问题的推文的推文 ID:858280532039397379
数据:
"Elke Sleurs gehoord op de radio. Dan viel Siegi precies nog mee. #schizo \xed\xa0\xbd\xed\xb8\xb3\xed\xa0\xbd\xed\xb9\x84 #gent #circulatieplan",
然后我尝试用正则表达式删除所有这些,但没有成功。正则表达式是否错误或您不能在语料库对象上使用正则表达式?
corpus <- tm_map(corpus, content_transformer(function(x) gsub(x, pattern = "(\)\w+", replacement = "")))
我找到了过滤表情的方法。经过大量搜索后,我发现有一个函数可以在编码之间转换字符向量。 iconv documentation
...
text = tweets_df$text
# remove emoticons
text <- sapply(text,function(row) iconv(row, "latin1", "ASCII", sub=""))
corpus = Corpus(VectorSource(text))
...