Created
July 31, 2013 11:37
-
-
Save zellux/6121288 to your computer and use it in GitHub Desktop.
Extract words and explanations from epub version of Merriam Webster's Vocabulary Builder. See http://blog.yxwang.me/notes/languages/vocabulary-builder.html for more details.
This file contains 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' | |
unit = ARGV[0].to_i | |
started = false | |
titles = [] | |
1.upto(3000) do |i| | |
filename = "html/Merriam-Webster_s_Vocabulary_Bu_split_#{'%03d' % i}.html" | |
doc = Nokogiri::HTML(open(filename)) | |
title = doc.css('div span.bold').first | |
next if title == nil | |
word = title.content.strip | |
if word == "Unit #{unit}" | |
started = true | |
titles = doc.css('div p.calibre36 a').map(&:content) | |
titles = titles[0...titles.find_index { |e| e[/^Quiz/] }].map(&:strip) | |
end | |
break if word == "Unit #{unit + 1}" | |
next unless started | |
next if word[/^([ABC]\.)|(Quiz)/] | |
begin | |
if word.start_with?('Unit ') | |
word = doc.css('div p.calibre12 span').first.content.strip | |
explanation = doc.css('div p.calibre12').first.content.strip | |
elsif word == titles[-1] | |
word = doc.css('div p.calibre34 span.bold')[1].content.strip | |
explanation = doc.css('div p.calibre34 br').first.previous.content.strip | |
elsif titles.include?(word) | |
explanation = doc.css('div p').first.content.strip | |
else | |
explanation = doc.css('div p br').first.previous.content.strip | |
end | |
puts "#{word}:::#{explanation}" | |
rescue => e | |
puts word | |
puts e | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://blog.yxwang.me/notes/languages/vocabulary-builder.html for more details.