Navigation Menu

Skip to content

Commit

Permalink
file as of Nov 13, 2007 10:36 pm
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Folkinshteyn committed Jul 2, 2009
1 parent 8640550 commit 010be35
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions ricemaker.py
Expand Up @@ -32,7 +32,7 @@
#
# Author: Daniel Folkinshteyn <dfolkins@temple.edu>
#
# Version: ricemaker.py 0.1.2 13-Nov-2007 dfolkins@temple.edu
# Version: ricemaker.py 0.1.3 14-Nov-2007 dfolkins@temple.edu
#
# Project home (where to get the freshest version):
# http://smokyflavor.wikispaces.com/RiceMaker
Expand All @@ -48,7 +48,33 @@
import time
import traceback
import pickle


###############
# Some configuration variables
###############

# if you have wordnet installed, set this to full path of commandline wordnet executable
# on windows, it might be something like "C:\Program Files\WordNet\wn.exe" ?
wordnetpath = '/usr/bin/wn'

# loop delay config: sleep a random number of seconds, between lowsec and highsec
sleeplowsec = 1
sleephighsec = 5

# filename for internally generated dictionary
# you may specify a full path here, otherwise it will just get written to the same
# directory where this script resides (default behavior)
freericedictfilename = 'freericewordlist.txt'

# number of iterations between dictionary dumps to file
# more often than 30 seconds is really unnecessary...
# consider: iterations * avgsleeptime = time between dumps
iterationsbetweendumps = 10

###############
# Start code
###############

class RiceMaker:

def __init__(self, url):
Expand All @@ -58,9 +84,9 @@ def __init__(self, url):
result = response.read()
self.soup = BeautifulSoup(result)

if os.path.lexists('freericewordlist.txt') and os.path.getsize('freericewordlist.txt') > 0:
if os.path.lexists(freericedictfilename) and os.path.getsize(freericedictfilename) > 0:
try:
f = open('freericewordlist.txt', 'rb')
f = open(freericedictfilename, 'rb')
self.ricewordlist = pickle.load(f)
f.close()
print "dict read successful"
Expand All @@ -72,14 +98,14 @@ def __init__(self, url):
self.ricewordlist = {}

def __del__(self):
f = open('freericewordlist.txt', 'wb')
f = open(freericedictfilename, 'wb')
pickle.dump(self.ricewordlist, f, -1)
f.close()
print 'dump successful'

def dbDump(self):
try:
f = open('freericewordlist.txt', 'wb')
f = open(freericedictfilename, 'wb')
pickle.dump(self.ricewordlist, f, -1)
f.close()
print 'dump successful'
Expand All @@ -93,10 +119,10 @@ def start(self):
while 1:
i = i+1
print "*************************"
if i % 10 == 0:
if i % iterationsbetweendumps == 0:
self.dbDump()

time.sleep(random.randint(1,5)) # let's wait - to not hammer the server, and to not appear too much like a bot
time.sleep(random.randint(sleeplowsec,sleephighsec)) # let's wait - to not hammer the server, and to not appear too much like a bot

try: #to catch all exceptions and ignore them
mydiv = self.soup.findAll(attrs={'class':'wordSelection'})
Expand Down Expand Up @@ -180,7 +206,7 @@ def lookupInMyDict(self, targetword, wordlist):
return self.lookupInWordnet(targetword, wordlist)

def lookupInWordnet(self, targetword, wordlist):
if os.path.lexists('/usr/bin/wn'):
if os.path.lexists(wordnetpath):
executionstring = "wn '" + targetword + "' -synsn -synsv -synsa -synsr -hypen -hypev -hypon -hypov"
p = subprocess.Popen(executionstring, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
returncode = p.wait()
Expand Down

0 comments on commit 010be35

Please sign in to comment.