I am reading a blog post related to a Recurrent Neural Network for Natural Language Processing and am trying to recreate the code to practice with. The sample code uses a method to read in a .txt file called file().read()
. I am not familiar with this method and would like to know if it's contained in an importable module, or at least what it would return so I could recreate the method with with different code.
I did attempt to substitute a with open(filename) as f
but it did not return the data in the same format as the file().read()
method seems to. "File" isn't the easiest term to Google if you're looking for a specific result!
def train_char_lm(fname, order=4):
data = file(fname).read()
lm = defaultdict(Counter)
pad = "~" * order
data = pad + data
for i in xrange(len(data)-order):
history, char = data[i:i+order], data[i+order]
lm[history][char]+=1
def normalize(counter):
s = float(sum(counter.values()))
return [(c,cnt/s) for c,cnt in counter.iteritems()]
outlm = {hist:normalize(chars) for hist, chars in lm.iteritems()}
return outlm