Guest User

feature_loader.rb

a guest
May 27th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. class FeatureLoader
  2. attr_accessor :train_data
  3. attr_accessor :test_data
  4.  
  5. def initialize(path)
  6. self.load_data(path)
  7. end
  8.  
  9. def load_data(path)
  10. data = load_file(path).shuffle
  11. feature_space(data)
  12.  
  13. self.train_data = feature_vectors(data[0..(data.length*0.8).floor])
  14. self.test_data = feature_vectors(data[(data.length*0.8).floor..data.length])
  15. end
  16.  
  17. def load_simple_features
  18. train_data = [["r",[1,1]],
  19. ["r",[2,3]],
  20. ["r",[3,1]],
  21. ["g",[15,17]],
  22. ["g",[20,17]],
  23. ["g",[16,16]],
  24. ["b",[1,15]],
  25. ["b",[5,15]],
  26. ["b",[7,20]],
  27. ].shuffle
  28. test_data = [["r",[3,3]],
  29. ["g",[15,19]],
  30. ["b",[1,21]],
  31. ]
  32. end
  33.  
  34. def load_file(path)
  35. File.open(path, "r").map{ |line|
  36. remove_stop_words(line)
  37. }.map { |x|
  38. x.split(' ')
  39. }.map { |x|
  40. [x[0], x[1..x.length]]
  41. }
  42. end
  43.  
  44. def feature_space(data = nil)
  45. raise 'no data' unless data || @feature_space
  46. @feature_space ||= _feature_space(data)
  47. end
  48.  
  49. def _feature_space(data)
  50. data.map {|x| x[1]}.flatten.uniq.sort_by { |x| x.downcase }
  51. end
  52.  
  53. def feature_vectors(data)
  54. data.map { |x| feature_vector(x) }
  55. end
  56.  
  57. def feature_vector(review)
  58. review_text = review[1]
  59. array = empty_feature_array
  60.  
  61. for word in review_text
  62. if feature_space.include? word
  63. array[feature_space.index word] = array[feature_space.index word] + 1
  64. end
  65. end
  66. [review[0], array]
  67. end
  68.  
  69. def empty_feature_array
  70. Array.new(feature_space.length, 0)
  71. end
  72.  
  73. def stop_words
  74. ["a", "about", "above", "across", "after", "afterwards", "again",
  75. "against", "all", "almost", "alone", "along", "already", "also", "although",
  76. "always", "am", "among", "amongst", "amoungst", "amount", "an", "and",
  77. "another", "any", "anyhow", "anyone", "anything", "anyway", "anywhere", "are",
  78. "around", "as", "at", "back", "be", "became", "because", "become", "becomes",
  79. "becoming", "been", "before", "beforehand", "behind", "being", "below",
  80. "beside", "besides", "between", "beyond", "bill", "both", "bottom", "but",
  81. "by", "call", "can", "cannot", "cant", "co", "computer", "con", "could",
  82. "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due",
  83. "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere",
  84. "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything",
  85. "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire",
  86. "first", "five", "for", "former", "formerly", "forty", "found", "four",
  87. "from", "front", "full", "further", "get", "give", "go", "had", "has",
  88. "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby",
  89. "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how",
  90. "however", "hundred", "i", "ie", "if", "in", "inc", "indeed", "interest",
  91. "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly",
  92. "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might",
  93. "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must",
  94. "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next",
  95. "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now",
  96. "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or",
  97. "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over",
  98. "own", "part", "per", "perhaps", "please", "put", "rather", "re", "same",
  99. "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she",
  100. "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some",
  101. "somehow", "someone", "something", "sometime", "sometimes", "somewhere",
  102. "still", "such", "system", "take", "ten", "than", "that", "the", "their",
  103. "them", "themselves", "then", "thence", "there", "thereafter", "thereby",
  104. "therefore", "therein", "thereupon", "these", "they", "thick", "thin",
  105. "third", "this", "those", "though", "three", "through", "throughout", "thru",
  106. "thus", "to", "together", "too", "top", "toward", "towards", "twelve",
  107. "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via",
  108. "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever",
  109. "where", "whereafter", "whereas", "whereby", "wherein", "whereupon",
  110. "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole",
  111. "whom", "whose", "why", "will", "with", "within", "without", "would", "yet",
  112. "you", "your", "yours", "yourself", "yourselves"]
  113. end
  114.  
  115. def punctuation
  116. ["!", '"', "£", "$", "%", "^", "&", "*", "(", ")", "-", ".", ",", "'"]
  117. end
  118.  
  119. def remove_stop_words(review)
  120. review_text = review.split(" ").map{ |x| x.split("__") }
  121.  
  122. review_text.delete_if { |x|
  123. stop = false
  124. x.each { |y|
  125. if stop_words.include?(y) || punctuation.include?(y)
  126. stop = true
  127. end
  128. }
  129. stop
  130. }
  131.  
  132. review_text.map { |x| x.join("__") }.join(" ")
  133. end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment