Last active
January 30, 2019 17:36
-
-
Save brenopoggiali/6ee0e28394cb7df70692733869241d17 to your computer and use it in GitHub Desktop.
Program that prints the number of lines spoken by each character in "The Tragedy of Macbeth" by Shakespeare to train my Ruby skills
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
| source 'https://rubygems.org' | |
| ruby '2.5.1' | |
| gem "nokogiri", "~> 1.10.1" |
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
| require 'nokogiri' | |
| require 'open-uri' | |
| print "Copy your play.xml URL here: " | |
| play = gets.chomp | |
| doc = Nokogiri::XML(open(play)) | |
| speeches = doc.xpath('//SPEECH') | |
| people = Hash.new | |
| speeches.each do |speech| | |
| person = speech.at('./SPEAKER').content | |
| people[person] ||= 0 | |
| speech.xpath('LINE').each do |line| | |
| people[person] += 1 | |
| end | |
| end | |
| people.reject! { |key, value| key == "ALL" } | |
| people = people.sort { |key, value| value[1]<=>key[1] } | |
| people.each do |person, lines| | |
| puts "#{lines} #{person}" | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Engraçado que não consegui usar o
sort!. Imagino que talvez ele não funcione para Hash. Ai acabei fazendopeople =mesmo