#!/usr/bin/env python3 """The Oracle7 kernel, v8: three classes found by v7's OpenStax run (3 wrong answers), each fixed at one place. 1. A UNIT READ SHORT. "1250 N/m" was read as "1250 N" (the reader had no "N/m"), so every "springs" face learned its spring constant as a FORCE, and a cart's 10 N push and a nail puller's 1250 N were then taken as spring constants. Fix: units with "/" composed from units already defined (N/m, J/kg, W/m^2, kg/m^3 ...), and a quantity whose unit continues past what was read ("N" then "/m") is never read short. Faces are learned again under this reader. 2. AN ARTICLE READ AS A UNIT. "in series to a 12.0-V battery" gave the goal unit "a" (amperes). One-letter unit words that are English words ("a", "in") are never a goal unit. 3. A COMMON WORD AS A SLOT CUE. "one headlight" matched the cue "one" of "one leg is {1}". Cue words must be distinctive: IDF above the median of the face vocabulary. usage: module (Kernel8, a drop-in for Kernel7)""" from __future__ import annotations import re import statistics import kernel_v5 import kernel_v7 from kernel_v7 import Kernel7 reader = kernel_v5.reader DENS = ("m", "s", "kg", "h", "m^2", "m2", "m^3", "m3", "s^2", "s2", "kwh", "mol", "l", "min", "day", "km", "cm") def _extend_units(): U = reader.UNITS if U.get("n/m"): return for num in [k for k in list(U) if " " not in k and "/" not in k and "^" not in k]: for den in DENS: if den in U and num != den: (f1, d1), (f2, d2) = U[num], U[den] if isinstance(d1, tuple) and isinstance(d2, tuple): U.setdefault("%s/%s" % (num, den), (f1 / f2, reader.dmul(d1, d2, -1))) reader.UNIT_ALT = "|".join(sorted((re.escape(k) for k in U), key=len, reverse=True)) NUM, ALT = reader.NUM, reader.UNIT_ALT reader.RX_QTY = re.compile(r"(? 2 and str(g[2]).strip().lower() in ("a", "an", "in", "at", "as"): return None # an article or preposition, never a unit return g def _cues(self, f): c = super()._cues(f) return {w for w in c if self.induced.idf.get(w, 0) > self._cue_floor}