Im new to Python NLTK and really need your advise.
I want to open my own txt file and do some preprocessing like replacing words with its regex.
I’ve tried to do it as in NLTK 2.0 Cookbook
import re
replacement_patterns = [
(r'won\'t', 'will not'),
(r'can\'t', 'cannot'),
(r'i\'m', 'i am'),
(r'ain\'t', 'is not'),
(r'(\w+)\'ll', '\g<1> will'),
(r'(\w+)n\'t', '\g<1> not'),
(r'(\w+)\'ve', '\g<1> have'),
(r'(\w+t)\'s', '\g<1> is'),
(r'(\w+)\'re', '\g<1> are'),
(r'(\w+)\'d', '\g<1> would'),
]
class RegexpReplacer(object):
def __init__(self, patterns=replacement_patterns):
self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
def replace(self, line):
s = line
for (pattern, repl) in self.patterns:
(s, count) = re.subn(pattern, repl, s)
return s
it works perfect but how can I use it with my txt file?
I’ve tried to do my own way but I think its wrong
import nltk
f=open("C:/nltk_data/file.txt", "rU")
raw=f.readlines()
from replacers import RegexpReplacer
replacer=RegexpReplacer()
replacer.replace(raw)
thx in advance!!!
I think you want to use the read method to read all the file contents into a string first.