Converted to use json for rules instead of yaml
[eliza.git] / eliza.py
index 2a3e04cf810185ae9e5c6cd34ae107a37cd98b0a..c86f3f3974ea44c17a619e3bffd7368da0a4a3b7 100644 (file)
--- 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)