Commit 5dbcad33 by Will Daly

Merge pull request #47 from edx/will/tempdir-fix

Will/tempdir fix
parents 42f95138 2b66d6b0
from unittest import TestCase
from nose.tools import assert_equal
from mock import patch
from ease.util_functions import spell_correct
class SpellCheckUnitTest(TestCase):
"""
Test that spell-check works correctly.
"""
def test_all_correct(self):
self._assert_spell(
"The quick brown dog jumped over the lazy fox.",
"The quick brown dog jumped over the lazy fox.",
0, "The quick brown dog jumped over the lazy fox."
)
def test_some_misspelled(self):
self._assert_spell(
"The quuick brown dog jimped over the lazy fox.",
"The quick brown dog jumped over the lazy fox.",
2, "The <bs>quuick</bs> brown dog <bs>jimped</bs> over the lazy fox.",
)
def test_empty_str(self):
self._assert_spell("", "", 0, "")
def test_unicode(self):
self._assert_spell(
"The quick brown dog jimped over the \u00FCber fox.",
"The quick brown dog jumped over the \u00FCber fox.",
2, "The quick brown dog <bs>jimped</bs> over the \u00FCber fox.",
)
@patch("util_functions.os.popen")
def test_aspell_not_found(self, popen_mock):
# Expected behavior when aspell is not installed is to return the original
# string with no corrections.
popen_mock.side_effect = OSError
self._assert_spell("Test striiing", "Test striiing", 0, "Test striiing")
def _assert_spell(self, input_str, expected_str, expected_num_changed, expected_markup):
result = spell_correct(input_str)
assert_equal(result, (expected_str, expected_num_changed, expected_markup))
...@@ -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