r西班牙术语频率矩阵,带有TD和Quanteda西班牙字符



我正在尝试学习如何使用Twitter数据进行一些文本分析。创建一个术语频率矩阵时,我正在遇到问题。我用西班牙文本(带有特殊字符(创建语料库,没有问题。

但是,当我创建术语频率矩阵(带有Quanteda或TM库(时,西班牙字符并未按预期显示(而不是看到Canción,我看到Canciãnn(。

关于如何获得术语频率矩阵以使用正确字符存储文本的任何建议?

谢谢您的任何帮助。

注意:我更喜欢使用Quanteda库,因为最终我将创建一个WordCloud,并且我认为我更好地理解了该库的方法。我也在使用Windows机器。

我尝试编码(TW2(< - " UTF-8",没有运气。

library(dplyr)
library(tm)
library(quanteda)
#' Creating a character with special Spanish characters:
tw2 <- "RT @None: Enmascarados, si masduro chingán a tarek. Si quieres ahora, la aguantas canción  . https://t."

#Cleaning the tweet, removing special punctuation, numbers http links, 
extra spaces:
clean_tw2 <- tolower(tw2)
clean_tw2 = gsub("&amp", "", clean_tw2)
clean_tw2 = gsub("(rt|via)((?:\b\W*@\w+)+)", "", clean_tw2)
clean_tw2 = gsub("@\w+", "", clean_tw2)
clean_tw2 = gsub("[[:punct:]]", "", clean_tw2)
clean_tw2 = gsub("http\w+", "", clean_tw2)
clean_tw2 = gsub("[ t]{2,}", "", clean_tw2)
clean_tw2 = gsub("^\s+|\s+$", "", clean_tw2) 
# creates a vector with common stopwords, and other words which I want removed.
myStopwords <- c(stopwords("spanish"),"tarek","vez","ser","ahora")
clean_tw2 <- (removeWords(clean_tw2,myStopwords))
# If we print clean_tw2 we see that all the characters are displayed as expected.
clean_tw2
#'Create Corpus Using quanteda library
corp_quan<-corpus(clean_tw2)
# The corpus created via quanteda, displays the characters as expected.
corp_quan$documents$texts
#'Create Corpus Using TD library
corp_td<-Corpus(VectorSource(clean_tw2))
#' Remove common words from spanish from the Corpus.
#' If we inspect the corp_td, we see that the characters and words are displayed correctly
inspect(corp_td)
# Create the DFM with quanteda library.
tdm_quan<-dfm(corp_quan)
# Here we see that the spanish characters are displayed incorrectly for Example: canción = canción
tdm_quan
# Create the TDM with TD library
tdm_td<-TermDocumentMatrix(corp_td)
# Here we see that the Spanish characters are displayed incorrectly (e.g. canción = canciÃ), and "si" is missing.
tdm_td$dimnames$Terms

在Windows平台上创建DFM时,看起来Quanteda(和TM(正在失去编码。在这个问题上的问题中,同样的问题在毫无疑问的代币中也会发生。现在可以正常工作,并且quantedatokens工作正常。如果我在dfm@Dimnames$features上执行UTF-8latin1编码,则获得正确的结果。

....
previous code
.....
tdm_quan<-dfm(corp_quan)
# Here we see that the spanish characters are displayed incorrectly for Example: canción = canción
tdm_quan
Document-feature matrix of: 1 document, 8 features (0% sparse).
1 x 8 sparse Matrix of class "dfm"
       features
docs    enmascarados si masduro chingán quieres aguantas canción t
  text1            1  2       1        1       1        1        1 1

如果执行以下操作:

Encoding(tdm_quan@Dimnames$features) <- "UTF-8"
tdm_quan
Document-feature matrix of: 1 document, 8 features (0% sparse).
1 x 8 sparse Matrix of class "dfm"
       features
docs    enmascarados si masduro chingán quieres aguantas canción t
  text1            1  2       1       1       1        1       1 1

让我猜...您在使用Windows吗?在MacOS上,它可以正常工作:

clean_tw2
## [1] "enmascarados si masduro chingán   si quieres   aguantas canción"
Encoding(clean_tw2)
## [1] "UTF-8"
dfm(clean_tw2)
## Document-feature matrix of: 1 document, 7 features (0% sparse).
## 1 x 7 sparse Matrix of class "dfm"
##        features
## docs    enmascarados si masduro chingán quieres aguantas canción
##   text1            1  2       1       1       1        1       1

我的系统信息:

sessionInfo()
# R version 3.4.4 (2018-03-15)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS High Sierra 10.13.4
# 
# Matrix products: default
# BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
# 
# locale:
# [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] tm_0.7-3       NLP_0.1-11     dplyr_0.7.4    quanteda_1.1.6

最新更新