library(tm)
library(SnowballC)

1 Φόρτωση & βασικά στοιχεία (2.1)

emails <- read.csv("emails.csv", stringsAsFactors = FALSE)
nrow(emails)        # πλήθος emails
## [1] 5728
sum(emails$spam)    # πλήθος spam
## [1] 1368

Η πρώτη λέξη σε όλα τα emails:

first_words <- sapply(emails$text, function(x) strsplit(trimws(x), " ")[[1]][1])
table(first_words)
## first_words
## Subject: 
##     5728

➜ Απάντηση. Η βάση περιλαμβάνει 5728 emails, εκ των οποίων 1368 είναι spam (και 4360 ham). Η πρώτη λέξη σε όλα τα emails είναι “Subject”.

2 Μήκος των emails με nchar() (2.2)

max(nchar(emails$text)); which.max(nchar(emails$text))   # μεγαλύτερο
## [1] 43952
## [1] 2651
min(nchar(emails$text)); which.min(nchar(emails$text))   # μικρότερο
## [1] 13
## [1] 1992
# Μικρότερο μήνυμα (καταχώρηση 1992)
emails$text[which.min(nchar(emails$text))]
## [1] "Subject: fyi "
# Μεγαλύτερο μήνυμα (καταχώρηση 2651) — 43.952 χαρακτήρες
emails$text[which.max(nchar(emails$text))]

➜ Απάντηση. Το πιο μακροσκελές email έχει 43.952 χαρακτήρες (καταχώρηση 2651, ξεκινά με “Subject: from the enron india newsdesk – april 27 th newsclips …”). Το πιο μικρό έχει 13 χαρακτήρες (καταχώρηση 1992: “Subject: fyi”).

Δημιουργία και επεξεργασία του corpus:

corpus <- VCorpus(VectorSource(emails$text))
corpus <- tm_map(corpus, content_transformer(tolower))      # κεφαλαία -> πεζά
corpus <- tm_map(corpus, removePunctuation)                  # εκκαθάριση ανομοιομορφιών
corpus <- tm_map(corpus, removeWords, stopwords("english"))  # αφαίρεση stopwords
corpus <- tm_map(corpus, stemDocument)                       # αποκοπή καταλήξεων
# Προεπισκόπηση 1ου εγγράφου μετά την επεξεργασία:
strwrap(as.character(corpus[[1]]), width = 90)[1:3]
## [1] "subject natur irresist corpor ident lt realli hard recollect compani market full suqgest" 
## [2] "inform isoverwhelminq good catchi logo stylish statloneri outstand websit will make task" 
## [3] "much easier promis havinq order iogo compani will automaticaili becom world ieader isguit"

3 Document Term Matrix (2.3)

dtm <- DocumentTermMatrix(corpus)
dtm                              # πλήθος όρων στο dtm
## <<DocumentTermMatrix (documents: 5728, terms: 28687)>>
## Non-/sparse entries: 481719/163837417
## Sparsity           : 100%
## Maximal term length: 24
## Weighting          : term frequency (tf)
spdtm <- removeSparseTerms(dtm, 0.95)  # κρατά όρους σε >= 5% των εγγραφών
spdtm                            # πλήθος όρων στο spdtm
## <<DocumentTermMatrix (documents: 5728, terms: 330)>>
## Non-/sparse entries: 213551/1676689
## Sparsity           : 89%
## Maximal term length: 10
## Weighting          : term frequency (tf)

➜ Απάντηση. Το dtm περιέχει 28.687 όρους. Μετά τον περιορισμό στο 5% των εγγραφών, το spdtm περιέχει 330 όρους.

4 Δημιουργία emailsSparse (2.4)

emailsSparse <- as.data.frame(as.matrix(spdtm))
colnames(emailsSparse) <- make.names(colnames(emailsSparse))
emailsSparse$spam <- emails$spam   # προσθήκη ταμπέλας spam από το αρχικό data frame
dim(emailsSparse)
## [1] 5728  331
table(emailsSparse$spam)
## 
##    0    1 
## 4360 1368

➜ Απάντηση. Το emailsSparse είναι data frame 5728 × 331 (330 όροι + η μεταβλητή spam). Κατανομή: 4360 μη-spam / 1368 spam.