<jupyter_start><jupyter_text>Winograd Schema with Deeper SemanticsThis sample builds on the earlier Winograd Schema examples but adds a deeper semantic parsing technique, to construct an item and add attributes to it.As usual, fire up the engine.<jupyter_code>import os, sys
sys.path.insert(1, os.path.abspath('..\\..'))

from thoughts.rules_engine import RulesEngine
import pprint

engine = RulesEngine()<jupyter_output><empty_output><jupyter_text>Define Your RulesThe initial rules are the same as the simpler Winograd Schema examples. Note there is a new rule to detect "?x is to ?quality", which will construct an item to track this quality about ?x.<jupyter_code>rules = [
  {
    "#when": { "parse": "?effect because ?cause" },
    "#then": ["?effect"]
  },

  {
    "#when": "?x doesn't fit inside ?y",
    "#then": ["?x is too large", "?y is too small"]
  },

  {
    "#when": "?x is too ?quality",
    "#then": [ {"#store": { "is": { "quality": "?quality", "degree": "excessive" } }, 
                "#into": "?x" }
    ]
  },

  {
    "#when": { "parse": "what was too ?quality" },
    "#then": [{"#lookup": { "is": { "quality": "?quality", "degree": "excessive" } }, "#into": "?answer" },
              {"#output": "?answer.item is too ?quality"}
    ]
  }
]

engine.load_rules_from_list(rules, "winograd-1")<jupyter_output><empty_output><jupyter_text>Assert the PremiseWhen the engine runs the "?x is too ?quality" rule actually fires twice - once for the lamp being too large and once for the suitcase being too small. The end result is two items in memory.<jupyter_code>premise = "the lamp doesn't fit inside the suitcase because it is too large"
conclusions = engine.process({"parse": premise})
print("Conclusions:")
pprint.pprint(conclusions)<jupyter_output>Conclusions:
["the lamp doesn't fit inside the suitcase",
 'the lamp is too large',
 'the suitcase is too small',
 {'#into': 'the lamp',
  '#store': {'is': {'degree': 'excessive', 'quality': 'large'}}},
 {'#into': 'the suitcase',
  '#store': {'is': {'degree': 'excessive', 'quality': 'small'}}}]<jupyter_text>Then if a question is asserted, "what was too ?quality", this information is retrieved via the lookup command and asserted. That assertion triggers the last rule, to output this information back.<jupyter_code>question = "what was too large"
result = engine.process({"parse": question})<jupyter_output>the lamp is too large
{'fraction_non_alphanumeric': 0.15253532834580216, 'fraction_numerical': 0.0008312551953449709, 'mean_word_length': 4.5846867749419955, 'pattern_counts': {'":': 23, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '72066', 'license': 'MIT', 'n_tokens_mistral': 775, 'n_tokens_neox': 746, 'n_words': 270, 'path': 'notebooks/academic/010_winograd_0304.ipynb', 'repo_name': 'hofmanniac/thoughts', 'size': '2406'}