Commit 2b66d6b0 by Will Daly

Create unique tempfiles in the system /tmp directory

parent cc97ad30
...@@ -13,6 +13,7 @@ import nltk ...@@ -13,6 +13,7 @@ import nltk
import pickle import pickle
import logging import logging
import sys import sys
import tempfile
log=logging.getLogger(__name__) log=logging.getLogger(__name__)
...@@ -84,20 +85,27 @@ def spell_correct(string): ...@@ -84,20 +85,27 @@ def spell_correct(string):
string - string string - string
""" """
#Create a temp file so that aspell could be used # Create a temp file so that aspell could be used
f = open('tmpfile', 'w') # By default, tempfile will delete this file when the file handle is closed.
f = tempfile.NamedTemporaryFile(mode='w')
f.write(string) f.write(string)
f.flush()
f_path = os.path.abspath(f.name) f_path = os.path.abspath(f.name)
f.close()
try: try:
p = os.popen(aspell_path + " -a < " + f_path + " --sug-mode=ultra") p = os.popen(aspell_path + " -a < " + f_path + " --sug-mode=ultra")
except:
log.exception("Could not find aspell, so could not spell correct!") # Aspell returns a list of incorrect words with the above flags
#Return original string if aspell fails incorrect = p.readlines()
p.close()
except Exception:
log.exception("aspell process failed; could not spell check")
# Return original string if aspell fails
return string,0, string return string,0, string
#Aspell returns a list of incorrect words with the above flags
incorrect = p.readlines() finally:
p.close() f.close()
incorrect_words = list() incorrect_words = list()
correct_spelling = list() correct_spelling = list()
for i in range(1, len(incorrect)): for i in range(1, len(incorrect)):
...@@ -487,4 +495,4 @@ def getMedian(numericValues): ...@@ -487,4 +495,4 @@ def getMedian(numericValues):
lower = theValues[len(theValues) / 2 - 1] lower = theValues[len(theValues) / 2 - 1]
upper = theValues[len(theValues) / 2] upper = theValues[len(theValues) / 2]
return (float(lower + upper)) / 2 return (float(lower + upper)) / 2
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment