import commands, os, posix, re, warnings from config import directory jargon=['blog', 'pingback', 'trackback'] def spellcheck(text): # generate a temp file name warnings.filterwarnings('ignore','tmpnam') tmpnam = posix.tmpnam() # write text out to a temporary file tmp = open(tmpnam,'w') tmp.write(text.encode('ascii','replace').replace(u'’',"'")) tmp.close() # call out to aspell program aspell=commands.getoutput('aspell --add-extra-dicts=%s/addendum.dict -a < %s' % (directory.codebase,tmpnam)) os.remove(tmpnam) # split out all questionable words and non-html text aspell=dict(re.compile('^& (\w+) \d+ \d+: (.*)',re.M).findall(aspell)) # substitute questionable words in text with a titled span if aspell: text = re.split('(<.*?>|&.*?;)',text) for i in range(0,len(text),2): words=re.split(r'\b(\S+)\b',text[i]) for j in range(0,len(words)): if words[j] in aspell: words[j]='%s' % ( aspell[words[j]],words[j]) text[i]=''.join(words) text=''.join(text) # return the concatenated result return "".join(text) if __name__ == '__main__': print spellcheck('I are a good speler')