Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
First version of pydict
- Loading branch information
0 parents
commit b07f252
Showing
28 changed files
with
464 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include README.rst |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .pydict import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mechanize | ||
BeautifulSoup |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pydict |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .pydict import * |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
Binary file not shown.
Oops, something went wrong.