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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tmp
*.gem
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
10 changes: 10 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard :rspec, cli: "--tag ~@external" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^spec/support/.+\.rb$}) { "spec" }
end

30 changes: 30 additions & 0 deletions lib/macbeth_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'open-uri'
require "nokogiri"

class MacbethAnalyzer
attr_reader :url

def initialize
@url = "http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about pulling this into a named constant?


end

def contents
@contents ||= open(url).read
end

def analyze
Nokogiri::XML(contents).xpath("//SPEECH[SPEAKER!='ALL']/LINE").inject(Hash.new(0)) do |recorder, line_node|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could extract a few small methods here and make this method substantially easier to read.

line_node.xpath("preceding-sibling::SPEAKER").each do |speaker|
recorder[speaker.content] += 1
end
recorder
end
end

def output
analyze.sort_by {|speaker, line_count| -line_count }.map do |speaker, line_count|
"#{line_count.to_s.rjust(3, " ")} #{speaker}"
end
end
end
3 changes: 3 additions & 0 deletions macbeth_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative 'lib/macbeth_analyzer'

MacbethAnalyzer.new.output.each {|o| puts o }
40 changes: 40 additions & 0 deletions output
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
719 MACBETH
265 LADY MACBETH
212 MALCOLM
180 MACDUFF
135 ROSS
113 BANQUO
74 LENNOX
70 DUNCAN
62 First Witch
46 Porter
45 Doctor
41 LADY MACDUFF
39 HECATE
35 Sergeant
30 SIWARD
30 First Murderer
27 Second Witch
27 Third Witch
23 Messenger
23 Gentlewoman
21 Lord
21 ANGUS
20 Son
15 Second Murderer
12 MENTEITH
11 Old Man
11 CAITHNESS
10 DONALBAIN
8 Third Murderer
7 YOUNG SIWARD
5 Servant
5 SEYTON
5 Third Apparition
4 Second Apparition
3 Lords
2 First Apparition
2 Both Murderers
2 FLEANCE
1 Soldiers
1 ATTENDANT
Loading