Vielleicht habe ich falsch interpretiert, wie tm::DocumentTermMatrix
funktioniert. Ich habe einen Korpus, der nach der Vorverarbeitung wie folgt aussieht:TM DocumentTermMatrix gibt Ergebnisse, die beim Korpus unerwartet sind
head(Description.text, 3)
[1] "azi sanitar local to1 presid osp martin presid ospedalier martin tofan torin tel possibil raggiung ospedal segu bus tram"
[2] "torin croll controsoffitt repart pediatr martin mag cartell compars sest pian ospedal martin torin ospedal tofan sol due anno riapertur"
[3] "ospedal martin croll controsoffitt repart pediatr mag ospedal martin croll controsoffitt repart pediatr distacc intonac avven nott mattin"
, die ich über verarbeiten:
Description.text.features <- DocumentTermMatrix(Corpus(VectorSource(Description.text)), list(
bounds = list(local = c(3, Inf)),
tokenize = 'scan'
))
, als ich die erste Zeile der DTM inspizieren ich dieses:
inspect(Description.text.features[1,])
<<DocumentTermMatrix (documents: 1, terms: 887)>>
Non-/sparse entries: 0/887
Sparsity : 100%
Maximal term length: 15
Weighting : term frequency (tf)
Sample :
Terms
Docs banc camill mar martin ospedal presid san sanitar torin vittor
1 0 0 0 0 0 0 0 0 0 0
Diese Begriffe entsprechen nicht dem ersten Dokument im Korpus Description.text
(zB banc
oder camill
sind nicht im ersten Dokument und es gibt eine Null für zB martin
oder presid
welche sind).
Außerdem, wenn ich laufe:
Description.text.features[1,] %>% as.matrix() %>% sum
I Null bekommen, dass es keine Bedingungen mit der Frequenz im ersten Dokument zeigt> Null!
Was geht hier vor?
Dank
UPDATE
habe ich meine eigene "corpus DTM-Funktion und in der Tat gibt es sehr unterschiedliche Ergebnisse. Abgesehen von den Gewichtungen von Dokumenten, die sich sehr von denen von tm::DocumentTermMatrix
unterscheiden (meine sind das, was man beim Corpus erwarten würde), bekomme ich viel mehr Ausdrücke mit meiner Funktion als mit der tm-Funktion (~ 3000 vs 800 von tm).
Hier ist meine Funktion:
corpus.to.DTM <- function(corpus, min.doc.freq = 3, minlength = 3, weight.fun = weightTfIdf) {
library(dplyr)
library(magrittr)
library(tm)
library(parallel)
lvls <- mclapply(corpus, function(doc) words(doc) %>% unique, mc.cores = 8) %>%
unlist %>%
table %>%
data.frame %>%
set_colnames(c('term', 'freq')) %>%
mutate(lengths = str_length(term)) %>%
filter(freq >= min.doc.freq & lengths >= minlength) %>%
use_series(term)
dtm <- mclapply(corpus, function(doc) factor(words(doc), levels = lvls) %>% table %>% as.vector, mc.cores = 8) %>%
do.call(what = 'rbind') %>%
set_colnames(lvls)
as.DocumentTermMatrix(dtm, weighting = weightTfIdf) %>%
as.matrix() %>%
as.data.frame()
}
Dank für die Anregung! Ich werde das Paket anschauen! aber meine Frage war speziell, was mit tm los war! – Bakaburg