def get_rule(rule_string_format): return { # tuple(k.split()): [line.split() for line in v] # tuple(cut(k)): [cut(line) for line in v] tuple([i for i in cut(k) if i.strip()]): [cut(line) for line in v] # modified for k, v in rule_string_format.items() }
def eliza(speech): # speech = speech.split() speech = cut(speech) for index, (pattern, responses) in enumerate(get_rule(rule_responses).items()): match = pattern_match_l(list(pattern), speech) if match is not None and match != fail: response = random.choice(responses) return sub_list(match, response) return None
def segWAV(inputFile): f = open("tmp.segment", "rb+") reader = csv.reader(f, delimiter=',') # 切分点初始化时添加开始点——-0 delimit_points = [0] for row in reader: delimit_points.append(int(row[0])) delimit_points.append(int(row[1])) delimit_list = utilities.cut(inputFile, delimit_points) f.close() return delimit_list
assert pattern_match_l('?X is ?X'.split(), '2 + 2 is 4'.split()) is None assert pattern_match_l(['?X', 'is', '?X'], ['2 + 2', 'is', '4']) is None assert dict(pattern_match_l(['?X', 'is', '?X'], ['2 + 2', 'is', '2 + 2'])) == { '?X': '2 + 2' } r = pattern_match_l(['?P', 'need', '?X'], ['I', 'need', 'a', 'long', 'trip']) assert dict(r) == {'?P': 'I', '?X': 'a long trip'}, dict(r) r = pattern_match_l(['?*P', 'need', '?*X'], ['Mr', 'Hulot', 'and', 'I', 'need', 'a', 'vacation']) assert dict(r) == { '?P': ['Mr', 'Hulot', 'and', 'I'], '?X': ['a', 'vacation'] }, dict(r) r = pattern_match_l(['?*x', 'is', 'a', '?*y'], ['what', 'he', 'is', 'is', 'a', 'fool']) assert dict(r) == {'?x': ['what', 'he', 'is'], '?y': ['fool']}, dict(r) r = pattern_match_l(['?*x', 'a', 'b', '?*x'], '1 2 a b a b 1 2 a b'.split()) assert dict(r) == {'?x': ['1', '2', 'a', 'b']}, dict(r) r = pattern_match_l(['我', '想', '?*y'], ['我']) assert r == fail r = pattern_match_l(cut('?*x我想?*y'), cut('然后我爸爸就骂我')) assert r == fail print('test done!')
if match is not None and match != fail: response = random.choice(responses) return sub_list(match, response) return None def compose_single_sentene(tree_words): results = [] for e in tree_words: if isinstance(e, list): results += compose_single_sentene(e) else: results.append(e) return results if __name__ == '__main__': while True: sentence = input('USER >>>') match = eliza(sentence) if match: print('{:>40}<<<'.format(concat(compose_single_sentene(match)))) else: print('sorry I don\'t know it') sentence = '三个人怎么读?' match = eliza(sentence) if '读' in match: cut(sentence) print(match)
def get_rule(rule_string_format): return { # tuple(k.split()): [line.split() for line in v] tuple(cut(k)): [cut(line) for line in v] for k, v in rule_string_format.items() }
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: feng from utilities import cut from eliza.pattern_match import pattern_match_l, segment_match, defaultdict # pattern=tuple(cut('?*xAI?*y')) # speech='hello' pattern = tuple(cut('?*x hello ?*y')) speech = 'hello' print(pattern_match_l(list(pattern), speech)) bindings = None or defaultdict(lambda: None) input = speech segment_match(pattern, input, bindings) # 0, get_rule function changed # 1, pattern => complete split this pattern? # 2, first_match_pos function chang?