[Version:1] Easy Dictionary


Hi .

Easily browse for meaning of words from terminal, than searching for word in dictionary or searching in internet (dont waiting for word till whole page loads). This script scraps word form http://dictionary.reference.com .

Dependencies:

ruby, rubygems, mechanize(gem).

we can have multiple search or one way search

Eg.   dict.rb embattled

or run dict.rb and search for unlimitted words.

Note: It works with internet only.

#!/usr/bin/env ruby
=begin
Program name: Easy Dictionary
Date Written: 04/09/2011
Date Modified: 04/09/2011
Author : Sathianarayanan.S
License: GPL2.0
Version: 1.1
=end
begin
require 'rubygems'
require 'mechanize'

agent = Mechanize.new
agent.user_agent_alias = 'Mac Safari'
page = agent.get("http://dictionary.reference.com/")
puts 'Powered by Dictionary.com (dictionary.reference.com)'
puts "\n"
puts 'searching....'
puts "=========================================================================\n"
if ARGV[0]
  word = ARGV[0]
end
while true
        unless ARGV[0]
          print "Enter the word: "
          word = gets.chomp
	end
	form = page.forms.first
	form.q = word
	system("espeak #{word}")
	page = agent.submit form
	meanings = agent.page.search('.lunatext .body .luna-Ent')
	if meanings.empty?
	  puts 'No results'
	else
	  meanings.each do |meaning|
	    puts meaning.text
	  end
	end
        puts '=================================================================='
        if ARGV[0]
          break
        end
end
rescue SocketError
  puts 'Internet connection failed!!'
rescue Interrupt
  puts 'Quiting!!!'
end

Usage:

sathia@sathia27 ~/Desktop $ ruby dict.rb embattled
Powered by Dictionary.com (dictionary.reference.com)

searching....
=========================================================================
1.disposed or prepared for battle.
2.engaged in or beset by conflict or struggle.
1.to arrange in order of battle; prepare for battle; arm.
2.to fortify (a town, camp, etc.).
to furnish with battlements.
==================================================================
sathia@sathia27 ~/Desktop $

OR

sathia@sathia27 ~/Desktop $ ruby dict.rb
Powered by Dictionary.com (dictionary.reference.com)

searching....
=========================================================================
Enter the word: embattled
1.disposed or prepared for battle.
2.engaged in or beset by conflict or struggle.
1.to arrange in order of battle; prepare for battle; arm.
2.to fortify (a town, camp, etc.).
to furnish with battlements.
==================================================================
Enter the word:
Advertisements

About sathia

Web developer at cloudmint
This entry was posted in programs and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s