-
-
Notifications
You must be signed in to change notification settings - Fork 50
My final solution to the ShakespeareAnalyzer problem #15
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
charlieanna
wants to merge
9
commits into
thoughtbot:master
Choose a base branch
from
charlieanna: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
9 commits
Select commit
Hold shift + click to select a range
a833ded
My first try at this problem with one major spec passing
charlieanna 5fbc8b6
After refactoring by using extract methods and extract class
charlieanna 3146ada
Added results file and a method to print the result
charlieanna 5aae115
Added analyzer.rb to show the result
charlieanna d867241
Now ShakespeareAnalyzer can take in any type of content(string, url, …
charlieanna 52dfbc2
got the analyzer.rb to work
charlieanna 9ecd2dd
formatted the result as wanted in the solution
charlieanna 42c7694
deleted analyzer.rb
charlieanna 4eb202c
changed the result.txt
charlieanna 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,3 @@ | ||
| --color | ||
| --drb | ||
| --format documentation |
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,8 @@ | ||
| source 'https://rubygems.org' | ||
| ruby '2.0.0' | ||
|
|
||
| gem "rspec", "~> 2.14.1" | ||
| gem "nokogiri", "~> 1.6.1" | ||
| gem "debugger", "~> 1.6.3" | ||
| gem 'webmock' | ||
| gem 'shoulda-matchers' |
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,54 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| activesupport (4.0.1) | ||
| i18n (~> 0.6, >= 0.6.4) | ||
| minitest (~> 4.2) | ||
| multi_json (~> 1.3) | ||
| thread_safe (~> 0.1) | ||
| tzinfo (~> 0.3.37) | ||
| addressable (2.3.5) | ||
| atomic (1.1.14) | ||
| columnize (0.3.6) | ||
| crack (0.4.1) | ||
| safe_yaml (~> 0.9.0) | ||
| debugger (1.6.3) | ||
| columnize (>= 0.3.1) | ||
| debugger-linecache (~> 1.2.0) | ||
| debugger-ruby_core_source (~> 1.2.4) | ||
| debugger-linecache (1.2.0) | ||
| debugger-ruby_core_source (1.2.4) | ||
| diff-lcs (1.2.5) | ||
| i18n (0.6.9) | ||
| mini_portile (0.5.2) | ||
| minitest (4.7.5) | ||
| multi_json (1.8.2) | ||
| nokogiri (1.6.1) | ||
| mini_portile (~> 0.5.0) | ||
| rspec (2.14.1) | ||
| rspec-core (~> 2.14.0) | ||
| rspec-expectations (~> 2.14.0) | ||
| rspec-mocks (~> 2.14.0) | ||
| rspec-core (2.14.7) | ||
| rspec-expectations (2.14.4) | ||
| diff-lcs (>= 1.1.3, < 2.0) | ||
| rspec-mocks (2.14.4) | ||
| safe_yaml (0.9.7) | ||
| shoulda-matchers (2.4.0) | ||
| activesupport (>= 3.0.0) | ||
| thread_safe (0.1.3) | ||
| atomic | ||
| tzinfo (0.3.38) | ||
| webmock (1.16.1) | ||
| addressable (>= 2.2.7) | ||
| crack (>= 0.3.2) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| debugger (~> 1.6.3) | ||
| nokogiri (~> 1.6.1) | ||
| rspec (~> 2.14.1) | ||
| shoulda-matchers | ||
| webmock |
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 @@ | ||
| require 'nokogiri' | ||
| class Content | ||
| attr_accessor :data,:source | ||
| def initialize(source) | ||
| @source = source | ||
| @data = get_content_from_source | ||
| end | ||
|
|
||
| def get_content_from_source | ||
| 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,8 @@ | ||
| require 'content' | ||
| require 'nokogiri' | ||
| class FileContent < Content | ||
| def get_content_from_source | ||
| f = File.open(@source) | ||
| Nokogiri::XML(f) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| 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,8 @@ | ||
| require 'open-uri' | ||
| require_relative 'content' | ||
| require 'nokogiri' | ||
| class HttpContent < Content | ||
| def get_content_from_source | ||
| Nokogiri::XML(open(@source)) | ||
| 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,71 @@ | ||
| require_relative 'speech' | ||
| class ShakespeareAnalyzer | ||
| attr_accessor :doc,:result | ||
| def initialize(doc) | ||
| @doc = doc | ||
| @result = {} | ||
| end | ||
|
|
||
| def analyze | ||
| play = get_play | ||
| acts = get_acts_from_play(play) | ||
| acts.each do |act| | ||
| add_act_to_result(act) | ||
| end | ||
| end | ||
|
|
||
| def add_act_to_result(act) | ||
| scenes = act.css("SCENE") | ||
| scenes.each do |scene| | ||
| add_scene_to_result(scene) | ||
| end | ||
| end | ||
|
|
||
| def add_scene_to_result(scene) | ||
| speeches = get_speeches_from_scene(scene) | ||
| speeches.each do |speech| | ||
| add_speech_to_result(speech) | ||
| end | ||
| end | ||
|
|
||
| def get_play | ||
| @doc.css("PLAY") | ||
| end | ||
|
|
||
| def get_acts_from_play(play) | ||
| play.css("ACT") | ||
| end | ||
|
|
||
| def get_speeches_from_scene(scene) | ||
| scene.css("SPEECH") | ||
| end | ||
|
|
||
| def add_speech_to_result(speech) | ||
| speech = Speech.new(speech) | ||
| speaker = speech.speaker | ||
| lines = speech.lines | ||
| add_to_result(speaker,lines) | ||
| end | ||
|
|
||
| def add_to_result(speaker,lines) | ||
| if @result[speaker].nil? | ||
| @result[speaker] = lines | ||
| else | ||
| @result[speaker] = (@result[speaker] + lines) | ||
| end | ||
| end | ||
|
|
||
| def sort_result | ||
| @result.sort_by { |name, lines| lines }.reverse | ||
| end | ||
|
|
||
| def format_result | ||
| sort_result | ||
| end | ||
|
|
||
| def print_result | ||
| format_result.each do |name,lines| | ||
| puts "#{lines} #{name.downcase.capitalize}" | ||
| end | ||
| 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,13 @@ | ||
| class Speech | ||
| def initialize(speech) | ||
| @speech = speech | ||
| end | ||
|
|
||
| def speaker | ||
| @speech.css("SPEAKER").text | ||
| end | ||
|
|
||
| def lines | ||
| @speech.css("LINE").count | ||
| 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,7 @@ | ||
| require 'nokogiri' | ||
| require 'content' | ||
| class StringContent < Content | ||
| def get_content_from_source | ||
| Nokogiri::XML(@source) | ||
| end | ||
| end |
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.
I think you can raise an exception here in case this method is called from an instance of Content class directly.