text
stringlengths
1
93.6k
with open(filename + '_bigrams', 'wb+') as f:
pickle.dump(self.bigrams, f)
def load(self, filename):
self.unigrams = np.load(filename + '_unigrams')
with open(filename + '_bigrams', 'rb') as f:
self.bigrams = pickle.load(f)
def get_language_model(lm_type, w2id, test=False):
if lm_type is None:
return None
if lm_type == 'uniform':
return None
elif lm_type == 'unigram':
return lm.UnigramLanguageModel(w2id)
elif lm_type == 'bigram':
return lm.BigramLanguageModel(w2id)
else:
print('Unknown language model %s, using unigram language model' % lm_type)
return lm.UnigramLanguageModel(w2id)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Train a simple LM',
)
parser.add_argument('train_corpus', type=str, help='a path to the training corpus')
parser.add_argument('out', type=str, help='Output file')
parser.add_argument('--lm_type', '-lm', type=str, default=None,
help='Type of language model (for instance unigram or bigram)')
parser.add_argument('--dic', '-d', type=str, default=None,
help='Path to a pickled dictionnary (maps words to ints). '
'If not provided, will learn from training data')
parser.add_argument("--verbose", '-v',
help="increase output verbosity",
action="store_true")
opt = parser.parse_args()
# Load/create dic
if opt.dic is None:
dic = data.read_dic(opt.train_corpus)
else:
dic = data.load_dic(opt.dic)
# Create LM object
lm = get_language_model(opt.lm_type, dic)
if lm is not None:
# Load data
train_data = data.read_corpus(opt.train_corpus, dic)
# Train lm
lm.fit(train_data)
# Save lm
lm.save(opt.out)
# <FILESEP>
################################################################################
#
# Program: Delete All The Files In A Directory
#
# Description: A program to delete all the files in a directory using Python
# with functions in the built-in os module.
#
# YouTube Lesson: https://www.youtube.com/watch?v=61Mhj94-0I8
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# import the os module which includes helpful functions for working with files
# and directories
import os
# The path of the directory where we want to delete all the files, ./ is the
# current working directory
path = "./"
# The listdir() function will return a list of all the files and directories in
# directory stored in path, we'll loop through each of these setting name to
# each file/directory
for name in os.listdir(path):
# Build the path to the file or directory by concatenating the name to the
# directory path
file = path + name
# The path.isfile() function will return true if the path we've constructed
# above is a path for a file, otherwise it will return false. If we have a
# file, we delete it using the remove() function.
if os.path.isfile(file):
os.remove(file)
# <FILESEP>
import argparse
from options import process_options
import trainers
from encode import FullDatasetEncoder
parser = argparse.ArgumentParser()
# master options
parser.add_argument('cmd',
choices = ['stage1', 'encode', 'classifier', 'stage2', 'gan'],
help = 'action to be performed')