#定义函数, open each file, find a blank line, and return the text after the empty line as a string vector with only one element, which is the string after all the text after the empty line is stitched
#很多邮件都包含了非ASCII字符, so you can read non-ASCII characters by setting it to Latin1
#readLines, read each line as an element
Get.msg <-function (path)
{
Con <-file (path, open = "RT")
Text <-readlines (con)
# The message always begins after the first
MSG <-TEXT[SEQ (which (Text = = "") [1] + 1, Length (text), 1)]
Close (Con)
Return (Paste (msg, collapse = "\ n"))
}
#dir读取目录下所有文件
Spam.docs<-dir (Spam.path)
#去掉目录下的cmds文件
Spam.docs<-spam.docs[which (spam.docs!= ' Cmds ')]
#利用get. msg function to read the entire contents of a blank line of each message and form a vector of text
All.spam<-sapply (Spam.docs,function (P) get.msg (Paste (spam.path,p,sep= ")))
#定义函数get. TDM, input message text vector, output Word Item document matrix
GET.TDM <-function (Doc.vec)
{
Control <-List (stopwords = TRUE,
Removepunctuation = TRUE,
RemoveNumbers = TRUE,
Mindocfreq = 2)
Doc.corpus <-Corpus (Vectorsource (Doc.vec))
Doc.dtm <-Termdocumentmatrix (Doc.corpus, control)
Return (DOC.DTM)
}
Can't write down, according to the book to the program can not run, may be operating system differences, error is not know which function in the ToLower
Even ToLower will make mistakes, really no words, it is a statistical science written by the software
Machine learning for hackers reading notes (iii) Category: Garbage filter (unfinished)