Skip to content
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
2 changes: 1 addition & 1 deletion data/stopwords.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your,one,out,more,now,first,two,very,such,same,shall,upon,before,therefore,great,made,even,same,work,make,being,through,here,way,true,see,time,those,place,much,without,body,whole,another,thus,set,new,given,both,above,well,part,between,end,order,each,form,gutenberg
light,because,been,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,in,into,just,least,like,likely,might,most,must,my,neither,often,on,only,or,other,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your,one,out,more,now,first,two,very,such,same,shall,upon,before,therefore,great,made,even,same,work,being,through,here,true,see,time,those,place,much,without,body,whole,another,thus,set,given,both,above,well,part,between,end,order,each,form,gutenberg,project,should,cannot,things,without,character,little,before,called,further,together,through,therefore,certain,between,saying,themselves,things,without,answer,person,another,though,brought,neither,whether,nothing,people,number,within,houses,species,inasmuch,number,perhaps,others,indeed,having,second,course,during,different,nearly,twenty,already,possible,always,actually,others,become,really,whatsoever,question,toward,coming,purpose,present,personal,friends,friend,behind,places,picture,precious,example,action,destroy,filled,stated,surely,office,wherefore,namely,spoken,speaks,mearly,remember,individual,simply,wilderness,contrary,greater,change,necessary,broken,suppose,relation,men,man,many,feet,small,distance,observations,footnote,found,illustration,large,stone,years,world,nature,miles,itself,earth,sense,hello,planet,about,house,ancient,works,under,three,after,thing,theory,times,against,death,spirit,christ,words,faith,father,ditto,temple,
1 change: 1 addition & 0 deletions gutenberg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ def run!(predictor_klass, opts={})
end

run!(SimplePredictor)

run!(ComplexPredictor, debug: true)

27 changes: 25 additions & 2 deletions lib/complex_predictor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'predictor'
require 'pry-byebug'

class ComplexPredictor < Predictor
# Public: Trains the predictor on books in our dataset. This method is called
Expand All @@ -7,6 +8,18 @@ class ComplexPredictor < Predictor
# Returns nothing.
def train!
@data = {}
@all_books.each do |category, books|
@data[category] = Hash.new(0)
tokenHash = Hash.new(0)
books.each do |filename, tokens|
tokens[2000..-tokens.count/2].each do |word|
if word.length > 4 && good_token?(word)
tokenHash[word]+= 1
end
end
end
@data[category][:keys] = tokenHash.sort_by{|x,y| y}[-3..-1].map!{|x| x[0]}
end
end

# Public: Predicts category.
Expand All @@ -15,8 +28,18 @@ def train!
#
# Returns a category.
def predict(tokens)
# Always predict astronomy, for now.
:astronomy
newarray = tokens[0..18000]
@decider = Hash.new(0)
tokens[2500..10000].each do |word|
if word.length > 4 && good_token?(word)
@data.each do |key,val|
if val.values[0].include?(word)
@decider[key]+=1
end
end
end
end
return @decider.max_by{|x,y| y}[0]
end
end

2 changes: 1 addition & 1 deletion lib/predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def predict(tokens)
# Returns true if you should use this token. In our project, "token" is
# synonymous with "word".
def good_token?(token)
!STOP_WORDS.include?(token) && token.size > 2
token.size > 4 && !STOP_WORDS.include?(token)
end

#############################################################################
Expand Down
1 change: 0 additions & 1 deletion lib/simple_predictor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def train!
# }
# }
@data = {}

@all_books.each do |category, books|
@data[category] = {
words: 0,
Expand Down