def demo_discrete_path_sampling(): import EnglishModel initial_state = 'a' terminal_state = 'b' states = 'abcdefghijklmnopqrstuvwxyz ' path_length = 10 transition_matrix = EnglishModel.get_transition_matrix() path = get_discrete_path_sample(initial_state, terminal_state, states, path_length, transition_matrix) print ''.join(path)
def get_form(): """ @return: the body of a form """ # define the default transition matrix dictionary_rate_matrix = EnglishModel.get_transition_matrix() labels = list(sorted(set(a for a, b in dictionary_rate_matrix))) T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels) T = np.array(T) form_objects = [ Form.Matrix('matrix', 'transition matrix', T, MatrixUtil.assert_transition_matrix)] return form_objects
def get_form(): """ @return: the body of a form """ # define the default transition matrix dictionary_rate_matrix = EnglishModel.get_transition_matrix() labels = list(sorted(set(a for a, b in dictionary_rate_matrix))) T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels) T = np.array(T) form_objects = [ Form.Matrix('matrix', 'transition matrix', T, MatrixUtil.assert_transition_matrix) ] return form_objects
def get_form(): """ @return: the body of a form """ # define the default matrix lines dictionary_rate_matrix = EnglishModel.get_transition_matrix() labels = list(sorted(set(a for a, b in dictionary_rate_matrix))) T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels) T = np.array(T) # define the form objects form_objects = [ Form.Matrix('matrix', 'matrix', T), Form.Integer('maxcategories', 'maximum number of categories', 5, low=2)] return form_objects
def get_form(): """ @return: the body of a form """ # define the default transition matrix dictionary_rate_matrix = EnglishModel.get_transition_matrix() labels = list(sorted(set(a for a, b in dictionary_rate_matrix))) T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels) T = np.array(T) # define the form objects form_objects = [ Form.Matrix("matrix", "transition matrix", T, MatrixUtil.assert_transition_matrix), Form.SingleLine("first", "first letter", "a"), Form.SingleLine("last", "last letter", "b"), Form.Integer("count", "character count", 10, low=1, high=80), ] return form_objects
def get_form(): """ @return: the body of a form """ # define the default transition matrix dictionary_rate_matrix = EnglishModel.get_transition_matrix() labels = list(sorted(set(a for a, b in dictionary_rate_matrix))) T = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels) T = np.array(T) # define the form objects form_objects = [ Form.Matrix('matrix', 'transition matrix', T, MatrixUtil.assert_transition_matrix), Form.SingleLine('first', 'first letter', 'a'), Form.SingleLine('last', 'last letter', 'b'), Form.Integer('count', 'character count', 10, low=1, high=80) ] return form_objects
def get_response_content(fs): # define the states using the default transition matrix default_transition_matrix = EnglishModel.get_transition_matrix() states = list(sorted(set(a for a, b in default_transition_matrix))) # read the constraints from the form data if fs.first not in states: raise HandlingError("invalid first letter") if fs.last not in states: raise HandlingError("invalid last letter") if fs.count == 1 and fs.first != fs.last: msg_a = "if a single letter is to be simulated " msg_b = "then the first letter must be the same as the last letter." raise HandlingError(msg_a + msg_b) # read the transition matrix from the form data T = fs.matrix if T.shape[0] != len(states): msg = "expected the transition matrix to have %d lines" % len(states) raise HandlingError(msg) matrix = MatrixUtil.row_major_to_dict(T.tolist(), states, states) # simulate the path path = PathSampler.get_discrete_path_sample(fs.first, fs.last, states, fs.count, matrix) # show the simulated path in convenient text form return "".join(path) + "\n"
def get_response_content(fs): # define the states using the default transition matrix default_transition_matrix = EnglishModel.get_transition_matrix() states = list(sorted(set(a for a, b in default_transition_matrix))) # read the constraints from the form data if fs.first not in states: raise HandlingError('invalid first letter') if fs.last not in states: raise HandlingError('invalid last letter') if fs.count == 1 and fs.first != fs.last: msg_a = 'if a single letter is to be simulated ' msg_b = 'then the first letter must be the same as the last letter.' raise HandlingError(msg_a + msg_b) # read the transition matrix from the form data T = fs.matrix if T.shape[0] != len(states): msg = 'expected the transition matrix to have %d lines' % len(states) raise HandlingError(msg) matrix = MatrixUtil.row_major_to_dict(T.tolist(), states, states) # simulate the path path = PathSampler.get_discrete_path_sample(fs.first, fs.last, states, fs.count, matrix) # show the simulated path in convenient text form return ''.join(path) + '\n'
['ordinary', 'regular', 'everyday', 'usual', 'normalcy'], ['[NOUN]'], ['nature ', 'sex', 'gender'], ['from', 'since', 'because'], ['[POS]'], ['convert', 'divert']], [['Mr.', 'Ms.'], ['Sato'], ['[WA]'], ['be'], ['English'], ['[NOUN]'], ['[POS]'], ['teacher', 'master', 'doctor']], [['today', 'this day'], ['[WA]'], ['be'], [ 'spare time', 'free time', 'leisure', 'time off', 'day off', 'vacation', 'holiday', 'leave' ]], [['be'], ['bullet train'], ['[NOUN]'], ['[POS]'], ['ticket'], ['[NOUN]'], ['[OBJ]'], [ 'buy', 'purchase', 'value', 'have a high opinion', 'stir', 'provoke' ], ['[WANT]']], [['I', 'me'], ['[WA]'], ['read'], ['[NEG]'], ['[PAST]'], ['book', 'volume'], ['[NOUN]'], ['[OBJ]'], ['yesterday']]] model = em.EnglishModel() model.load_bigrams("./w2Caps") for i in inputs: sen = Sentence(i) result = model.rate_sentences(sen.noTags())[0][0] if result[-3:] == " ?.": result = result[:-3] + "?" print result print "_______________" # training inputs = [[['I', 'me'], ['[WA]'], ['drink', 'gulp', 'swallow'], ['[WANT]'], ['milk'], ['[NOUN]'], ['[OBJ]']], [['Mr.', 'Ms.'], ['Robert'], [','], ['I', 'me'], ['[PLR]'], ['[WA]'], ['be', 'exist', 'live', 'have', 'be located', 'be equipped with', 'happen', 'come about'], ['test'], ['tomorrow'], ['[OBJ]']], [['I', 'me'], ['[WA]'], ['study'], ['[NEG]'], ['[PAST]'], ['Japanese'], ['[NOUN]'], ['yesterday'], ['[OBJ]']], [['this'], ['[WA]'], ['be'], ['I', 'me'], ['[POS]'], ['cute', 'adorable', 'charming', 'lovely', 'pretty', 'dear', 'precious', 'darling', 'pet', 'cute little', 'tiny'], ['pen']], [["Let's"], ['[WA]'], ['drink', 'gulp', 'swallow'], ['[VOL]'], ['[QUE]'], ['coffee'], ['[NOUN]'], ['coffee lounge', 'coffee shop', 'cafe'], ['[NOUN]'], ['[LOC]'], ['[OBJ]']], [["Let's"], ['[WA]'], ['study'], ['[VOL]'], ['together', 'at the same time', 'same', 'identical'], ['[DIR]'], ['library'], ['[NOUN]'], ['[LOC]']], [['such', 'like that', 'that sort of', 'very'], ['thing', 'matter', 'incident', 'occurrence', 'event', 'circumstances', 'situation', 'state of affairs', 'work', 'business', 'affair'], ['if', 'in case', 'if it is the case that', 'if it is true that', 'as for', 'on the topic of'], [','], ['cooperate'], ['happily'], ['[LOC]']], [['I', 'me'], ['[WA]'], ['help', 'assist', 'take part in'], ['he', 'him', 'his'], ['[OBJ]']], [['I'], ['[WA]'], ['teach', 'inform', 'instruct'], ['[PROG]'], ['university'], ['[NOUN]'], ['[LOC]']], [['Jason'], ['[NOUN]'], [','], ['hold', 'carry', 'possess'], ['[PROG]'], ['[QUE]'], ['family', 'members of a family'], ['[NOUN]'], ['[POS]'], ['photograph', 'photo', 'movie'], ['[NOUN]'], ['[OBJ]']]]