1 / 27

Coding For Humanists Workshop

Coding For Humanists Workshop. Topics - Today. Morning Introduction and Justification Installing Ruby Small Code Practice Examples Text Processing Afternoon Web scraping Wikipedia Text Parsing. Topics - Tomorrow. Morning Intermediate Ruby Methods Conditionals Loops Afternoon

matia
Download Presentation

Coding For Humanists Workshop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coding For Humanists Workshop

  2. Topics - Today • Morning • Introduction and Justification • Installing Ruby • Small Code Practice Examples • Text Processing • Afternoon • Web scraping Wikipedia • Text Parsing

  3. Topics - Tomorrow • Morning • Intermediate Ruby • Methods • Conditionals • Loops • Afternoon • Working with APIs • Twitter • Google Maps API • Google Charts API

  4. Introductions • Who you are • What you hope to learn • What tools you currently use

  5. Digital Humanities • Data Mining • Data Visualization • Text Analysis • Geographic Information Systems • Multimedia

  6. Why Ruby • Open-Source • Community • English-like syntax • Interpreted • Ruby on Rails – for web apps

  7. Shorter Code • C++ • Ruby

  8. Ruby Installation • Windows • rubyinstaller • Mac • Comes pre-installed • railsinstaller

  9. Text Editor • Any text editor will do. • Preferences: • Windows • SciTE • Notpad++ • Mac • Text Wrangler • Sublime Edit

  10. Running Ruby Code • Write code using text editor • Save file as ____________.rb • Open Terminal (Command Prompt) • Type “ruby ____________.rb

  11. IRB • Interactive Ruby • Can run commands directly from terminal • Start by typing “irb” • “exit” to return to terminal

  12. First, some terminal commands • ls / dir • cd • .. • tab (auto complete) • mkdir • rmdir • rm

  13. You knew it was coming… • Hello World. • First in irb • Then as a .rb file

  14. Resources • The Bastard’s Book of Ruby • CodeAcademy • Ruby-lang.org

  15. Writing to a file • open("hello-world.txt", 'w') • Will create a new file relative to the program that runs it.

  16. Reading a web page require "open-uri” puts open("http://en.wikipedia.org/wiki/Ada_Lovelace").read What if you wanted to grab the text of the New York Times home page?

  17. Data Types • Strings • In quotes • Just characters. • Numbers • Integers • Whole numbers with no decimals • 5 • 458 • -7 • Floats • Numbers with decimals • 1.4 • 0.5 • 3.0

  18. Variables • Words or letters that stand in for something else. • Numbers • Strings • Objects • Examples • Arithmetic • String concatenation • Mixed

  19. String Example x = "http://en.wikipedia.org/wiki" y = "Ada_Lovelace" z = x + "/" + y puts x puts y puts z

  20. Another way… require "open-uri" remote_base_url = "http://en.wikipedia.org/wiki" remote_page_name = "Ada_Lovelace" remote_full_url = remote_base_url + "/" + remote_page_name puts open(remote_full_url).read

  21. Another way… require "open-uri" remote_base_url = "http://en.wikipedia.org/wiki" remote_page_name = ”Ruby" remote_full_url = remote_base_url + "/" + remote_page_name puts open(remote_full_url).read

  22. Saving remote files require "open-uri" remote_base_url = "http://en.wikipedia.org/wiki" remote_page_name = "Ada_Lovelace" remote_full_url = remote_base_url + "/" + remote_page_name remote_data = open(remote_full_url).read my_local_file = open("my-downloaded-page.html", "w") my_local_file.write(remote_data) my_local_file.close

  23. Collections and Loops (1..10).each do |a_number| puts a_number end

  24. Strings and Numbers • What a difference quotes make. • “42” is different than 42 • Try arithmatic

  25. Looping remote_base_url = “http://en.wikipedia.org/wiki" (1..3).each do |some_number| r_url = remote_base_url + "/" + some_number.to_s puts r_url end

  26. Exercise • Visit one of the URLs generated from our numerical loop, such as http://en.wikipedia.org/wiki/1. It's the Wikipedia entry for Year 1.

  27. Exercise • Specify two numbers, representing a start and end year • Use that range to create a loop • Retrieve the Wikipedia entry that corresponds to each iteration of the loop • Save that Wikipedia page to a corresponding file on your hard drive • In a second loop, combine all those year entries into one file, with the name of "start_year-end_year.html"

More Related