Synonyms (commandline application)

Get Synonyms for any word using the commandline.

This little script demonstrates the power of BeautifulSoup, a library for parsing elements from html or xml files.

the result is something like this:

~> ./synon.py code
Synonyms (Grouped by Similarity of Meaning) of verb code
Sense 1:code
tag, label, mark

Sense 2:code, encipher, cipher, cypher, encrypt, inscribe, write in code
encode
~>

What this script does is acually very simple: it gets the information from www.synonyms.com, then BeautfulSoup finds the info in the right tags.

#!/usr/bin/python

# get synonyms for any word from synonym.com
# uses python + beautifulsoup

import re, urllib, sys
from BeautifulSoup import BeautifulSoup

if (len(sys.argv) > 1):
word = sys.argv[1]
else:
print " usage: synon word"
exit(0)

url = "http://www.synonym.com/synonyms/"+word
f = urllib.urlopen(url)
doc = f.read()

soup = BeautifulSoup(''.join(doc))

def strip_tags(value):
return re.sub(r'<[^>]*?>', '', value)

divs = soup.findAll('div')
result = str(divs[9])
result = result.replace("<br />", "\n")
result = strip_tags(result)
#remove empty lines
a = result.split('\n')
for b in a:
if b:
print b

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments