main.R 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. library(readr)
  2. library(dplyr)
  3. library(stringr)
  4. library(parallel)
  5. source("threaded.R")
  6. SENSITIVITY <- 4
  7. #MAX <- nrow(DATA1)
  8. MAX <- 500
  9. LEARNED.POSITIVE <- paste(getwd(), "learned_positive.csv", sep = "/")
  10. LEARNED.NEGATIVE <- paste(getwd(), "learned_negative.csv", sep = "/")
  11. cat("Hey there! My name is Naiba. Nice to meet you.\n")
  12. cat("Thanks to the magic of multi-threading I have", PROCESSES, "brains (CPU). But this only works under a UNIX environment (e.g. MacOS).\n")
  13. cat("Don't you even dare to call the *.threaded functions in a windwows environment.\n")
  14. cat("If you have any question about me, just go to:\n")
  15. cat("https://github.com/DebenOldert/Big-Data-Review-Analysis/blob/master/README.md\n")
  16. cat("But that's enough trashtalk. Let's do this!")
  17. sentiment.train <- function(str, sen){
  18. env <- .GlobalEnv
  19. spl <- sentiment.split(toupper(str))
  20. if((length(spl)-SENSITIVITY+1) < 1) {
  21. cat("You have to train me with more text.\n")
  22. return()
  23. }
  24. for(i in 1:(length(spl)-SENSITIVITY+1)){
  25. grp <- paste(spl[i:(i+SENSITIVITY-1)], collapse = ' ')
  26. if(sen == 1){
  27. mtc <- match(grp, env$positive.cmb)
  28. if(!is.na(mtc)){
  29. env$positive.cnt[mtc] <- env$positive.cnt[mtc] + 1
  30. }
  31. else{
  32. env$positive.cmb <- c(env$positive.cmb, grp)
  33. env$positive.cnt <- c(env$positive.cnt, 1)
  34. }
  35. }
  36. else{
  37. mtc <- match(grp, env$negative.cmb)
  38. if(!is.na(mtc)){
  39. env$negative.cnt[mtc] <- env$negative.cnt[mtc] + 1
  40. }
  41. else{
  42. env$negative.cmb <- c(env$negative.cmb, grp)
  43. env$negative.cnt <- c(env$negative.cnt, 1)
  44. }
  45. }
  46. }
  47. }
  48. sentiment.test <- function(){
  49. env <- .GlobalEnv
  50. cat("Ohh so you want to test me?\n")
  51. cat("Well come on then. Let's do this!\n\n")
  52. cat("First of all. Can you give me the test(set)?\n")
  53. set <- file.choose()
  54. set <- env$set.import(set)
  55. MAX <- nrow(set)
  56. MIN <- 1
  57. if(!console.confirm(paste("Do you want me to test all of the", as.character(nrow(set)), "records?"))){
  58. cat("Well thanks that might just saved me a huge headache.\n")
  59. repeat{
  60. MIN <- console.ask("So where do you want me to start?", type = "integer")
  61. if(MIN > 0 && MIN <= MAX) break
  62. else cat("Please enter a number bigger than 0 and smaller or equal than the records in this set.\n")
  63. }
  64. repeat{
  65. MAX <- console.ask("And where do you want me to stop?", type = "integer")
  66. if(MAX >= MIN && MAX <= nrow(set)) break
  67. else cat(paste("Please enter a number bigger or equal then", as.character(MIN), "and smaller or equal then", as.character(nrow(set))))
  68. }
  69. }
  70. score <- c()
  71. time.start <- Sys.time()
  72. cat("*Intensive thinking* Hmmmm...\n")
  73. progress <- txtProgressBar(min = (MIN - 1), max = MAX, style = 3)
  74. setTxtProgressBar(progress, (MIN - 1))
  75. for(i in MIN:MAX){
  76. test <- sentiment.calc(set[i,]$review, progress = FALSE)
  77. score <- c(score, (test==as.integer(set[i,]$sentiment)))
  78. setTxtProgressBar(progress, i)
  79. }
  80. time.end <- Sys.time()
  81. close(progress)
  82. cat("Phoee... Finally done. Hope I did well...\n")
  83. cat("It took me", format(time.end - time.start, format = "%H:%M:%S"), "\n")
  84. score <- as.integer(mean(score)*100)
  85. if(score > 80){
  86. cat(paste0("OMG! I got ", as.character(score), "% correct!\n"))
  87. }
  88. else{
  89. cat(paste0("Hmm. I'm not quite happy with a score of ", as.character(score), "%\n"))
  90. }
  91. return(score)
  92. }
  93. sentiment.calc <- function(str, progress=TRUE) {
  94. env <- .GlobalEnv
  95. spl <- sentiment.split(toupper(str))
  96. pos <- 0
  97. neg <- 0
  98. data <- data.frame(grp=character())
  99. if((length(spl)-SENSITIVITY+1) < 1){
  100. stop("I really need some more text to figure this one out.\n")
  101. }
  102. if(progress){
  103. time.start <- Sys.time()
  104. cat("Let me think...\n")
  105. prog <- txtProgressBar(0, (length(spl)-SENSITIVITY+1), style = 3)
  106. }
  107. for(i in 1:(length(spl)-SENSITIVITY+1)){
  108. grp <- paste(spl[i:(i+SENSITIVITY-1)], collapse = ' ')
  109. mtc <- match(grp, env$positive.cmb)
  110. if(!is.na(mtc)){
  111. pos <- pos + env$positive.cnt[mtc]
  112. }
  113. mtc <- match(grp, env$negative.cmb)
  114. if(!is.na(mtc)){
  115. neg <- neg + env$negative.cnt[mtc]
  116. }
  117. # if(nrow(env$positive[env$positive==grp,]) == 1){
  118. # pos <- pos + env$positive[env$positive==grp,]$cnt
  119. # }
  120. # if(nrow(env$negative[env$negative==grp,]) == 1){
  121. # neg <- neg + env$negative[env$negative==grp,]$cnt
  122. # }
  123. if(progress) setTxtProgressBar(prog, i)
  124. }
  125. if(progress){
  126. close(prog)
  127. time.end <- Sys.time()
  128. cat("It took me", format(time.end - time.start, format = "%H:%M:%S"), "\n")
  129. if(pos >= neg){
  130. cat("This must be a POSITIVE review\n")
  131. }
  132. else{
  133. cat("This must be a NEGATIVE review\n")
  134. }
  135. }
  136. print(pos)
  137. print(neg)
  138. return(pos >= neg)
  139. }
  140. sentiment.split <- function(str){
  141. return(na.omit(unlist(strsplit(unlist(str), "[^a-zA-Z]+"))))
  142. }
  143. learn.save <- function(){
  144. env <- .GlobalEnv
  145. if(!file.exists(LEARNED.POSITIVE) || console.confirm("Positive learned file already exists. Overwrite?")){
  146. pos <- data.frame(cmb=env$positive.cmb, cnt=env$positive.cnt)
  147. write_csv(pos, LEARNED.POSITIVE)
  148. }
  149. if(!file.exists(LEARNED.NEGATIVE) || console.confirm("Negative learned file already exists. Overwrite?")){
  150. neg <- data.frame(cmb=env$negative.cmb, cnt=env$negative.cnt)
  151. write_csv(neg, LEARNED.NEGATIVE)
  152. }
  153. }
  154. learn.load <- function(){
  155. env <- .GlobalEnv
  156. if(file.exists(LEARNED.POSITIVE) && file.exists(LEARNED.NEGATIVE)){
  157. if(console.confirm("I found out that I already learned something a while ago. Do you want to use that data?")){
  158. pos <- read_csv(LEARNED.POSITIVE)
  159. neg <- read_csv(LEARNED.NEGATIVE)
  160. env$positive.cmb <- pos$cmb
  161. env$positive.cnt <- pos$cnt
  162. env$negative.cmb <- neg$cmb
  163. env$negative.cnt <- neg$cnt
  164. return(TRUE)
  165. }
  166. }
  167. return(FALSE)
  168. }
  169. console.confirm <- function(str){
  170. repeat{
  171. ans <- readline(prompt = paste(str, "[Y|N]: "))
  172. if(ans == "Y") return(TRUE)
  173. if(ans == "N") return(FALSE)
  174. cat("Enter Y or N. Let's try it again.\n")
  175. }
  176. }
  177. console.ask <- function(str, type="string"){
  178. repeat{
  179. ans <- readline(prompt = paste0(str, " [", type, "]: "))
  180. if(type == "string"){
  181. return(ans)
  182. }
  183. if(type == "integer"){
  184. if(grepl("^[0-9]+$", ans)){
  185. return(as.integer(ans))
  186. }
  187. }
  188. cat(paste(type, "only please!", "\n"))
  189. }
  190. }
  191. set.import <- function(fullPath){
  192. suppressMessages(suppressWarnings(
  193. if(endsWith(fullPath, ".tsv")) return(read_delim(fullPath, "\t", escape_backslash = TRUE, escape_double = FALSE, trim_ws = TRUE))
  194. else if(endsWith(fullPath, ".csv")) return(read_csv(fullPath, trim_ws = TRUE))
  195. ))
  196. }
  197. learn.teach <- function(){
  198. if(console.confirm("Do you want to train me so I can be better?")){
  199. if(exists("positive.cmb") && exists("positive.cnt") && exists("negative.cmb") && exists("negative.cnt")){
  200. cat("Hmmm... I already know someting.\n")
  201. if(!console.confirm("Do you want me to continue to learn? (Append learning skillset)")){
  202. positive.cmb <- c()
  203. positive.cnt <- c()
  204. negative.cmb <- c()
  205. negative.cnt <- c()
  206. }
  207. }
  208. else{
  209. positive.cmb <- c()
  210. positive.cnt <- c()
  211. negative.cmb <- c()
  212. negative.cnt <- c()
  213. }
  214. set <- file.choose()
  215. set <- set.import(set)
  216. MAX <- nrow(set)
  217. MIN <- 1
  218. if(!console.confirm(paste("Do you want me to learn all of the", as.character(nrow(set)), "records?"))){
  219. cat("Well thanks that might just saved me a huge headache.\n")
  220. repeat{
  221. MIN <- console.ask("So where do you want me to start?", type = "integer")
  222. if(MIN > 0 && MIN <= MAX) break
  223. else cat("Please enter a number bigger than 0 and smaller or equal than ", as.character(MAX), "\n")
  224. }
  225. repeat{
  226. MAX <- console.ask("And where do you want me to stop?", type = "integer")
  227. if(MAX >= MIN && MAX <= nrow(set)) break
  228. else cat(paste("Please enter a number bigger or equal then", as.character(MIN), "and smaller or equal then", as.character(nrow(set))))
  229. }
  230. }
  231. cat("Getting smarter...\n")
  232. progress <- txtProgressBar((MIN-1), MAX, style = 3)
  233. setTxtProgressBar(progress, (MIN-1))
  234. for(i in MIN:MAX){
  235. sentiment.train(set[i,]$review, as.integer(set[i,]$sentiment))
  236. setTxtProgressBar(progress, i)
  237. }
  238. close(progress)
  239. if(console.confirm("Let me catch some breath here. Do you want me to remeber this training?")) learn.save()
  240. }
  241. cat("Now that I know everything. There is one thing you should learn.\n")
  242. cat("If you want me to analyse a review just call:\n\n")
  243. cat("sentiment.calc(<any text>)\n\n")
  244. cat("Now let's get started!\n")
  245. }