Direkt deinen Vorschlag kopiert und um den PDF-Teil ergänzt:
Code: Alles auswählen
install.packages("tm")
install.packages("magrittr")
install.packages("pdftools")
library("tm")
library("magrittr")
library("pdftools")
# some data
setwd("H:/HEIBOX/My Library/Ebooks/")
text <- pdf_text("Pack.pdf")
df <- data.frame(doc_id = seq_along(text), text = text, stringsAsFactors = FALSE)
corpus <- Corpus(DataframeSource(df))
# funtion that just prints the table of words
print_words <- function(corpus) {
print(rowSums(as.matrix(TermDocumentMatrix(corpus))))
return(corpus)
}
corpus %>%
print_words() %>%
# this drops the 'the'
tm_map(removeWords, stopwords('english')) %>%
print_words() %>%
# tolower drops also the other the
tm_map(tolower) %>%
tm_map(removeWords, stopwords('english')) %>%
print_words() %>%
# this drops potter
tm_map(removeWords, 'potter') %>%
print_words()
# TermDocumentMatrix
dtm <- TermDocumentMatrix(corpus)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d,10)
Ergebnis von head(d,10)
Code: Alles auswählen
word freq
the the 6404
and and 3045
was was 1493
his his 933
that that 933
had had 910
for for 840
with with 730
anson anson 652
were were 588
Während folgendes das passende Ergebnis liefert:
Code: Alles auswählen
# Install
install.packages("tm") # for text mining
install.packages("SnowballC") # for text stemming
install.packages("wordcloud") # word-cloud generator
install.packages("RColorBrewer") # color palettes
install.pdftools #pdftools zum öffnen von PDFS
# Load
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")
library("pdftools")
# In welchem Verzeichnis soll gesucht werden?
# Format: "Dateipfad"
setwd("H:/HEIBOX/My Library/EBOOKS/")
#Vorbereitung des Textes
text <- pdf_text("Pack.pdf")
# Text zum Corpus machen
docs <- Corpus(VectorSource(text))
inspect(docs)
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\|")
# Text alles zu Kleinbuchstaben
docs <- tm_map(docs, content_transformer(tolower))
# Wir brauchen keine Zahlen - oder etwa doch?
docs <- tm_map(docs, removeNumbers)
# Entfernen der "Stopwords"
docs <- tm_map(docs, removeWords, stopwords("english"))
# Eigene Liste mit Stopwords die entfernt werden sollen
# Eigene Stopwords als character vector definieren
docs <- tm_map(docs, removeWords, c("blabla1", "blabla2"))
# Satzzeichen entfernen
docs <- tm_map(docs, removePunctuation)
# "White space" entfernen
docs <- tm_map(docs, stripWhitespace)
# Text stemming
docs <- tm_map(docs, stemDocument)
# TermDocumentMatrix
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)
das passende Eregbnis liefert:
Code: Alles auswählen
> head(d, 10)
word freq
anson anson 912
ship ship 539
lord lord 343
admir admir 249
command command 239
centurion centurion 232
offic offic 232
men men 225
time time 225
great great 217
Da zweiteres funktioniert ist das nicht so schlimm, aber ich fände es trotzdem spannend, woran das liegt ...
mir fallen spontan zwei Unterschiede auf:
Normaler Text der zum dataframe gemacht wird, vs. PDF als VectorSource, sowie der Gebrauch von magrittr.
Falls es relevant ist, @ EDi, die Ausgabe exakt deines Codes schaut bei mir ebenfalls noch sehr "the"-lastig aus.
Code: Alles auswählen
> text <- c('The warden is best.', 'Happy Potter is the smartest.', 'I think I spider.')
> df <- data.frame(doc_id = seq_along(text), text = text, stringsAsFactors = FALSE)
> corpus <- Corpus(DataframeSource(df))
> print_words <- function(corpus) {
+ print(rowSums(as.matrix(TermDocumentMatrix(corpus))))
+ return(corpus)
+ }
> corpus %>%
+ print_words() %>%
+ # this drops the 'the' in Happy Potter
+ tm_map(removeWords, stopwords('english')) %>%
+ print_words() %>%
+ # tolower drops also the other the
+ tm_map(tolower) %>%
+ tm_map(removeWords, stopwords('english')) %>%
+ print_words() %>%
+ # this drops potter
+ tm_map(removeWords, 'potter') %>%
+ print_words()
best. the warden happy potter smartest. spider. think
1 2 1 1 1 1 1 1
best. the warden happy potter smartest. spider. think
1 1 1 1 1 1 1 1
best. warden happy potter smartest. spider. think
1 1 1 1 1 1 1
best. warden happy smartest. spider. think
1 1 1 1 1 1
<<SimpleCorpus>>
Metadata: corpus specific: 1, document level (indexed): 0
Content: documents: 3
Ich verstehe das langsam nicht mehr.