Giter VIP home page Giter VIP logo

wikipedia-ruby-xml-lab's Introduction

Wikipedia Ruby Xml Lab

Objectives

  1. Parse XML with Ruby
  2. Write to a Text File
  3. Correlate Data from Different Sources

Instructions

Write a ruby program wikipedia_processor.rb to process enwiki-latest-stub-articles1.xml file for the following tasks:

1. Collect all Page Ids and Titles

Collect all page ids and title names for pages whose title starts with either "Afghanistan" or "America."

Write this information into a file named pages.txt in the following format: <page id>, <page title>

2. Find Contributor username and id

For each of those in task 1 above, find contributor username and the corresponding id.

Write this information into a file named users.txt in the following format: <contributor id>, <contributor name>

3. Get the page id and contributor id

From tasks 1 and 2 above, get the page id and contributor id, and write into the file named page_users.txt in the following format: ,

4. Page count

Build a method, page_count, the returns the number of pages which start with either "Afghanistan" or "America" (case sensitive).

Note: There are some pages that do not have usernames for contributors; ignore task 2 and 3 for such cases e.g.: <title>Afghanistan (1911 Encyclopedia)</title> has contributor 96.251.199.116; i.e., it does not have username and userid.

Using Nokogiri

nokogiri is a Ruby gem for parsing XML and it has been included in the Gemfile of this project.

Sample usage:

xml = <<-XML
<root>
  <sitcoms>
    <sitcom>
      <name>Married with Children</name>
      <characters>
        <character>Al Bundy</character>
        <character>Bud Bundy</character>
        <character>Marcy Darcy</character>
      </characters>
    </sitcom>
    <sitcom>
      <name>Perfect Strangers</name>
      <characters>
        <character>Larry Appleton</character>
        <character>Balki Bartokomous</character>
      </characters>
    </sitcom>
  </sitcoms>
  <dramas>
    <drama>
      <name>The A-Team</name>
      <characters>
        <character>John "Hannibal" Smith</character>
        <character>Templeton "Face" Peck</character>
        <character>"B.A." Baracus</character>
        <character>"Howling Mad" Murdock</character>
      </characters>
    </drama>
  </dramas>
</root>
XML

@doc = Nokogiri::XML(xml)
@doc.xpath("//character")
# => ["<character>Al Bundy</character>",
#    "<character>Bud Bundy</character>",
#    "<character>Marcy Darcy</character>",
#    "<character>Larry Appleton</character>",
#    "<character>Balki Bartokomous</character>",
#    "<character>John \"Hannibal\" Smith</character>",
#    "<character>Templeton \"Face\" Peck</character>",
#    "<character>\"B.A.\" Baracus</character>",
#    "<character>\"Howling Mad\" Murdock</character>"]

Resources

  1. Searching a Document
  2. Writing a File in Ruby

wikipedia-ruby-xml-lab's People

Contributors

aviflombaum avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

antoniotto71

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.