def spell_correct(string):
<EOL>f = tempfile.NamedTemporaryFile(mode='<STR_LIT:w>')<EOL>f.write(string)<EOL>f.flush()<EOL>f_path = os.path.abspath(f.name)<EOL>try:<EOL><INDENT>p = os.popen(aspell_path + "<STR_LIT>" + f_path + "<STR_LIT>")<EOL>incorrect = p.readlines()<EOL>p.close()<EOL><DEDENT>except Exception:<EOL><INDENT>log.exception("<STR_LIT>")<EOL>return string,<NUM_LIT:0>, string<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>incorrect_words = list()<EOL>correct_spelling = list()<EOL>for i in range(<NUM_LIT:1>, len(incorrect)):<EOL><INDENT>if(len(incorrect[i]) > <NUM_LIT:10>):<EOL><INDENT>match = re.search("<STR_LIT::>", incorrect[i])<EOL>if hasattr(match, "<STR_LIT:start>"):<EOL><INDENT>begstring = incorrect[i][<NUM_LIT:2>:match.start()]<EOL>begmatch = re.search("<STR_LIT:U+0020>", begstring)<EOL>begword = begstring[<NUM_LIT:0>:begmatch.start()]<EOL>sugstring = incorrect[i][match.start() + <NUM_LIT:2>:]<EOL>sugmatch = re.search("<STR_LIT:U+002C>", sugstring)<EOL>if hasattr(sugmatch, "<STR_LIT:start>"):<EOL><INDENT>sug = sugstring[<NUM_LIT:0>:sugmatch.start()]<EOL>incorrect_words.append(begword)<EOL>correct_spelling.append(sug)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>newstring = string<EOL>markup_string = string<EOL>already_subbed=[]<EOL>for i in range(<NUM_LIT:0>, len(incorrect_words)):<EOL><INDENT>sub_pat = r"<STR_LIT>" + incorrect_words[i] + r"<STR_LIT>"<EOL>sub_comp = re.compile(sub_pat)<EOL>newstring = re.sub(sub_comp, correct_spelling[i], newstring)<EOL>if incorrect_words[i] not in already_subbed:<EOL><INDENT>markup_string=re.sub(sub_comp,'<STR_LIT>' + incorrect_words[i] + "<STR_LIT>", markup_string)<EOL>already_subbed.append(incorrect_words[i])<EOL><DEDENT><DEDENT>return newstring,len(incorrect_words),markup_string<EOL>
Uses aspell to spell correct an input string.
Requires aspell to be installed and added to the path.
Returns the spell corrected string if aspell is found, original string if not.
string - string