X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=eliza.py;fp=eliza.py;h=c86f3f3974ea44c17a619e3bffd7368da0a4a3b7;hb=d8d232077ea07852f6e627a78c1139d90c37b97a;hp=2a3e04cf810185ae9e5c6cd34ae107a37cd98b0a;hpb=3e0b9e4fe16e573e9343495665d68ef836810d67;p=eliza.git diff --git a/eliza.py b/eliza.py index 2a3e04c..c86f3f3 100644 --- a/eliza.py +++ b/eliza.py @@ -1,6 +1,7 @@ # coding: utf-8 -import yaml +#import yaml +import json import collections import random @@ -11,14 +12,26 @@ pronoun_swaps = { 'me': 'you', 'my': 'your', 'mine': 'yours', - 'am': 'are' + 'am': 'are', + 'you': 'i', + 'your': 'my', + 'yours': 'mine' } +halt_words = 'quit halt exit stop'.split() + +# def read_rules(rules_file): +# with open(rules_file) as f: +# rules = [{'pattern': r['pattern'].split(), +# 'responses': [t.split() for t in r['responses']]} +# for r in yaml.safe_load(f)] +# return rules + def read_rules(rules_file): with open(rules_file) as f: rules = [{'pattern': r['pattern'].split(), 'responses': [t.split() for t in r['responses']]} - for r in yaml.load(f)] + for r in json.load(f)] return rules def is_var(word): @@ -108,14 +121,14 @@ def eliza_loop(rules): print("Hello. I'm Eliza. What seems to be the problem?") while True: c = input("> ") - if c.strip() in 'quit halt exit stop'.split(): break + if c.strip() in halt_words: break comment = c.split() rule, bindings = candidate_rules(rules, comment)[0] swapped_bindings = pronoun_person_swap(bindings) print(' '.join(respond(rule, swapped_bindings))) -all_rules = read_rules('rules.yaml') +all_rules = read_rules('rules.json') eliza_loop(all_rules)