如何使用 tm 包进行希伯来语或阿拉伯语文本分析

How to use tm package for text analytics in Hebrew or Arabic

我想使用 tm 包进行希伯来语或阿拉伯语文本分析。我尝试了几种方法来查看 tm 是否能够处理某些单词,但我 运行 出错了,有没有办法解决这个问题?

 text  <- "הנוסעים חיכו זמן רב לנסיעה"
 Encoding(text)
#[1] "unknown"
 Encoding(text)  <- "UTF-8"
 ap.corpus <- Corpus(DataframeSource(data.frame(text)))
 ap.corpus <- tm_map(ap.corpus, removePunctuation)
 ap.corpus <- tm_map(ap.corpus, content_transformer(tolower))
Error in FUN(content(x), ...) : 
  invalid input 'הנוסעים חיכו זמן רב לנסיעה' in 'utf8towcs'

来自tm vignette

The second argument readerControl of the corpus constructor has to be a list with the named components reader and language. (...) Finally, the second component language sets the texts’ language (preferably using ISO 639-2 codes).

Wikipedia 开始,阿拉伯语的 ISO 639-2 代码是 ara,希伯来语的 ISO 639-2 代码是 heb。所以也许试试这个:

 ap.corpus <- Corpus(DataframeSource(data.frame(text), readerControl = list(language = "heb")))

编辑:很高兴您找到了答案。当使用错误的编码时,会出现此错误:

  • R tm package invalid input in 'utf8towcs'
  • FUN-error after running 'tolower' while making Twitter wordcloud

这里是答案,我们需要添加这种编码方式:

iconv(text, "ISO-8859-8", "UTF-8")[1]

而不是使用:Encoding(text) <- "UTF-8"