-
-
Notifications
You must be signed in to change notification settings - Fork 50
Pull request for Shakespeare Analyzer challenge #5
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
Open
JESii
wants to merge
19
commits into
thoughtbot:master
Choose a base branch
from
JESii:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1edcbe8
Initial comment into forked repo
JESii 352c76c
Add net retrieval of play
JESii bda073e
Add output of program
JESii d57e8b4
Count LINES, not SPEECHES...
JESii 686f5e7
Update to properly capitalize names
JESii a4f3113
Add updated output
JESii e058977
Updates for multiple speakers on a single speech
JESii e86ef6e
Merge file checking into initialize method...
JESii da8650a
Begin code cleanup/refactoring...
JESii 1bcbd1d
Refactoring...
JESii 003fac1
Refactoring...
JESii 19f3325
Refactoring file handing based on 'Confident Ruby'...
JESii cef26b0
Add spec for bad http file...
JESii d5db412
Minor refactoring...
JESii 2e27699
Refactor/Extract Method to get_personal
JESii b446cef
Refactor/extract method to get speaker lines
JESii 12f3c1f
Refactor/rename get_speaker_lines => analyze_speeches
JESii c725669
Refactor/extract method from analyze_speeches to get_speaker_lines
JESii 0f670cb
Minor refactoring/cleanup indentation
JESii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| text.xml | ||
| macbeth.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rvm use ruby-1.9.3-p327 | ||
| rvm gemset use rails3213 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #!/usr/bin/env ruby | ||
| load "lib/shakespeare_analyzer.rb" | ||
|
|
||
| if ARGV[0].nil? then | ||
| puts "usage: shakespeare_analyzer <input-file>" | ||
| exit 1 | ||
| end | ||
|
|
||
| analyzer = ShakespeareAnalyzer.new(ARGV[0]) | ||
| analyzer.analyze | ||
| analyzer.list_by_speaker_count |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| require 'pp' | ||
| require 'nokogiri' | ||
| require 'net/http' | ||
| require 'uri' | ||
|
|
||
| class ShakespeareAnalyzer | ||
| def initialize(file) | ||
| @file = file | ||
| @persona = {} | ||
| end | ||
|
|
||
| def analyze | ||
| #TODO: Probably better name of 'parse' | ||
| @file = checked_file_for_open | ||
| doc = Nokogiri::XML(open(@file)) { |config| config.noerror } | ||
| get_persona(doc) | ||
| analyze_speeches(doc) | ||
| end | ||
|
|
||
| def get_persona(doc) | ||
| doc.css('PERSONA').each do |p| | ||
| pname = p.children.text.tr('"','') | ||
| @persona[pname] = 0 | ||
| end | ||
| end | ||
|
|
||
| def analyze_speeches(doc) | ||
| doc.css('SPEECH').each do |speech| | ||
| speech.css('SPEAKER').each do |speaker| | ||
| get_speaker_lines(speaker, speech) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def get_speaker_lines(speaker, speech) | ||
| ## Sometimes the speaker is 'ALL', but who that is depends on who's on stage... | ||
| ## Ignoring this for now | ||
| ## Turns out there are speakers without @persona! | ||
| speaker_name = speaker.children.text.tr('"','') | ||
| @persona[speaker_name] = 0 if @persona[speaker_name].nil? | ||
| speech.css('LINE').each do |line| | ||
| @persona[speaker_name] += 1 | ||
| end | ||
| end | ||
|
|
||
| def list_by_speaker_count | ||
| sorted_output = (@persona.sort_by {|k,v| v}).reverse | ||
| sorted_output.each do |a| | ||
| name = (a[0].split(' ').map {|n| n.capitalize }).join(" ") | ||
| puts "#{a[1]} #{name}" | ||
| end | ||
| end | ||
|
|
||
| def checked_file_for_open | ||
| return @file if File.file?(@file) | ||
| return get_http_file | ||
| end | ||
|
|
||
| def get_http_file | ||
| uri = URI(@file) | ||
| raise "Unreadable file" if uri.scheme != "http" | ||
| @file = 'play.xml' | ||
| Net::HTTP.start(uri.host) do |http| | ||
| resp = http.get(uri.path) | ||
| raise "Unreadable file" unless (resp.code.to_i >= 200 && resp.code.to_i <= 299) | ||
| open(@file, 'wb') do |file| | ||
| file.write(resp.body) | ||
| end | ||
| end | ||
| @file | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 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 Third Witch | ||
| 27 Second Witch | ||
| 23 Gentlewoman | ||
| 23 Messenger | ||
| 21 Lord | ||
| 21 Angus | ||
| 20 Son | ||
| 15 Second Murderer | ||
| 12 Menteith | ||
| 11 Caithness | ||
| 11 Old Man | ||
| 10 Donalbain | ||
| 8 Third Murderer | ||
| 7 Young Siward | ||
| 5 Third Apparition | ||
| 5 Seyton | ||
| 5 Servant | ||
| 4 Second Apparition | ||
| 3 Lords | ||
| 2 Fleance | ||
| 2 Both Murderers | ||
| 2 First Apparition | ||
| 1 Attendant | ||
| 1 Soldiers | ||
| 0 Lords, Gentlemen, Officers, Soldiers, Murderers, Attendants, And Messengers. | ||
| 0 Young Siward, His Son. | ||
| 0 Siward, Earl Of Northumberland, General Of The English Forces. | ||
| 0 Fleance, Son To Banquo. | ||
| 0 Boy, Son To Macduff. | ||
| 0 An English Doctor. | ||
| 0 A Scotch Doctor. | ||
| 0 A Soldier. | ||
| 0 A Porter. | ||
| 0 An Old Man. | ||
| 0 Gentlewoman Attending On Lady Macbeth. | ||
| 0 Three Witches. | ||
| 0 Apparitions. | ||
| 0 Seyton, An Officer Attending On Macbeth. | ||
| 0 Duncan, King Of Scotland. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty large method. I'd try to extract several methods out of this to make things more readable.