Replacing words matching regular expressions
Takes long time to move on this because of lack of knowledge.
First I created replacer.py the put under the root of Python27. However, I got following error.
>>> from replacer import * Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named replacer
Teacher Google game me a solution. I didn't import sys in advance.
>>> import sys >>> print sys.path ['', 'C:\\WINDOWS\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\ \lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27 ', 'C:\\Python27\\lib\\site-packages'] >>> from replacers import * Traceback (most recent call last): File "<stdin>", line 1, in <module> File "replacers.py", line 19 def replace(self, repl) in self.patterns: ^ SyntaxError: invalid syntax >>>
Oops, sytax error found. After a couple of try & errors, I got a following results.
>>> from replacers import * >>> replacer = RegexpReplacer() >>> replacer.replace("Can't is a contraction") 'Ca not is a contraction' >>> replacer.replace("can't is a contraction") 'cannot is a contraction' >>> replacer.replace("I should've done that thing I didn't do") 'I should have done that thing I did not do'
(I will add some more here when I have time...)