From 94fb5ecc23337b4eea68da1ae4941c5e97567ffd Mon Sep 17 00:00:00 2001 From: Derek Ellis Date: Sun, 3 Aug 2014 20:55:56 -0500 Subject: [PATCH 1/3] to push --- lib/complex_predicton.rb | 71 ++++++++++++++++ lib/complex_predicton4.rb | 70 +++++++++++++++ lib/complex_predictor.rb | 79 +++++++++++++---- lib/complex_predictor2.rb | 171 +++++++++++++++++++++++++++++++++++++ lib/complex_predictor3.rb | 173 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 549 insertions(+), 15 deletions(-) create mode 100644 lib/complex_predicton.rb create mode 100644 lib/complex_predicton4.rb create mode 100644 lib/complex_predictor2.rb create mode 100644 lib/complex_predictor3.rb diff --git a/lib/complex_predicton.rb b/lib/complex_predicton.rb new file mode 100644 index 0000000..80b7591 --- /dev/null +++ b/lib/complex_predicton.rb @@ -0,0 +1,71 @@ +require_relative 'predictor' + +class ComplexPredictor < Predictor + + + def build(category) + length = @all_books[category].length - 1 + words_hash = Hash.new(0) + + for num in 0..length + for word in @all_books[category][num][1] + words_hash[word] += 1 if good_token?(word) == true + end + end + + return_array = [] + + return_hash = words_hash.sort_by{|key, value| value}.reverse[0..200] + return_hash.each { |k,v| return_array << k } + + return return_array + end + + def score(tokens_list) + score = 0 + length = tokens_list.length - 1 + for num in 0..length + @bookarr.each { + |word| score += 1 if word == tokens_list[num] } + end + return score + end + + def train! + # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} + @archarr = build(:archeology) + @astroarr = build(:astronomy) + @philarr = build(:philosophy) + @relarr = build(:religion) + end + + def predict(tokens) + @goodtokens = [] + tokens.each do |word| + @goodtokens << word if good_token?(word) == true + end + testhash = Hash.new(0) + @goodtokens.each do |word| + testhash[word] += 1 + end + + newtesthash = testhash.sort_by{|key, value| value}.reverse[0..200] + @bookarr = [] + newtesthash.each { |k,v| @bookarr << k } + + scores = [score(@philarr), score(@astroarr), score(@archarr), score(@relarr)] + case + when scores.max == scores.first + return :philosophy + when scores.max == scores[1] + return :astronomy + when scores.max == scores[2] + return :archeology + when scores.max == scores[3] + return :religion + end + + + end + +end diff --git a/lib/complex_predicton4.rb b/lib/complex_predicton4.rb new file mode 100644 index 0000000..3414880 --- /dev/null +++ b/lib/complex_predicton4.rb @@ -0,0 +1,70 @@ +require_relative 'predictor' + +class ComplexPredictor < Predictor + + + def build(category) + + length = @all_books[category].length - 1 + words_hash = Hash.new(0) + + for num in 0..length + for word in @all_books[category][num][1] + words_hash[word] += 1 if good_token?(word) == true + end + end + + return_array = [] + return_hash = words_hash.sort_by{|key, value| value}.reverse[0..200] + return_hash.each { |k,v| return_array << k } + return return_array + end + + def score(tokens_list) + score = 0 + length = tokens_list.length - 1 + for num in 0..length + @bookarr.each { + |word| score += 1 if word == tokens_list[num] } + end + return score + end + + def train! + # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} + @archarr = build(:archeology) + @astroarr = build(:astronomy) + @philarr = build(:philosophy) + @relarr = build(:religion) + end + + def predict(tokens) + @goodtokens = [] + tokens.each do |word| + @goodtokens << word if good_token?(word) == true + end + testhash = Hash.new(0) + @goodtokens.each do |word| + testhash[word] += 1 + end + + newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] + @bookarr = [] + newtesthash.each { |k,v| @bookarr << k } + + scores = [score(@philarr), score(@astroarr), score(@archarr), score(@relarr)] + case + when scores.max == scores.first + return :philosophy + when scores.max == scores[1] + return :astronomy + when scores.max == scores[2] + return :archeology + when scores.max == scores[3] + return :religion + end + + + end + +end diff --git a/lib/complex_predictor.rb b/lib/complex_predictor.rb index b8921f3..1e15c48 100644 --- a/lib/complex_predictor.rb +++ b/lib/complex_predictor.rb @@ -1,22 +1,71 @@ require_relative 'predictor' class ComplexPredictor < Predictor - # Public: Trains the predictor on books in our dataset. This method is called - # before the predict() method is called. - # - # Returns nothing. - def train! - @data = {} + + + def build(category) + + length = @all_books[category].length - 1 + words_hash = Hash.new(0) + + for num in 0..length + for word in @all_books[category][num][1] + words_hash[word] += 1 if good_token?(word) == true + end + end + + return_array = [] + return_hash = words_hash.sort_by{|key, value| value}.reverse[0..200] + return_hash.each { |k,v| return_array << k } + return return_array end - # Public: Predicts category. - # - # tokens - A list of tokens (words). - # - # Returns a category. - def predict(tokens) - # Always predict astronomy, for now. - :astronomy + def score(tokens_list) + score = 0 + length = tokens_list.length - 1 + for num in 0..length + @bookarr.each { + |word| score += 1 if word == tokens_list[num] } + end + return score end -end + def train! + # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} + @archarr = build(:archeology) + @astroarr = build(:astronomy) + @philarr = build(:philosophy) + @relarr = build(:religion) + end + + def predict(tokens) + @goodtokens = [] + tokens.each do |word| + @goodtokens << word if good_token?(word) == true + end + testhash = Hash.new(0) + @goodtokens.each do |word| + testhash[word] += 1 + end + # piss + + newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] + @bookarr = [] + newtesthash.each { |k,v| @bookarr << k } +# shoot flub + scores = [score(@philarr), score(@astroarr), score(@archarr), score(@relarr)] + case + when scores.max == scores.first + return :philosophy + when scores.max == scores[1] + return :astronomy + when scores.max == scores[2] + return :archeology + when scores.max == scores[3] + return :religion + end +# :( + + end + +end diff --git a/lib/complex_predictor2.rb b/lib/complex_predictor2.rb new file mode 100644 index 0000000..f890ec7 --- /dev/null +++ b/lib/complex_predictor2.rb @@ -0,0 +1,171 @@ +require_relative 'predictor' + +class ComplexPredictor < Predictor + + def train! + + @archwords = [] + + (0..(@all_books[:archeology].length - 1)).each do |num| + @all_books[:archeology][num][1].each do |word| + @archwords << word if good_token?(word) == true + end + end + + archhash = {} + + @archwords.each do |word| + if archhash[word] == nil + archhash[word] = 1 + else + archhash[word] += 1 + end + end + + newarchhash = archhash.sort_by{|k,v| v}.reverse[0..70] + + @archarr = [] + newarchhash.each { |k,v| @archarr << k } +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + @astrowords = [] + (0..(@all_books[:astronomy].length - 1)).each do |num| + @all_books[:astronomy][num][1].each do |word| + @astrowords << word if good_token?(word) == true + end + end + + astrohash = {} + + @astrowords.each do |word| + if astrohash[word] == nil + astrohash[word] = 1 + else + astrohash[word] += 1 + end + end + + newastrohash = astrohash.sort_by{|k,v| v}.reverse[0..70] + + @astroarr = [] + newastrohash.each { |k,v| @astroarr << k } + +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + @philwords = [] + (0..(@all_books[:philosophy].length - 1)).each do |num| + @all_books[:philosophy][num][1].each do |word| + @philwords << word if good_token?(word) == true + end + end + + philhash = {} + + @philwords.each do |word| + if philhash[word] == nil + philhash[word] = 1 + else + philhash[word] += 1 + end + end + + newphilhash = philhash.sort_by{|k,v| v}.reverse[0..70] + + @philarr = [] + newphilhash.each { |k,v| @philarr << k } + +#----------------------------- + + @relwords = [] + (0..(@all_books[:religion].length - 1)).each do |num| + @all_books[:religion][num][1].each do |word| + @relwords << word if good_token?(word) == true + end + end + + relhash = {} + + @relwords.each do |word| + if relhash[word] == nil + relhash[word] = 1 + else + relhash[word] += 1 + end + end + + newrelhash = relhash.sort_by{|k,v| v}.reverse[0..70] + + + @relarr = [] + newrelhash.each { |k,v| @relarr << k } + + end + + def predict(tokens) + @goodtokens = [] + tokens.each do |word| + @goodtokens << word if good_token?(word) == true + end + testhash = {} + @goodtokens.each do |word| + if testhash[word] == nil + testhash[word] = 1 + else + testhash[word] += 1 + end + end + + + newtesthash = testhash.sort_by{|k,v| v}.reverse[0..70] + @bookarr = [] + newtesthash.each { |k,v| @bookarr << k } + + philscore = 0 + + (0..@philarr.length - 1).each do |num| + @bookarr.each do |word| + philscore += 1 if word == @philarr[num] + end + end + + + astroscore = 0 + + (0..@astroarr.length - 1).each do |num| + @bookarr.each do |word| + astroscore += 1 if word == @astroarr[num] + end + end + + + archscore = 0 + (0..@archarr.length - 1).each do |num| + @bookarr.each do |word| + archscore += 1 if word == @archarr[num] + end + end + + relscore = 0 + + (0..@relarr.length - 1).each do |num| + @bookarr.each do |word| + relscore += 1 if word == @relarr[num] + end + end + + scores = [philscore, astroscore, archscore, relscore] + case + when scores.max == philscore + return :philosophy + when scores.max == astroscore + return :astronomy + when scores.max == archscore + return :archeology + when scores.max == relscore + return :religion + end + + + end + +end diff --git a/lib/complex_predictor3.rb b/lib/complex_predictor3.rb new file mode 100644 index 0000000..0a9c9ad --- /dev/null +++ b/lib/complex_predictor3.rb @@ -0,0 +1,173 @@ +require_relative 'predictor' + +class ComplexPredictor < Predictor + + def train! + + # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} + + @archwords = [] + + (0..(@all_books[:archeology].length - 1)).each do |num| + @all_books[:archeology][num][1].each do |word| + @archwords << word if good_token?(word) == true + end + end + + archhash = {} + + @archwords.each do |word| + if archhash[word] == nil + archhash[word] = 1 + else + archhash[word] += 1 + end + end + + newarchhash = archhash.sort_by{|key, value| value}.reverse[0..60] + + @archarr = [] + newarchhash.each { |k,v| @archarr << k } +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + @astrowords = [] + (0..(@all_books[:astronomy].length - 1)).each do |num| + @all_books[:astronomy][num][1].each do |word| + @astrowords << word if good_token?(word) == true + end + end + + astrohash = {} + + @astrowords.each do |word| + if astrohash[word] == nil + astrohash[word] = 1 + else + astrohash[word] += 1 + end + end + + newastrohash = astrohash.sort_by{|key, value| value}.reverse[0..60] + + @astroarr = [] + newastrohash.each { |k,v| @astroarr << k } + +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + @philwords = [] + (0..(@all_books[:philosophy].length - 1)).each do |num| + @all_books[:philosophy][num][1].each do |word| + @philwords << word if good_token?(word) == true + end + end + + philhash = {} + + @philwords.each do |word| + if philhash[word] == nil + philhash[word] = 1 + else + philhash[word] += 1 + end + end + + newphilhash = philhash.sort_by{|key, value| value}.reverse[0..60] + + @philarr = [] + newphilhash.each { |k,v| @philarr << k } + +#----------------------------- + + @relwords = [] + (0..(@all_books[:religion].length - 1)).each do |num| + @all_books[:religion][num][1].each do |word| + @relwords << word if good_token?(word) == true + end + end + + relhash = {} + + @relwords.each do |word| + if relhash[word] == nil + relhash[word] = 1 + else + relhash[word] += 1 + end + end + + newrelhash = relhash.sort_by{|key, value| value}.reverse[0..60] + + + @relarr = [] + newrelhash.each { |k,v| @relarr << k } + + end + + def predict(tokens) + @goodtokens = [] + tokens.each do |word| + @goodtokens << word if good_token?(word) == true + end + testhash = {} + @goodtokens.each do |word| + if testhash[word] == nil + testhash[word] = 1 + else + testhash[word] += 1 + end + end + + + newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] + @bookarr = [] + newtesthash.each { |k,v| @bookarr << k } + + philscore = 0 + + (0..@philarr.length - 1).each do |num| + @bookarr.each do |word| + philscore += 1 if word == @philarr[num] + end + end + + + astroscore = 0 + + (0..@astroarr.length - 1).each do |num| + @bookarr.each do |word| + astroscore += 1 if word == @astroarr[num] + end + end + + + archscore = 0 + (0..@archarr.length - 1).each do |num| + @bookarr.each do |word| + archscore += 1 if word == @archarr[num] + end + end + + relscore = 0 + + (0..@relarr.length - 1).each do |num| + @bookarr.each do |word| + relscore += 1 if word == @relarr[num] + end + end + + scores = [philscore, astroscore, archscore, relscore] + case + when scores.max == philscore + return :philosophy + when scores.max == astroscore + return :astronomy + when scores.max == archscore + return :archeology + when scores.max == relscore + return :religion + end + + + end + +end From b83ab408be298ace151d5df751fa4545a5941159 Mon Sep 17 00:00:00 2001 From: Derek Ellis Date: Sun, 3 Aug 2014 20:58:45 -0500 Subject: [PATCH 2/3] to push/pull --- lib/complex_predicton.rb | 71 ---------------- lib/complex_predicton4.rb | 70 --------------- lib/complex_predictor2.rb | 171 ------------------------------------- lib/complex_predictor3.rb | 173 -------------------------------------- 4 files changed, 485 deletions(-) delete mode 100644 lib/complex_predicton.rb delete mode 100644 lib/complex_predicton4.rb delete mode 100644 lib/complex_predictor2.rb delete mode 100644 lib/complex_predictor3.rb diff --git a/lib/complex_predicton.rb b/lib/complex_predicton.rb deleted file mode 100644 index 80b7591..0000000 --- a/lib/complex_predicton.rb +++ /dev/null @@ -1,71 +0,0 @@ -require_relative 'predictor' - -class ComplexPredictor < Predictor - - - def build(category) - length = @all_books[category].length - 1 - words_hash = Hash.new(0) - - for num in 0..length - for word in @all_books[category][num][1] - words_hash[word] += 1 if good_token?(word) == true - end - end - - return_array = [] - - return_hash = words_hash.sort_by{|key, value| value}.reverse[0..200] - return_hash.each { |k,v| return_array << k } - - return return_array - end - - def score(tokens_list) - score = 0 - length = tokens_list.length - 1 - for num in 0..length - @bookarr.each { - |word| score += 1 if word == tokens_list[num] } - end - return score - end - - def train! - # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} - @archarr = build(:archeology) - @astroarr = build(:astronomy) - @philarr = build(:philosophy) - @relarr = build(:religion) - end - - def predict(tokens) - @goodtokens = [] - tokens.each do |word| - @goodtokens << word if good_token?(word) == true - end - testhash = Hash.new(0) - @goodtokens.each do |word| - testhash[word] += 1 - end - - newtesthash = testhash.sort_by{|key, value| value}.reverse[0..200] - @bookarr = [] - newtesthash.each { |k,v| @bookarr << k } - - scores = [score(@philarr), score(@astroarr), score(@archarr), score(@relarr)] - case - when scores.max == scores.first - return :philosophy - when scores.max == scores[1] - return :astronomy - when scores.max == scores[2] - return :archeology - when scores.max == scores[3] - return :religion - end - - - end - -end diff --git a/lib/complex_predicton4.rb b/lib/complex_predicton4.rb deleted file mode 100644 index 3414880..0000000 --- a/lib/complex_predicton4.rb +++ /dev/null @@ -1,70 +0,0 @@ -require_relative 'predictor' - -class ComplexPredictor < Predictor - - - def build(category) - - length = @all_books[category].length - 1 - words_hash = Hash.new(0) - - for num in 0..length - for word in @all_books[category][num][1] - words_hash[word] += 1 if good_token?(word) == true - end - end - - return_array = [] - return_hash = words_hash.sort_by{|key, value| value}.reverse[0..200] - return_hash.each { |k,v| return_array << k } - return return_array - end - - def score(tokens_list) - score = 0 - length = tokens_list.length - 1 - for num in 0..length - @bookarr.each { - |word| score += 1 if word == tokens_list[num] } - end - return score - end - - def train! - # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} - @archarr = build(:archeology) - @astroarr = build(:astronomy) - @philarr = build(:philosophy) - @relarr = build(:religion) - end - - def predict(tokens) - @goodtokens = [] - tokens.each do |word| - @goodtokens << word if good_token?(word) == true - end - testhash = Hash.new(0) - @goodtokens.each do |word| - testhash[word] += 1 - end - - newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] - @bookarr = [] - newtesthash.each { |k,v| @bookarr << k } - - scores = [score(@philarr), score(@astroarr), score(@archarr), score(@relarr)] - case - when scores.max == scores.first - return :philosophy - when scores.max == scores[1] - return :astronomy - when scores.max == scores[2] - return :archeology - when scores.max == scores[3] - return :religion - end - - - end - -end diff --git a/lib/complex_predictor2.rb b/lib/complex_predictor2.rb deleted file mode 100644 index f890ec7..0000000 --- a/lib/complex_predictor2.rb +++ /dev/null @@ -1,171 +0,0 @@ -require_relative 'predictor' - -class ComplexPredictor < Predictor - - def train! - - @archwords = [] - - (0..(@all_books[:archeology].length - 1)).each do |num| - @all_books[:archeology][num][1].each do |word| - @archwords << word if good_token?(word) == true - end - end - - archhash = {} - - @archwords.each do |word| - if archhash[word] == nil - archhash[word] = 1 - else - archhash[word] += 1 - end - end - - newarchhash = archhash.sort_by{|k,v| v}.reverse[0..70] - - @archarr = [] - newarchhash.each { |k,v| @archarr << k } -# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - @astrowords = [] - (0..(@all_books[:astronomy].length - 1)).each do |num| - @all_books[:astronomy][num][1].each do |word| - @astrowords << word if good_token?(word) == true - end - end - - astrohash = {} - - @astrowords.each do |word| - if astrohash[word] == nil - astrohash[word] = 1 - else - astrohash[word] += 1 - end - end - - newastrohash = astrohash.sort_by{|k,v| v}.reverse[0..70] - - @astroarr = [] - newastrohash.each { |k,v| @astroarr << k } - -# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - @philwords = [] - (0..(@all_books[:philosophy].length - 1)).each do |num| - @all_books[:philosophy][num][1].each do |word| - @philwords << word if good_token?(word) == true - end - end - - philhash = {} - - @philwords.each do |word| - if philhash[word] == nil - philhash[word] = 1 - else - philhash[word] += 1 - end - end - - newphilhash = philhash.sort_by{|k,v| v}.reverse[0..70] - - @philarr = [] - newphilhash.each { |k,v| @philarr << k } - -#----------------------------- - - @relwords = [] - (0..(@all_books[:religion].length - 1)).each do |num| - @all_books[:religion][num][1].each do |word| - @relwords << word if good_token?(word) == true - end - end - - relhash = {} - - @relwords.each do |word| - if relhash[word] == nil - relhash[word] = 1 - else - relhash[word] += 1 - end - end - - newrelhash = relhash.sort_by{|k,v| v}.reverse[0..70] - - - @relarr = [] - newrelhash.each { |k,v| @relarr << k } - - end - - def predict(tokens) - @goodtokens = [] - tokens.each do |word| - @goodtokens << word if good_token?(word) == true - end - testhash = {} - @goodtokens.each do |word| - if testhash[word] == nil - testhash[word] = 1 - else - testhash[word] += 1 - end - end - - - newtesthash = testhash.sort_by{|k,v| v}.reverse[0..70] - @bookarr = [] - newtesthash.each { |k,v| @bookarr << k } - - philscore = 0 - - (0..@philarr.length - 1).each do |num| - @bookarr.each do |word| - philscore += 1 if word == @philarr[num] - end - end - - - astroscore = 0 - - (0..@astroarr.length - 1).each do |num| - @bookarr.each do |word| - astroscore += 1 if word == @astroarr[num] - end - end - - - archscore = 0 - (0..@archarr.length - 1).each do |num| - @bookarr.each do |word| - archscore += 1 if word == @archarr[num] - end - end - - relscore = 0 - - (0..@relarr.length - 1).each do |num| - @bookarr.each do |word| - relscore += 1 if word == @relarr[num] - end - end - - scores = [philscore, astroscore, archscore, relscore] - case - when scores.max == philscore - return :philosophy - when scores.max == astroscore - return :astronomy - when scores.max == archscore - return :archeology - when scores.max == relscore - return :religion - end - - - end - -end diff --git a/lib/complex_predictor3.rb b/lib/complex_predictor3.rb deleted file mode 100644 index 0a9c9ad..0000000 --- a/lib/complex_predictor3.rb +++ /dev/null @@ -1,173 +0,0 @@ -require_relative 'predictor' - -class ComplexPredictor < Predictor - - def train! - - # take each book array.inject(Hash.new(0)) { |h, k| h[k] += 1, h} - - @archwords = [] - - (0..(@all_books[:archeology].length - 1)).each do |num| - @all_books[:archeology][num][1].each do |word| - @archwords << word if good_token?(word) == true - end - end - - archhash = {} - - @archwords.each do |word| - if archhash[word] == nil - archhash[word] = 1 - else - archhash[word] += 1 - end - end - - newarchhash = archhash.sort_by{|key, value| value}.reverse[0..60] - - @archarr = [] - newarchhash.each { |k,v| @archarr << k } -# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - @astrowords = [] - (0..(@all_books[:astronomy].length - 1)).each do |num| - @all_books[:astronomy][num][1].each do |word| - @astrowords << word if good_token?(word) == true - end - end - - astrohash = {} - - @astrowords.each do |word| - if astrohash[word] == nil - astrohash[word] = 1 - else - astrohash[word] += 1 - end - end - - newastrohash = astrohash.sort_by{|key, value| value}.reverse[0..60] - - @astroarr = [] - newastrohash.each { |k,v| @astroarr << k } - -# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - @philwords = [] - (0..(@all_books[:philosophy].length - 1)).each do |num| - @all_books[:philosophy][num][1].each do |word| - @philwords << word if good_token?(word) == true - end - end - - philhash = {} - - @philwords.each do |word| - if philhash[word] == nil - philhash[word] = 1 - else - philhash[word] += 1 - end - end - - newphilhash = philhash.sort_by{|key, value| value}.reverse[0..60] - - @philarr = [] - newphilhash.each { |k,v| @philarr << k } - -#----------------------------- - - @relwords = [] - (0..(@all_books[:religion].length - 1)).each do |num| - @all_books[:religion][num][1].each do |word| - @relwords << word if good_token?(word) == true - end - end - - relhash = {} - - @relwords.each do |word| - if relhash[word] == nil - relhash[word] = 1 - else - relhash[word] += 1 - end - end - - newrelhash = relhash.sort_by{|key, value| value}.reverse[0..60] - - - @relarr = [] - newrelhash.each { |k,v| @relarr << k } - - end - - def predict(tokens) - @goodtokens = [] - tokens.each do |word| - @goodtokens << word if good_token?(word) == true - end - testhash = {} - @goodtokens.each do |word| - if testhash[word] == nil - testhash[word] = 1 - else - testhash[word] += 1 - end - end - - - newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] - @bookarr = [] - newtesthash.each { |k,v| @bookarr << k } - - philscore = 0 - - (0..@philarr.length - 1).each do |num| - @bookarr.each do |word| - philscore += 1 if word == @philarr[num] - end - end - - - astroscore = 0 - - (0..@astroarr.length - 1).each do |num| - @bookarr.each do |word| - astroscore += 1 if word == @astroarr[num] - end - end - - - archscore = 0 - (0..@archarr.length - 1).each do |num| - @bookarr.each do |word| - archscore += 1 if word == @archarr[num] - end - end - - relscore = 0 - - (0..@relarr.length - 1).each do |num| - @bookarr.each do |word| - relscore += 1 if word == @relarr[num] - end - end - - scores = [philscore, astroscore, archscore, relscore] - case - when scores.max == philscore - return :philosophy - when scores.max == astroscore - return :astronomy - when scores.max == archscore - return :archeology - when scores.max == relscore - return :religion - end - - - end - -end From d52b0b842c4d787ad2e97a108c918776a5cad4d5 Mon Sep 17 00:00:00 2001 From: Derek Ellis Date: Sun, 3 Aug 2014 21:00:06 -0500 Subject: [PATCH 3/3] Need to factor and speed up code --- lib/complex_predictor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/complex_predictor.rb b/lib/complex_predictor.rb index 1e15c48..8819eb6 100644 --- a/lib/complex_predictor.rb +++ b/lib/complex_predictor.rb @@ -47,7 +47,7 @@ def predict(tokens) @goodtokens.each do |word| testhash[word] += 1 end - # piss + #argh newtesthash = testhash.sort_by{|key, value| value}.reverse[0..60] @bookarr = []