Machine learning for hackers reading notes (iii) Category: Garbage filter (unfinished)

Source: Internet
Author: User

#定义函数, 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.