File size: 3,002 Bytes
d9b105f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/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"(?<![\w.])(%s)\s*[- ]?\s*(%s)(?![a-z/^])" % (NUM, ALT), re.I) # never read short
reader.RX_RANGE = re.compile(r"\bfrom\s+(%s)\s*(%s)?\s+(?:up\s+)?to\s+(%s)\s*(%s)(?![a-z/^])" % (NUM, ALT, NUM, ALT), re.I)
# "to a 12.0-V battery": the word after "to/in" is an article, not the unit ampere
reader.RX_GOAL_UNIT = re.compile(r"\b(?:how many|in|into|as|to)\s+(?:us\s+)?(?!(?:a|an|in|at|as)\s)(%s)(?![a-z/^])" % ALT, re.I)
class Kernel8(Kernel7):
def __init__(self, *a, **k):
_extend_units() # before any face is learned
super().__init__(*a, **k)
vals = sorted(self.induced.idf.values())
self._cue_floor = statistics.median(vals) if vals else 0.0
def goal_of(self, text):
g = super().goal_of(text)
if g is not None and len(g) > 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}
|