File size: 3,042 Bytes
2419253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""The Oracle7 kernel, v5: kernel v4 plus the class fix from KERNEL SEALED 2 (m13: "What power, in kilowatts, must the
motor supply? (Neglect friction.)" was answered in watts: the goal reader read the LAST sentence, the parenthetical, so the
asked-for unit was never seen). The goal, like the wanted quantity since v4, is read from the ASKING sentence.
Also the pack front door: Kernel5() with no arguments reads the specialists the released head stands (the union of its
standing lists) from this install, and `python oracle7.py kernel "question"` asks it.
usage: python oracle7.py kernel "question" [--json]   |   python oracle7.py kernel faces"""
from __future__ import annotations

import json
import os
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from kernel_v4 import Kernel4, reader  # noqa: E402

INSTALL = Path(os.environ.get("O7_KERNEL_TREE", HERE.parents[1]))
SPEC = INSTALL / "root" / "specialists"
GATE = SPEC / "_gate"
LISTS = ("standing_v2.txt", "standing_new_v1.txt", "standing_new_v2.txt", "standing_new_v3.txt", "standing_new_v4.txt",
         "standing_new_v5.txt", "standing_owner_v1.txt", "standing_stars_v1.txt", "standing_net_v6.txt")


def pack_standing():
    ids = set()
    for n in LISTS:
        p = GATE / n
        if p.exists():
            ids |= set(p.read_text(encoding="utf-8").split())
    return sorted(ids)


class Kernel5(Kernel4):
    def __init__(self, standing=None, spec_root=None, combo=None):
        super().__init__(standing or pack_standing(), str(spec_root or SPEC), str(combo or GATE))

    def goal_of(self, text):
        t = reader.normalise(text)
        g = reader.read_goal(self._asking(t))          # the asked-for unit is in the asking sentence
        if g is not None:
            return g
        return super().goal_of(text)


def cli(rest):
    as_json = "--json" in rest
    rest = [x for x in rest if x != "--json"]
    k = Kernel5()
    if rest[:1] == ["faces"]:
        print("declared faces (formula parts): %d over %d (quantity, dimension) keys" % (len(k.declared.parts), len(k.declared.keys)))
        print("induced faces (specialists, learned from their own examples): %d" % len(k.induced.faces))
        return 0
    if not rest:
        print(__doc__)
        return 0
    r = k.solve(" ".join(rest))
    if as_json:
        print(json.dumps(r, indent=1, default=str))
        return 0
    if r.get("status") == "answered":
        print(r["answer"])
        path = r.get("path") or [t.get("node") for t in r.get("trace", []) if t.get("node")]
        print("  [answered, %s reading] path: %s" % (r.get("reading", "induced"), " -> ".join(map(str, path)) or "core arithmetic"))
    else:
        print("I can't answer that with a checked path: %s" % (r.get("reason") or "no path"))
        print("  [declined] this goes to the holes as: %s" % json.dumps(r.get("hole") or {"question": " ".join(rest)})[:300])
    return 0


if __name__ == "__main__":
    sys.exit(cli(sys.argv[1:]))