Created
February 13, 2012 21:19
-
-
Save stephenroller/1820591 to your computer and use it in GitHub Desktop.
dict.leo.org command line interface
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
#!/usr/bin/ruby | |
# example usage: | |
# $ leo büch* | |
# book das Buch - Pl. die Bücher | |
# accounts die Bücher | |
# stock of books der Bücherbestand | |
# bookshelf - pl. bookshelves das Bücherbord - Pl. die Bücherborde | |
# shelf pl.: shelves das Bücherbord | |
# bookshelf - pl. bookshelves das Bücherbrett | |
# shelf pl.: shelves das Bücherbrett | |
# library die Bücherei | |
# bibliophile der Bücherfreund | |
# book lover der Bücherfreund | die Bücherfreundin | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'cgi' | |
RAW_URL = 'http://dict.leo.org/ende?lp=ende&lang=en&searchLoc=0&cmpType=relaxed§Hdr=on&spellToler=3&search=%s' | |
searchword = ARGV.join(" ") | |
url = RAW_URL % CGI.escape(searchword) | |
doc = Nokogiri::HTML(open(url)) | |
doc.encoding = 'utf-8' | |
results = [] | |
doc.css('#results tr').each do |row| | |
if row.children.length == 5 | |
texts = row.children.map{|c| c.content.gsub("\xc2", ""). | |
gsub("\xa0", "").strip}. | |
delete_if{|s| s == ""} | |
if texts.length == 2 and results.length < 10 | |
results.push(texts) | |
end | |
end | |
end | |
if results.length > 0 | |
leftspace = [results.map{|p| p[0].length}.max + 4, 25].max | |
#rightspace = [results.map{|p| p[1].length}.max + 4, 25].max | |
leftformat = "%%-%ds" % leftspace | |
#rightformat = "%%-%ds" % rightspace | |
rightformat = "%s" | |
results.each do |result| | |
puts leftformat % result[0] + " " + rightformat % result[1] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment