【问题标题】:invalid multibyte string in foreign language encoding外语编码中的无效多字节字符串
【发布时间】:2019-12-11 07:42:52
【问题描述】:

我正在使用 R 的 stm 分析已解析/分段的外语(简体中文)文本文档,以利用包的绘图环境。我没有使用该包的内置文本处理功能,因为它目前不支持处理中文文本;但是,在我成功准备数据(需要lda格式的documentsvocab结合相同行长的原始元数据)并拟合模型后,plot()函数向我抛出了错误消息,可能是由于预处理阶段的一些编码问题:

Error in nchar(text) : invalid multibyte string, element 1

根据之前一些线程的建议,我应用了stringiutf8 中的编码函数将vocab 编码为UTF-8 并再次重新绘制估计结果,但它返回了相同的错误。我想知道编码发生了什么以及此类错误是否可以修复,因为stm 使用基本 R 的绘图功能,而后者在显示外语文本时应该没有问题。 (请注意,在预处理原始文本之前,我已将语言区域设置为“中文”((简体)_China.936))

如果有人能就此启发我,将不胜感激。下面提供了我的代码。

Sys.setlocale("LC_ALL","Chinese")  # set locale to simplified Chinese to render the text file
# install.packages("stm")
require(stm)

con1 <- url("https://www.dropbox.com/s/tldmo7v9ssuccxn/sample_dat.RData?dl=1")
load(con1)
names(sample_dat)  # sample_dat is the original metadata and is reduced to only 3 columns
con2 <- url("https://www.dropbox.com/s/za2aeg0szt7nssd/blog_lda.RData?dl=1")
load(con2)
names(blog_lda)   # blog_lda is a lda type object consists of documents and vocab

# using the script from stm vignette to prepare the data
out <- prepDocuments(blog_lda$documents, blog_lda$vocab, sample_dat)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta

# estimate a 10-topic model for the ease of exposition
PrevFit <- stm(documents = docs, vocab = vocab, K = 10, prevalence =~ sentiment + s(day), max.em.its = 100, data = meta, init.type = "Spectral")
# model converged at the 65th run
# plot the model
par(mar=c(1,1,1,1))
plot(PrevFit, type = "summary", xlim = c(0, 1))
Error in nchar(text) : invalid multibyte string, element 1

#check vocab
head(vocab)
# returning some garbled text
[1] "\"�\xf3½\","       "\"���\xfa\xe8�\","
[3] "\"�\xe1\","        "\"\xc8\xcb\","    
[5] "\"\u02f5\","       "\"��\xca\xc7\","  

【问题讨论】:

    标签: r encoding topic-modeling chinese-locale


    【解决方案1】:

    请使用

    词汇

    词汇

    改为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-31
      • 2015-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2020-03-22
      相关资源
      最近更新 更多