使用波斯语在 R 中进行文本挖掘

Text Mining in R with Persian

我希望对我收集并存档在 csv 中的一些波斯语 facebook 帖子进行一些简单的数据挖掘(频率、二元字母、三元字母)。下面是我将与 facebook 评论的英语 csv 一起使用的脚本,用于将所有单个单词取消嵌套到它们自己的列中。

stp_tidy <- stc2 %>%
  filter(!str_detect(Message, "^RT")) %>%
  mutate(text = str_replace_all(Message, "https://t.co/[A-Za-z\d]+|http://[A-Za-z\d]+|&amp;|&lt;|&gt;|RT","")) %>%
  unnest_tokens(word, text, token = "regex", pattern = reg_words) %>%
  filter(!word %in% stop_words$word,
         str_detect(word, "[a-z]"))

有人知道在波斯语(或达里语)脚本中应用 unnest_tokens 的方法吗?

2 个选项。第一个例子是使用 quanteda,第二个例子是使用 udpipe。

请注意,使用波斯语打印 tibbles 很奇怪,a.k.a 特征和值往往打印在错误的列中,但数据正确存储在对象中以供进一步处理。 2 个选项之间的输出略有不同。但这些往往可以忽略不计。请注意,为了读取数据,我使用了 readtext 包。这往往与 quanteda 配合得很好。

1 名宠物

library(quanteda)
library(readtext)
# library(stopwords)

stp_test <- readtext("stp_test.csv", encoding = "UTF-8")

stp_test$Message[stp_test$Message != ""]
stp_test$text[stp_test$text != ""]

# remove records with empty messages

stp_test <- stp_test[stp_test$Message != "", ]

stp_corp <- corpus(stp_test, 
                   docid_field = "doc_id",
                   text_field = "Message")


stp_toks <- tokens(stp_corp, remove_punct = TRUE)
stp_toks <- tokens_remove(stp_toks, stopwords::stopwords(language = "fa", source = "stopwords-iso"))


# step for creating ngrams 1-3 can be done here, after removing stopwords. 
# stp_ngrams <- tokens_ngrams(stp_toks, n = 1L:3L, concatenator = "_")

stp_dfm <- dfm(stp_toks)
textstat_frequency(stp_dfm)

# transform into tidy data.frame
library(dplyr)
library(tidyr)
quanteda_tidy_out <- convert(stp_dfm, to = "data.frame") %>% 
  pivot_longer(-document, names_to = "features")

2 个 udpipe

library(udpipe)
model <- udpipe_download_model(language = "persian-seraji")
ud_farsi <- udpipe_load_model(model$file_model)

# use stp_test from quanteda example.
x <- udpipe_annotate(ud_farsi, doc_id = stp_test$doc_id, stp_test$Message)
stp_df <- as.data.frame(x)


# selecting only nouns and verbs and removing stopwords 
ud_tidy_out <- stp_df %>% 
  filter(upos %in% c("NOUN", "VERB"),
         !token %in% stopwords::stopwords(language = "fa", source = "stopwords-iso")) 

两个包都有很好的插图和支持页面。