首页 > 解决方案 > 如何为 R 主题模型正确编码 UTF-8 txt 文件

问题描述

标签: rencodingutf-8nlptopic-modeling

解决方案


我找到了一种解决方法,似乎可以在您提供的 2 个示例文件上正常工作。您首先需要做的是NFKD(兼容性分解)。这将“fi”正字法连字拆分为 f 和 i。幸运的是 stringi 包可以处理这个问题。因此,在进行所有特殊文本清理之前,您需要应用该功能stringi::stri_trans_nfkd。您可以在较低步骤之后(或之前)的预处理步骤中执行此操作。

请阅读此功能的文档和参考资料。

library(tm)
docs<- VCorpus(DirSource(directory = inputdir, encoding ="UTF-8"))

#Preprocessing
docs <-tm_map(docs,content_transformer(tolower))

# use stringi to fix all the orthographic ligature issues 
docs <- tm_map(docs, content_transformer(stringi::stri_trans_nfkd))

toSpace <- content_transformer(function(x, pattern) (gsub(pattern, " ", x)))

# add following line as well to remove special quotes. 
# this uses a replace from textclean to replace the weird quotes 
# which later get removed with removePunctuation
docs <- tm_map(docs, content_transformer(textclean::replace_curly_quote))

....
rest of process
.....

推荐阅读