我試圖在文本文件中找到所有名詞。最初我將.epub轉換爲.pdf文件。然後,我將.pdf成功轉換爲.txt文件,並刪除了一半的文本,因爲我只需要從本書的後半部分找到名詞。我想這樣做,所以我可以找到名詞的頻率,然後確定他們的決賽。獲取R中一本書(.txt文件)中的所有名詞,並製作頻率表和wordcloud
我可以通過原始文本文件正常執行頻率表,而無需進行任何轉換並製作wordcloud等,但似乎無法僅過濾名詞。有任何想法嗎?
cname <- file.path(".","Desktop", "egypt", "pdf")
mytxtfiles <- list.files(path = cname, pattern = "txt", full.names = TRUE)
#nouns2 and nouns doesnt seem to work :O -Ive tried both ways-
nouns2 <- regmatches(mytxtfiles, gregexpr("^([A-Z][a-z]+)+$", mytxtfiles, perl=TRUE))
nouns <- lapply(mytxtfiles, function(i) {
j <- paste0(scan(i, what = character()), collapse = " ")
regmatches(j, gregexpr("^([A-Z][a-z]+)+$", j, perl=TRUE))})
#transformation if nouns do not work
docs <- tm_map(docs[1], removeWords, stopwords("english"))
#working wordcloud and freq data
dtm <- DocumentTermMatrix(docs)
findFreqTerms(dtm, lowfreq=100)
findAssocs(dtm, "data", corlimit=0.6)
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
wf <- data.frame(word=names(freq), freq=freq)
p <- ggplot(subset(wf, freq >500), aes(word, freq))
p <-p + geom_bar(stat ="identity")
p <- p + theme(axis.text.x=element_text(angle=45, hjust=1))
library(wordcloud)
wordcloud(names(freq),freq,min.freq=100, colors=brewer.pal(6,"Dark2"))
我試圖nouns2和名詞,但他們返回類似:
nouns2
[[1]]
character(0)
[[2]]
character(0)
[[3]]
character(0)
事情與正則表達式的一部分心血來潮,代替'^([AZ] [AZ] +)+ $''與\\ B [AZ] [AZ] + \\ B' – hwnd
爲什麼地球上你是否準備了PDF格式的TXT文件,解壓'.epub'會取得更好的效果。 – Cylian