1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function ()
{
cat("\n")
cat("Do you want a sketch of the candidate Master?", "\n")
response <- readline(prompt = "Yes/No:")
if (response == "Yes") {
centerText(paste("Personality descripting....."))
cat("\n")
filename <- list.files(patter = "(Trump_[0-9])+|(Clinton_[0-9])+")
temp <- read.csv(filename[1], header = TRUE, nrows = 1)
for (name in filename) {
temp <- rbind(temp, read.csv(name, header = TRUE))
}
temp <- temp[-1, ]
temp$Speaker <- as.character(temp$Speaker)
temp$Speaker[grep("Clinton+", temp$Speaker)] <- "Clinton"
temp$Speaker[grep("Trump+", temp$Speaker)] <- "Trump"
temp <- temp[temp$Speaker %in% c("Clinton", "Trump"),
]
temp$Text <- as.character(temp$Text)
temp$Text <- iconv(temp$Text, to = "utf8")
traincorpus <- Corpus(VectorSource(temp$Text))
traincorpus <- tm_map(traincorpus, stripWhitespace)
traincorpus <- tm_map(traincorpus, tolower)
traincorpus <- tm_map(traincorpus, removeNumbers)
trainmatrix <- t(TermDocumentMatrix(traincorpus))
trainmatrix <- removeSparseTerms(trainmatrix, 0.99)
M_bayes <- naiveBayes(as.matrix(trainmatrix), as.factor(temp$Speaker),
laplace = 1)
fit <- predict(M_bayes, as.matrix(trainmatrix))
table(fit, temp$Speaker)
cat("Model Accuracy", "\n")
(Tab_ac <- table(fit, temp$Speaker))
(accuracy <- sum(diag(table(fit, temp$Speaker)))/sum(table(fit,
temp$Speaker)))
cat(accuracy, "\n")
return(list(Tab = Tab_ac, Accuracy = accuracy))
}
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.