Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
scripts for outputting some reports from the Web Data Commons NQuads
#!/usr/bin/env bash
# These steps will take a long time to download the data set.
# First, get the list of available NQuad files to download.
# We're only interested in the microdata set right now since that seems to be where is used more. So create a file list
cat files.list | grep html-microdata > microdata_files.list
# OK, this will take a while depending on your connection. Let it run overnight.
wget -i microdata_files.list
# Gunzip all the compressed files.
gunzip *gz
# Use grep to filter through all the NQuads and select only those that have on the line. This can be changed to get any type.
cat html-microdata* | grep >> microdata_books_nquads.nq
# Then run the script to create the report.
nquad_context_count_per_host.rb microdata_books_nquads.nq
# Open the CSV file with the appropriate program. This works on Ubuntu.
xdg-open microdata_books_nquads.csv
#! /usr/bin/env ruby
# counts statements and lists properties for hosts in nquads file
require 'rdf'
require 'rdf/nquads'
require 'csv'
contexts = {}
filename = ARGV[0]
extension = File.extname(filename)
basename = File.basename(filename, extension) do |reader|
reader.each_statement do |statement|
if !statement.context.nil?
host =
predicate = statement.predicate.to_s
contexts[host] ||= {}
contexts[host]['count'] ||= 0
contexts[host]['count'] += 1
contexts[host]['props'] ||= []
if predicate.include?('')
predicate.sub!('', '')
contexts[host]['props'] << predicate
sorted_contexts = contexts.sort_by{|k,v| v['count']}.reverse"#{basename}.csv", 'wb') do |csv|
csv << %w(host count properties)
sorted_contexts.each do |context, data|
csv << [context, data['count'], data['props'].sort.join(' ')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.