簡體   English   中英

在 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。

請注意,用波斯語打印小標題很奇怪,也就是特征和值往往打印在錯誤的列中,但數據正確存儲在對象內以供進一步處理。 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")) 

這兩個軟件包都有很好的小插圖和支持頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM