Created
December 9, 2012 18:01
-
-
Save Santarh/4246299 to your computer and use it in GitHub Desktop.
Dictionary Search Command
This file contains hidden or 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/local/bin/ruby | |
require "rubygems" | |
require "mechanize" | |
require "termcolor" | |
class Mean | |
attr_reader :text | |
def initialize word_class, text | |
@word_class = word_class | |
@text = text | |
end | |
def get_word_class_color_text | |
TermColor.parse("<blue>" + @word_class + "</blue>") | |
end | |
end | |
class Topic | |
attr_reader :means | |
def initialize headline, means | |
@headline = headline | |
@means = means | |
end | |
def get_headline_color_text | |
TermColor.parse("<on_red> " + @headline + " </on_red>") | |
end | |
end | |
class Alc | |
EOW_URL = "http://eow.alc.co.jp" | |
FOOTER = "/UTF-8" | |
def initialize | |
@agent = Mechanize.new | |
end | |
def search word_list | |
query = word_list.join "+" | |
url = EOW_URL + "/" + query + FOOTER | |
page = @agent.get url | |
topics = [] | |
results = page.search "div#resultsList>ul>li" | |
results.each do |r| | |
midashi = r.at("span.midashi").inner_text | |
div = r.at("div") | |
means = [] | |
unorderd_list = div.search("ul") | |
ul_text = unorderd_list.children.map{|x| x.inner_text}.join("\n") | |
means << Mean.new("", ul_text) | |
word_class = div.search("span.wordclass") | |
class_means = div.search("ol") | |
word_class.zip(class_means).each do |a| | |
text = a[1].children.map{|x| " " + x.inner_text}.join("\n") | |
means << Mean.new(a[0].inner_text, text) | |
end | |
topics << Topic.new(midashi, means) | |
end | |
topics | |
end | |
end | |
alc = Alc.new | |
results = alc.search(ARGV) | |
top = results[0] | |
puts top.get_headline_color_text | |
top.means.each do |mean| | |
puts mean.get_word_class_color_text | |
puts mean.text | |
end | |
#alc.search(ARGV).each do |result| | |
# puts result["midashi"] | |
# puts result["text"] | |
#end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment