threaded.R 5.2 KB

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