Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travis Hooper Gutenberg #69

Open
wants to merge 2 commits into
base: gutenberg
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
ruby '2.0.0'

gem 'rspec', '~> 2.14.1'
gem 'pry-byebug'
3 changes: 1 addition & 2 deletions gutenberg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ def run!(predictor_klass, opts={})
end

run!(SimplePredictor)
run!(ComplexPredictor, debug: true)

run!(ComplexPredictor, debug: true)
73 changes: 69 additions & 4 deletions lib/complex_predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,38 @@ class ComplexPredictor < Predictor
#
# Returns nothing.
def train!
@data = {}

@length_data = {}

@all_books.each do |category, books|
@length_data[category] = {
words: 0,
books: 0
}
books.each do |filename, tokens|
@length_data[category][:words] += tokens.count
@length_data[category][:books] += 1
end
end

@data = {} # {:astronomy=>"footnote", :philosophy=>"things", :religion=>"christ", :archeology=>"gutenberg"}
@length = {}
bad_words = ['footnote', 'things', 'gutenberg', 'project', 'illustration']


@all_books.each do |category, books|
@data[category] = Hash.new(0)
books.each do |filename, tokens|
tokens.each {|token| @data[category][token] += 1 if token.length > 5 && bad_words.include?(token) != true }
end
@data[category] = @data[category].max_by {|k,v| v}[0]
end
# raise "You must implement Predictor#train!."

# tokens - A list of tokens (words).

# Returns a category.
@data
end

# Public: Predicts category.
Expand All @@ -15,8 +46,42 @@ def train!
#
# Returns a category.
def predict(tokens)
# Always predict astronomy, for now.
:astronomy
# # Find the category that has the most similar word-count.
# #
# # Example: Say the average Archeology book has 50 words and the average
# # Philosophy book has 100 words. Then say we must predict some book with 120
# # words. In this case, we will predict Philosophy, since 120 is closer to 100
# # than it is to 50.
max_occurrences = 0
the_category = nil
counter = Hash.new

minimum_category = nil
minimum_distance = 999999999999

@length_data.each do |category, counts|
average_words_per_book = counts[:words].to_f / counts[:books]
difference = (tokens.count - average_words_per_book).abs

if difference < minimum_distance
minimum_category = category
minimum_distance = difference
end
end



@data.each do |category, word|
counter[category] = 0
tokens.each {|token| counter[category] += 1 if word == token || category.to_s == token}
if minimum_category == category
counter[category] += 2
end
if max_occurrences.to_f < counter[category]
max_occurrences = counter[category]
the_category = category
end
end
the_category
end
end

60 changes: 60 additions & 0 deletions lib/complex_predictor2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 = {}

@all_books.each do |category, books|
@data[category] = Hash.new(0)
books.each do |filename, tokens|
tokens.each {|token| @data[category][token] += 1 if token.length > 5}
end
@data[category] = @data[category].max_by {|k,v| v}[0]
end
# raise "You must implement Predictor#train!."

# Public: Predicts category.

# tokens - A list of tokens (words).

# Returns a category.
end

# Public: Predicts category.
#
# tokens - A list of tokens (words).
#
# Returns a category.
def predict(tokens)
# # Find the category that has the most similar word-count.
# #
# # Example: Say the average Archeology book has 50 words and the average
# # Philosophy book has 100 words. Then say we must predict some book with 120
# # words. In this case, we will predict Philosophy, since 120 is closer to 100
# # than it is to 50.
max_occurrences = 0
the_category = nil
counter = Hash.new

@all_books.each do |cat, books|
@data.each do |category, word|
books.each do |filename, tokens|
counter[category] = 0
tokens.each {|token| counter[category] += 1 if token == word}
if max_occurrences < counter[category]
max_occurrences = counter[category]
the_category = category
end
end
end
end

the_category
# # raise "You must implement Predictor#predict."
end
end

22 changes: 22 additions & 0 deletions lib/complex_predictor3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 = {}
end

# Public: Predicts category.
#
# tokens - A list of tokens (words).
#
# Returns a category.
def predict(tokens)
# Always predict astronomy, for now.
:astronomy
end
end

1 change: 1 addition & 0 deletions lib/predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ def load_books(dataset, opts={})
end
end


2 changes: 2 additions & 0 deletions lib/simple_predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ def predict(tokens)
end
end



4 changes: 4 additions & 0 deletions spec/gutenberg_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#require this file in your spec files to help DRY up your tests
require 'rspec'
require 'pry-byebug'
require_relative '../gutenberg.rb'