我正在寻找一些v。简单数据挖掘(频率,二字组,三字母组),这些数据是我在波斯语中收集并存档在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]+|&|<|>|RT","")) %>%
unnest_tokens(word, text, token = "regex", pattern = reg_words) %>%
filter(!word %in% stop_words$word,
str_detect(word, "[a-z]"))
有人知道在波斯(或Dari)脚本中应用unnest_tokens的任何方法吗?
2个选项。第一个示例使用Quanteda,第二个示例使用udpipe。
请注意,用波斯语打印小标题很怪异,也经常将特征和值打印在错误的列中,但数据已正确存储在对象中以进行进一步处理。这两个选项之间的输出略有不同。但是这些往往可以忽略不计。请注意,为了读取数据,我使用了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"))
两个软件包都具有良好的插页和支持页面。