Skip to content

Instantly share code, notes, and snippets.

@rttong
Last active December 17, 2015 18:09
Show Gist options
  • Save rttong/529df5894dc34668242a to your computer and use it in GitHub Desktop.
Save rttong/529df5894dc34668242a to your computer and use it in GitHub Desktop.
class Load
attr_reader :file, :words, :word_count
def initialize(filename)
@words = ""
@word_count = Hash.new(0)
@file = filename
parse_file
end
def parse_file
File.open(@file, "r").each do |line|
@words << line
end
@words = @words.downcase.gsub(/\W/, " ").split(" ")
# p @words.uniq
list
end
def list
@words.each do |word|
@word_count[word] += 1
end
p Hash[@word_count.sort_by{|k,v| v}.reverse]
end
end
text = Load.new("text.txt")
# OUTPUT
# {"the"=>41, "i"=>23, "of"=>21, "and"=>18, "a"=>16, "it"=>16, "to"=>14, "is"=>13, "that"=>11, "you"=>11, "when"=>10, "s"=>8, "words"=>8, "or"=>8, "for"=>8, "stairs"=>7, "repeating"=>7, "in"=>6, "was"=>6, "phrases"=>6, "as"=>5, "them"=>5, "not"=>5, "word"=>5, "course"=>4, "drop"=>4, "your"=>4, "example"=>4, "getting"=>4, "they"=>4, "pregnant"=>4, "sounds"=>3, "way"=>3, "good"=>3, "relax"=>3, "what"=>3, "because"=>3, "doctor"=>3, "effect"=>3, "fair"=>3, "paragraph"=>3, "there"=>3, "an"=>3, "were"=>3, "like"=>3, "better"=>3, "took"=>3, "be"=>3, "use"=>3, "can"=>3, "this"=>3, "right"=>3, "also"=>3, "t"=>3, "about"=>3, "me"=>3, "around"=>3, "but"=>2, "repeated"=>2, "reached"=>2, "top"=>2, "looked"=>2, "deliberately"=>2, "began"=>2, "ll"=>2, "works"=>2, "note"=>2, "talking"=>2, "m"=>2, "just"=>2, "phrase"=>2, "breath"=>2, "deep"=>2, "work"=>2, "need"=>2, "change"=>2, "so"=>2, "clich"=>2, "steep"=>2, "trousers"=>2, "high"=>2, "hat"=>2, "resource"=>2, "clunky"=>2, "free"=>2, "knew"=>2, "we"=>2, "everyone"=>2, "if"=>2, "steps"=>2, "climb"=>2, "know"=>2, "with"=>2, "saw"=>2, "hope"=>2, "at"=>2, "have"=>2, "done"=>2, "okay"=>2, "bottom"=>2, "another"=>2, "my"=>2, "got"=>2, "between"=>2, "her"=>2, "she"=>2, "all"=>2, "which"=>2, "conceiving"=>2, "said"=>2, "almost"=>1, "one"=>1, "number"=>1, "remember"=>1, "asked"=>1, "eagerly"=>1, "isn"=>1, "easy"=>1, "say"=>1, "fretting"=>1, "insisting"=>1, "gone"=>1, "has"=>1, "grainne"=>1, "situation"=>1, "looking"=>1, "novel"=>1, "wait"=>1, "page"=>1, "opening"=>1, "from"=>1, "take"=>1, "six"=>1, "months"=>1, "felt"=>1, "wailing"=>1, "fine"=>1, "time"=>1, "mature"=>1, "e"=>1, "once"=>1, "synonym"=>1, "reaction"=>1, "replaced"=>1, "twice"=>1, "out"=>1, "quick"=>1, "honestly"=>1, "prose"=>1, "wonderful"=>1, "still"=>1, "reads"=>1, "much"=>1, "how"=>1, "didn"=>1, "seem"=>1, "well"=>1, "something"=>1, "written"=>1, "more"=>1, "d"=>1, "would"=>1, "above"=>1, "qualify"=>1, "honest"=>1, "certainly"=>1, "lot"=>1, "our"=>1, "across"=>1, "come"=>1, "somehow"=>1, "grates"=>1, "do"=>1, "friends"=>1, "sound"=>1, "mostly"=>1, "doesn"=>1, "female"=>1, "short"=>1, "times"=>1, "four"=>1, "used"=>1, "ones"=>1, "admitting"=>1, "challenged"=>1, "yes"=>1, "had"=>1, "happened"=>1, "quite"=>1, "quickly"=>1, "thanks"=>1, "character"=>1, "echoing"=>1, "likewise"=>1, "aside"=>1, "humour"=>1, "by"=>1, "playing"=>1, "changing"=>1, "finally"=>1, "repeat"=>1, "deliberate"=>1, "see"=>1, "difference"=>1, "following"=>1, "read"=>1, "mean"=>1, "two"=>1, "examples"=>1, "wrong"=>1, "trick"=>1, "matter"=>1, "give"=>1, "ear"=>1, "such"=>1, "first"=>1, "sentence"=>1, "key"=>1, "specific"=>1, "boring"=>1, "second"=>1, "melodic"=>1, "dramatic"=>1, "terrific"=>1, "finding"=>1, "now"=>1, "style"=>1, "writing"=>1, "autocrit"=>1, "catches"=>1, "element"=>1, "important"=>1, "limited"=>1, "distinction"=>1, "www"=>1, "wordcounter"=>1, "definitely"=>1, "most"=>1, "com"=>1, "summary"=>1, "make"=>1, "sure"=>1, "want"=>1, "add"=>1, "melody"=>1, "re"=>1, "only"=>1, "snuck"=>1, "then"=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment