Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First version of pydict

  • Loading branch information...
commit b07f252df465d4fe1609635bd1f85380f657fb2c 0 parents
@sagarrakshe authored
1  MANIFEST.in
@@ -0,0 +1 @@
+include README.rst
42 README.rst
@@ -0,0 +1,42 @@
+Pydict
+-------
+
+Pydict is a simple command line dictionary, it lets you search the meanings of the word on the terminal.
+It refers to the online dictionary http://www.thefreeonlinedictionary.com
+It also pronouces the keywords.
+
+Requirements
+-------------
+
+It requires a command line mp3 player - mpg123. To install it use the following command:
+$ sudo apt-get install mpg123
+
+Usage
+------
+
+To search for a keyword:
+
+>>> import pydict
+>>> pydict.search('awesome', 2, 'uk')
+
+It will search for the word 'awesome' and pronouce it 2 times and in 'uk' style.
+It will display the result of meaning of 'awesome'.
+If a keyword is not found in the dictionary it reports error.
+
+To just pronouce the word:
+
+>>> import pydict
+>>> pydict.speak('awesome', 2, 'us')
+
+If pronouciation does not exist it reports error.
+It has three styles 'normal', 'us' and 'uk'.
+
+From command line:
+
+$ pydict -q 'awesome' -s 'uk' -p 2
+
+options:
+-q --query: keyword
+-s --style: 'normal', 'us' or 'uk'
+-p --pronounce: repeat pronounciation
+-m --meaning: display search or not. (0 or 1)
21 bin/pydict
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+import pydict
+import optparse
+
+def main():
+ p = optparse.OptionParser()
+ p.add_option('--query', '-q', default = '')
+ p.add_option('--pronounce', '-p', default = 1)
+ p.add_option('--style', '-s', default = 'normal')
+ p.add_option('--meaning', '-m', default = 1)
+ options, arguments = p.parse_args()
+
+ if int(options.meaning) >0:
+ pydict.search(str(options.query), int(options.pronounce), str(options.style).lower())
+ else:
+ pydict.speak(str(options.query), int(options.pronounce), str(options.style).lower())
+
+
+if __name__ == '__main__':
+ main()
1  build/lib.linux-x86_64-2.7/pydict/__init__.py
@@ -0,0 +1 @@
+from .pydict import *
30 build/lib.linux-x86_64-2.7/pydict/color.py
@@ -0,0 +1,30 @@
+class bcolors:
+ HEADER = '\033[95m'
+ OKBLUE = '\033[94m'
+ OKGREEN = '\033[92m'
+ WARNING = '\033[93m'
+ FAIL = '\033[91m'
+ ENDC = '\033[0m'
+
+ def disable(self):
+ self.HEADER = ''
+ self.OKBLUE = ''
+ self.OKGREEN = ''
+ self.WARNING = ''
+ self.FAIL = ''
+ self.ENDC = ''
+
+def print_meaning(meaning):
+ print bcolors.WARNING + meaning + bcolors.ENDC
+
+def print_heading(heading):
+ print bcolors.OKGREEN + heading + bcolors.ENDC
+
+def print_line(line):
+ print bcolors.HEADER + line + bcolors.ENDC
+
+def print_word(word):
+ print bcolors.OKBLUE + word + bcolors.ENDC
+
+def print_error(err):
+ print bcolors.FAIL + err + bcolors.ENDC
16 build/lib.linux-x86_64-2.7/pydict/parser.py
@@ -0,0 +1,16 @@
+from HTMLParser import HTMLParser
+
+class MLStripper(HTMLParser):
+ def __init__(self):
+ self.reset()
+ self.fed = []
+ def handle_data(self, d):
+ self.fed.append(d)
+ def get_data(self):
+ return ''.join(self.fed)
+
+def strip_tags(html):
+ s = MLStripper()
+ s.feed(html)
+ return s.get_data()
+
9 build/lib.linux-x86_64-2.7/pydict/pronounce.py
@@ -0,0 +1,9 @@
+import commands
+
+def play(sound_path, times):
+ play_file = 'mpg123 -q ' + sound_path
+ for i in range(times):
+ commands.getoutput(play_file)
+ if i == 5: break
+
+
110 build/lib.linux-x86_64-2.7/pydict/pydict.py
@@ -0,0 +1,110 @@
+"""
+/* Copyright (c) pydict 2012, Sagar Rakshe <sagarrakshe2@gmail.com>
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+"""
+
+import mechanize
+import urllib2
+import color
+import parser
+import pronounce
+from BeautifulSoup import BeautifulSoup
+from HTMLParser import HTMLParser
+
+htmlParsedPage = ''
+browser = mechanize.Browser()
+
+
+def get_parsed_page(keyword):
+ global htmlParsedPage, browser
+ url = 'http://www.thefreedictionary.com/' + str(keyword)
+ page = browser.open(url)
+ htmlParsedPage = page.read()
+ return htmlParsedPage
+
+
+def search(keyword, repeat, style):
+ global htmlParsedPage
+
+ query = keyword
+
+ if len(query):
+ color.print_line('---'*45)
+ color.print_heading('*\t'*4+"The Free Online Dictionary, Thesaurus and Encyclopedia\t"
+ +'\t'+'*\t'*5)
+ color.print_line('---'*45)
+ color.print_word(query.upper()+'\n')
+
+ try:
+ htmlParsedPage = get_parsed_page(keyword)
+
+ #parse to find the meaning
+ soupPage = BeautifulSoup(htmlParsedPage)
+ div = soupPage.findAll('div', attrs = {'id': 'MainTxt'})
+ meaning = div[0].findAll('div', attrs = {'class':'ds-list'})
+ if meaning == "":
+ meaning = div[0].findAll('div', attrs = {'class' : 'ds-single'})
+
+ for i in range(len(meaning)):
+ color.print_meaning('\t'+parser.strip_tags(str(meaning[i]))+'\n')
+ except:
+ color.print_error('Keyword not found in the Dictionary')
+
+ color.print_line('---'*45)
+ color.print_heading('\t'*6+'sagarrakshe2@gmail.com')
+ color.print_line('---'*45)
+
+ speak(keyword, repeat, style)
+
+ else:
+ color.print_error('Empty Keyword!')
+
+def speak(keyword, repeat, style):
+ global htmlParsedPage, browser
+
+ pronouce_style = ['normal', 'us', 'uk', '']
+ if repeat >0 and len(keyword) and any(style in item for item in pronouce_style):
+ if htmlParsedPage == '':
+ htmlParsedPage = get_parsed_page(keyword)
+
+ soupPage = BeautifulSoup(htmlParsedPage)
+ htmlParsedPage = ''
+ div = soupPage.findAll('div', attrs = {'id': 'MainTxt'})
+
+
+ try:
+ if style.lower() == 'normal' or style.lower() == '':
+ mean_sound = str(parser.strip_tags(str(div[0].findAll('script')[0]))).split('"')[1]
+ sound_url = 'http://img.tfd.com/hm/mp3/' + mean_sound +'.mp3'
+
+ elif style.lower() == 'uk':
+ sample = soupPage.findAll('td', attrs = {'id': 'MainTitle'})
+ mean_sound = str(parser.strip_tags(str(sample[0])).split("'")[3])
+ sound_url = 'http://img2.tfd.com/pron/mp3/' + mean_sound + '.mp3'
+
+ elif style.lower() == 'us':
+ sample = soupPage.findAll('td', attrs = {'id': 'MainTitle'})
+ mean_sound = str(parser.strip_tags(str(sample[0])).split("'")[1])
+ sound_url = 'http://img2.tfd.com/pron/mp3/' + mean_sound + '.mp3'
+
+ sound_path = browser.retrieve(sound_url)
+ pronounce.play(sound_path[0], repeat)
+ except:
+ color.print_error('Pronouciation not found!\nTry with different style.')
+
+
+ elif repeat:
+ color.print_error('Empty Keyword or Unknown Style!')
21 build/scripts-2.7/pydict
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+
+import pydict
+import optparse
+
+def main():
+ p = optparse.OptionParser()
+ p.add_option('--query', '-q', default = '')
+ p.add_option('--pronounce', '-p', default = 1)
+ p.add_option('--style', '-s', default = 'normal')
+ p.add_option('--meaning', '-m', default = 1)
+ options, arguments = p.parse_args()
+
+ if int(options.meaning) >0:
+ pydict.search(str(options.query), int(options.pronounce), str(options.style).lower())
+ else:
+ pydict.speak(str(options.query), int(options.pronounce), str(options.style).lower())
+
+
+if __name__ == '__main__':
+ main()
BIN  dist/pydict-0.1-py2.7.egg
Binary file not shown
BIN  dist/pydict-0.1.tar.gz
Binary file not shown
10 pydict.egg-info/PKG-INFO
@@ -0,0 +1,10 @@
+Metadata-Version: 1.0
+Name: pydict
+Version: 0.1
+Summary: Command line dictionary
+Home-page: http://github.com/sagarrakshe/pydict
+Author: Sagar Rakshe
+Author-email: sagarrakshe2@gmail.com
+License: MIT
+Description: UNKNOWN
+Platform: UNKNOWN
15 pydict.egg-info/SOURCES.txt
@@ -0,0 +1,15 @@
+MANIFEST.in
+README.rst
+setup.py
+bin/pydict
+pydict/__init__.py
+pydict/color.py
+pydict/parser.py
+pydict/pronounce.py
+pydict/pydict.py
+pydict.egg-info/PKG-INFO
+pydict.egg-info/SOURCES.txt
+pydict.egg-info/dependency_links.txt
+pydict.egg-info/not-zip-safe
+pydict.egg-info/requires.txt
+pydict.egg-info/top_level.txt
1  pydict.egg-info/dependency_links.txt
@@ -0,0 +1 @@
+
1  pydict.egg-info/not-zip-safe
@@ -0,0 +1 @@
+
2  pydict.egg-info/requires.txt
@@ -0,0 +1,2 @@
+mechanize
+BeautifulSoup
1  pydict.egg-info/top_level.txt
@@ -0,0 +1 @@
+pydict
1  pydict/__init__.py
@@ -0,0 +1 @@
+from .pydict import *
BIN  pydict/__init__.pyc
Binary file not shown
30 pydict/color.py
@@ -0,0 +1,30 @@
+class bcolors:
+ HEADER = '\033[95m'
+ OKBLUE = '\033[94m'
+ OKGREEN = '\033[92m'
+ WARNING = '\033[93m'
+ FAIL = '\033[91m'
+ ENDC = '\033[0m'
+
+ def disable(self):
+ self.HEADER = ''
+ self.OKBLUE = ''
+ self.OKGREEN = ''
+ self.WARNING = ''
+ self.FAIL = ''
+ self.ENDC = ''
+
+def print_meaning(meaning):
+ print bcolors.WARNING + meaning + bcolors.ENDC
+
+def print_heading(heading):
+ print bcolors.OKGREEN + heading + bcolors.ENDC
+
+def print_line(line):
+ print bcolors.HEADER + line + bcolors.ENDC
+
+def print_word(word):
+ print bcolors.OKBLUE + word + bcolors.ENDC
+
+def print_error(err):
+ print bcolors.FAIL + err + bcolors.ENDC
BIN  pydict/color.pyc
Binary file not shown
16 pydict/parser.py
@@ -0,0 +1,16 @@
+from HTMLParser import HTMLParser
+
+class MLStripper(HTMLParser):
+ def __init__(self):
+ self.reset()
+ self.fed = []
+ def handle_data(self, d):
+ self.fed.append(d)
+ def get_data(self):
+ return ''.join(self.fed)
+
+def strip_tags(html):
+ s = MLStripper()
+ s.feed(html)
+ return s.get_data()
+
BIN  pydict/parser.pyc
Binary file not shown
9 pydict/pronounce.py
@@ -0,0 +1,9 @@
+import commands
+
+def play(sound_path, times):
+ play_file = 'mpg123 -q ' + sound_path
+ for i in range(times):
+ commands.getoutput(play_file)
+ if i == 5: break
+
+
BIN  pydict/pronounce.pyc
Binary file not shown
110 pydict/pydict.py
@@ -0,0 +1,110 @@
+"""
+/* Copyright (c) pydict 2012, Sagar Rakshe <sagarrakshe2@gmail.com>
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+"""
+
+import mechanize
+import urllib2
+import color
+import parser
+import pronounce
+from BeautifulSoup import BeautifulSoup
+from HTMLParser import HTMLParser
+
+htmlParsedPage = ''
+browser = mechanize.Browser()
+
+
+def get_parsed_page(keyword):
+ global htmlParsedPage, browser
+ url = 'http://www.thefreedictionary.com/' + str(keyword)
+ page = browser.open(url)
+ htmlParsedPage = page.read()
+ return htmlParsedPage
+
+
+def search(keyword, repeat, style):
+ global htmlParsedPage
+
+ query = keyword
+
+ if len(query):
+ color.print_line('---'*45)
+ color.print_heading('*\t'*4+"The Free Online Dictionary, Thesaurus and Encyclopedia\t"
+ +'\t'+'*\t'*5)
+ color.print_line('---'*45)
+ color.print_word(query.upper()+'\n')
+
+ try:
+ htmlParsedPage = get_parsed_page(keyword)
+
+ #parse to find the meaning
+ soupPage = BeautifulSoup(htmlParsedPage)
+ div = soupPage.findAll('div', attrs = {'id': 'MainTxt'})
+ meaning = div[0].findAll('div', attrs = {'class':'ds-list'})
+ if meaning == "":
+ meaning = div[0].findAll('div', attrs = {'class' : 'ds-single'})
+
+ for i in range(len(meaning)):
+ color.print_meaning('\t'+parser.strip_tags(str(meaning[i]))+'\n')
+ except:
+ color.print_error('Keyword not found in the Dictionary')
+
+ color.print_line('---'*45)
+ color.print_heading('\t'*6+'sagarrakshe2@gmail.com')
+ color.print_line('---'*45)
+
+ speak(keyword, repeat, style)
+
+ else:
+ color.print_error('Empty Keyword!')
+
+def speak(keyword, repeat, style):
+ global htmlParsedPage, browser
+
+ pronouce_style = ['normal', 'us', 'uk', '']
+ if repeat >0 and len(keyword) and any(style in item for item in pronouce_style):
+ if htmlParsedPage == '':
+ htmlParsedPage = get_parsed_page(keyword)
+
+ soupPage = BeautifulSoup(htmlParsedPage)
+ htmlParsedPage = ''
+ div = soupPage.findAll('div', attrs = {'id': 'MainTxt'})
+
+
+ try:
+ if style.lower() == 'normal' or style.lower() == '':
+ mean_sound = str(parser.strip_tags(str(div[0].findAll('script')[0]))).split('"')[1]
+ sound_url = 'http://img.tfd.com/hm/mp3/' + mean_sound +'.mp3'
+
+ elif style.lower() == 'uk':
+ sample = soupPage.findAll('td', attrs = {'id': 'MainTitle'})
+ mean_sound = str(parser.strip_tags(str(sample[0])).split("'")[3])
+ sound_url = 'http://img2.tfd.com/pron/mp3/' + mean_sound + '.mp3'
+
+ elif style.lower() == 'us':
+ sample = soupPage.findAll('td', attrs = {'id': 'MainTitle'})
+ mean_sound = str(parser.strip_tags(str(sample[0])).split("'")[1])
+ sound_url = 'http://img2.tfd.com/pron/mp3/' + mean_sound + '.mp3'
+
+ sound_path = browser.retrieve(sound_url)
+ pronounce.play(sound_path[0], repeat)
+ except:
+ color.print_error('Pronouciation not found!\nTry with different style.')
+
+
+ elif repeat:
+ color.print_error('Empty Keyword or Unknown Style!')
BIN  pydict/pydict.pyc
Binary file not shown
17 setup.py
@@ -0,0 +1,17 @@
+from setuptools import setup
+
+def readme():
+ with open('README.rst') as f:
+ return f.read()
+
+setup(name='pydict',
+ version='0.1',
+ description='Command line dictionary',
+ url='http://github.com/sagarrakshe/pydict',
+ author='Sagar Rakshe',
+ author_email='sagarrakshe2@gmail.com',
+ license='MIT',
+ packages=['pydict'],
+ scripts=['bin/pydict'],
+ install_requires=['mechanize','BeautifulSoup'],
+ zip_safe=False)
Please sign in to comment.
Something went wrong with that request. Please try again.