threaded.R 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # ##########
  2. # Written by: Deben Oldert
  3. #
  4. # Keep in mind that it took me several days/weeks and beers to make this.
  5. # So please give me some credit. Naiba and I won't bite.
  6. #
  7. # This program is called Naiba.
  8. # She can tell you and learn if movie ratings are positive or negative
  9. #
  10. # #########
  11. PROCESSES <- ifelse(!is.na(detectCores()), detectCores(), 1)
  12. sentiment.test.threaded <- function(){
  13. env <- .GlobalEnv
  14. cat("Ohh so you want to test me?\n")
  15. cat("Well come on then. Let's do this!\n\n")
  16. cat("First of all. Can you give me the test(set)?\n")
  17. set <- file.choose()
  18. set <- env$set.import(set)
  19. MAX <- nrow(set)
  20. MIN <- 1
  21. if(!console.confirm(paste("Do you want me to test all of the", as.character(nrow(set)), "records?"))){
  22. cat("Well thanks that might just saved me a huge headache.\n")
  23. repeat{
  24. MIN <- console.ask("So where do you want me to start?", type = "integer")
  25. if(MIN > 0 && MIN <= MAX) break
  26. else cat("Please enter a number bigger than 0 and smaller or equal than the records in this set.\n")
  27. }
  28. repeat{
  29. MAX <- console.ask("And where do you want me to stop?", type = "integer")
  30. if(MAX >= MIN && MAX <= nrow(set)) break
  31. else cat(paste("Please enter a number bigger or equal then", as.character(MIN), "and smaller or equal then", as.character(nrow(set))))
  32. }
  33. }
  34. score <- c()
  35. cat("*Intensive thinking* Hmmmm... (No progressbar will be shown, be patient)\n")
  36. worker <- function(i){
  37. scr <- (sentiment.calc(set[i,]$review, progress = FALSE)==as.integer(set[i,]$sentiment))
  38. return(scr)
  39. }
  40. time.start <- Sys.time()
  41. score <- mcmapply(worker, MIN:MAX, mc.cores = PROCESSES)
  42. time.end <- Sys.time()
  43. cat("Phoee... Finally done. Hope I did well...\n")
  44. cat("It took me", format(time.end - time.start, format = "%H:%M:%S"), "\n")
  45. score <- unlist(score)
  46. score <- as.integer(mean(score) * 100)
  47. if(score > 80){
  48. cat(paste0("OMG! I got ", as.character(score), "% correct!\n"))
  49. }
  50. else{
  51. cat(paste0("Hmm. I'm not happy with a score of ", as.character(score), "%\n"))
  52. }
  53. return(score)
  54. }
  55. learn.teach.threaded <- function(){
  56. env <- .GlobalEnv
  57. if(console.confirm("Do you want to train me so I can be better?")){
  58. if(exists("positive.cmb") && exists("positive.cnt") && exists("negative.cmb") && exists("negative.cnt")){
  59. cat("Hmmm... I already know someting.\n")
  60. if(!console.confirm("Do you want me to continue to learn? (Append learning skillset)")){
  61. env$positive.cmb <- c()
  62. env$positive.cnt <- c()
  63. env$negative.cmb <- c()
  64. env$negative.cnt <- c()
  65. }
  66. }
  67. else{
  68. env$positive.cmb <- c()
  69. env$positive.cnt <- c()
  70. env$negative.cmb <- c()
  71. env$negative.cnt <- c()
  72. }
  73. set <- file.choose()
  74. set <- set.import(set)
  75. MAX <- nrow(set)
  76. MIN <- 1
  77. if(!console.confirm(paste("Do you want me to learn all of the", as.character(nrow(set)), "records?"))){
  78. cat("Well thanks that might just saved me a huge headache.\n")
  79. repeat{
  80. MIN <- console.ask("So where do you want me to start?", type = "integer")
  81. if(MIN > 0 && MIN <= MAX) break
  82. else cat("Please enter a number bigger than 0 and smaller or equal than ", as.character(MAX), "\n")
  83. }
  84. repeat{
  85. MAX <- console.ask("And where do you want me to stop?", type = "integer")
  86. if(MAX >= MIN && MAX <= nrow(set)) break
  87. else cat(paste("Please enter a number bigger or equal then", as.character(MIN), "and smaller or equal then", as.character(nrow(set))))
  88. }
  89. }
  90. worker <- function(x){
  91. prt <- as.integer((MAX - MIN + 1) / PROCESSES)
  92. if(x < PROCESSES) prt <- (((x - 1) * prt) + 1):(x * prt)
  93. else prt <- (((x - 1) * prt) + 1):MAX
  94. pos.cmb <- c()
  95. pos.cnt <- c()
  96. neg.cmb <- c()
  97. neg.cnt <- c()
  98. for(i in prt){
  99. if(set[i,]$sentiment == 1){
  100. pos <- sentiment.train.threaded(set[i,]$review, pos.cmb, pos.cnt)
  101. pos.cmb <- pos$cmb
  102. pos.cnt <- pos$cnt
  103. }
  104. else{
  105. neg <- sentiment.train.threaded(set[i,]$review, neg.cmb, neg.cnt)
  106. neg.cmb <- neg$cmb
  107. neg.cnt <- neg$cnt
  108. }
  109. }
  110. pos <- data.frame(positive.cmb=pos.cmb, positive.cnt=pos.cnt)
  111. neg <- data.frame(negative.cmb=neg.cmb, negative.cnt=neg.cnt)
  112. return(c(pos, neg))
  113. #return(list(pos.cmb=pos.cmb, pos.cnt=pos.cnt, neg.cmb=neg.cmb, neg.cnt=neg.cnt))
  114. }
  115. cat("Getting smarter... (No progressbar will be shown, be patient)\n")
  116. env$answer <- mcmapply(worker, 1:PROCESSES)
  117. if(console.confirm("Let me catch some breath here. Do you want me to remeber this training?")) learn.save()
  118. }
  119. cat("Now that I know everything. There is one thing you should learn.\n")
  120. cat("If you want me to analyse a review just call:\n\n")
  121. cat("sentiment.calc(<any text>)\n\n")
  122. cat("Now let's get started!\n")
  123. }
  124. sentiment.train.threaded <- function(str, cmb, cnt){
  125. spl <- sentiment.split(toupper(str))
  126. if((length(spl)-SENSITIVITY+1) < 1) {
  127. cat("You have to train me with more text.\n")
  128. return(list(cmb=cmb, cnt=cnt))
  129. }
  130. for(i in 1:(length(spl)-SENSITIVITY+1)){
  131. grp <- paste(spl[i:(i+SENSITIVITY-1)], collapse = ' ')
  132. mtc <- match(grp, cmb)
  133. if(!is.na(mtc)){
  134. cnt[mtc] <- cnt[mtc] + 1
  135. }
  136. else{
  137. cmb <- c(cmb, grp)
  138. cnt <- c(cnt, 1)
  139. }
  140. }
  141. return(list(cmb=cmb, cnt=cnt))
  142. }