Price moves checked against the price, return magnitude bands, pronoun objects and anchor-vs-price compounds, raw identifiers
Browse files- engine/prose.py +45 -6
- engine/tests/test_spgi_relations.py +28 -0
engine/prose.py
CHANGED
|
@@ -550,8 +550,14 @@ def valuation_relation_violation(sentence, vrels):
|
|
| 550 |
(price_named and (re.search(r"\b(?:it|they)\b", before) or bare))
|
| 551 |
said = rtype
|
| 552 |
if not compound and dm.start() >= am.start(): # "<anchor> … below … <price>": anchor is subject
|
| 553 |
-
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
if not explicit and rid not in toks:
|
| 556 |
return (f"'{w}' about the {a} names no subject and object (whose {w}?); state it as the "
|
| 557 |
f"price relative to the {a}, or cite [[{rid}]]")
|
|
@@ -939,7 +945,10 @@ MAGNITUDE_BANDS = (("low-single-digit", 0.0, 0.04), ("mid-single-digit", 0.03, 0
|
|
| 939 |
("single-digit", 0.0, 0.10), ("low-double-digit", 0.09, 0.14), ("low-teens", 0.09, 0.14),
|
| 940 |
("mid-teens", 0.13, 0.17), ("high-teens", 0.16, 0.20), ("double-digit", 0.095, 1.0),
|
| 941 |
("low-twenties", 0.19, 0.24), ("mid-twenties", 0.23, 0.27), ("high-twenties", 0.26, 0.30))
|
| 942 |
-
MAGNITUDE_FAMILIES = {"
|
|
|
|
|
|
|
|
|
|
| 943 |
"REVENUE_GROWTH": (("revenue", "sales", "top-line", "top line", "growth"),
|
| 944 |
("LIFECYCLE_FORWARD_GROWTH", "LIFECYCLE_TRAILING_GROWTH", "FORECAST_REVENUE_GROWTH"),
|
| 945 |
("REVENUE_GROWTH", "ORGANIC_REVENUE_GROWTH"))}
|
|
@@ -958,7 +967,8 @@ def magnitude_violation(sentence, catalog):
|
|
| 958 |
continue
|
| 959 |
fam = min(pos)[1]
|
| 960 |
words, cids, concepts = MAGNITUDE_FAMILIES[fam]
|
| 961 |
-
vals = [float(catalog[c].value)
|
|
|
|
| 962 |
for cid, c in catalog.items():
|
| 963 |
v = c.value if cid.startswith("GUIDANCE_") and isinstance(c.value, dict) else None
|
| 964 |
if v and v.get("concept") in concepts:
|
|
@@ -967,8 +977,8 @@ def magnitude_violation(sentence, catalog):
|
|
| 967 |
wide_lo = lo - 0.01 - (0.03 if "to-" in m.group(0) else 0.0)
|
| 968 |
if not any(wide_lo <= x <= hi + 0.01 for x in vals):
|
| 969 |
shown = ", ".join(format_percent(x, 1, sign=True) for x in vals[:3]) or "none"
|
| 970 |
-
return (f"'{m.group(0).replace('-to-', ' to ')}'
|
| 971 |
-
f"
|
| 972 |
return None
|
| 973 |
|
| 974 |
|
|
@@ -1007,6 +1017,29 @@ def complete_set_violation(sentence, catalog):
|
|
| 1007 |
return None
|
| 1008 |
|
| 1009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1010 |
def public_language_violation(sentence, terms):
|
| 1011 |
"""Internal QA or state vocabulary (coherence checks, verification status, validators, the claim registry) in text a
|
| 1012 |
reader sees. The terms are the system's own internal states and fields, not a list of forbidden opinions."""
|
|
@@ -1481,6 +1514,12 @@ def check_sentence(sentence, rules: Rules):
|
|
| 1481 |
sd = statement_domain_violation(sentence, rules.statements)
|
| 1482 |
if sd:
|
| 1483 |
return False, f"STATEMENT_DOMAIN: {sd}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1484 |
pl = public_language_violation(sentence, rules.internal_terms)
|
| 1485 |
if pl:
|
| 1486 |
return False, f"PUBLIC_LANGUAGE_ONLY: {pl}"
|
|
|
|
| 550 |
(price_named and (re.search(r"\b(?:it|they)\b", before) or bare))
|
| 551 |
said = rtype
|
| 552 |
if not compound and dm.start() >= am.start(): # "<anchor> … below … <price>": anchor is subject
|
| 553 |
+
between = low[am.end():dm.start()]
|
| 554 |
+
if any(re.search(r"\b" + p + r"\b", between) for p in PRICE_TERMS):
|
| 555 |
+
explicit, said = True, rtype # "composite-vs-price discount"
|
| 556 |
+
else:
|
| 557 |
+
after_ = low[dm.end():dm.end() + 40]
|
| 558 |
+
explicit = any(p in after_ for p in PRICE_TERMS) or \
|
| 559 |
+
(price_named and re.match(r"\W*(?:the\s+)?(?:it|them)\b", after_))
|
| 560 |
+
said = flip[rtype]
|
| 561 |
if not explicit and rid not in toks:
|
| 562 |
return (f"'{w}' about the {a} names no subject and object (whose {w}?); state it as the "
|
| 563 |
f"price relative to the {a}, or cite [[{rid}]]")
|
|
|
|
| 945 |
("single-digit", 0.0, 0.10), ("low-double-digit", 0.09, 0.14), ("low-teens", 0.09, 0.14),
|
| 946 |
("mid-teens", 0.13, 0.17), ("high-teens", 0.16, 0.20), ("double-digit", 0.095, 1.0),
|
| 947 |
("low-twenties", 0.19, 0.24), ("mid-twenties", 0.23, 0.27), ("high-twenties", 0.26, 0.30))
|
| 948 |
+
MAGNITUDE_FAMILIES = {"RETURN": (("total return", "return", "upside", "downside"),
|
| 949 |
+
("SCENARIO_BEAR_RETURN", "SCENARIO_NEUTRAL_RETURN", "SCENARIO_BULL_RETURN", "PM_RETURN",
|
| 950 |
+
"HEADLINE_RETURN", "TARGET_VS_PRICE", "RETURN_12M", "RETURN_3M"), ()),
|
| 951 |
+
"EPS_GROWTH": (("eps", "earnings", "profits", "profit"), ("FORECAST_EPS_GROWTH",), ("EPS_GROWTH",)),
|
| 952 |
"REVENUE_GROWTH": (("revenue", "sales", "top-line", "top line", "growth"),
|
| 953 |
("LIFECYCLE_FORWARD_GROWTH", "LIFECYCLE_TRAILING_GROWTH", "FORECAST_REVENUE_GROWTH"),
|
| 954 |
("REVENUE_GROWTH", "ORGANIC_REVENUE_GROWTH"))}
|
|
|
|
| 967 |
continue
|
| 968 |
fam = min(pos)[1]
|
| 969 |
words, cids, concepts = MAGNITUDE_FAMILIES[fam]
|
| 970 |
+
vals = [abs(float(catalog[c].value)) if fam == "RETURN" else float(catalog[c].value)
|
| 971 |
+
for c in cids if c in catalog and isinstance(catalog[c].value, (int, float))]
|
| 972 |
for cid, c in catalog.items():
|
| 973 |
v = c.value if cid.startswith("GUIDANCE_") and isinstance(c.value, dict) else None
|
| 974 |
if v and v.get("concept") in concepts:
|
|
|
|
| 977 |
wide_lo = lo - 0.01 - (0.03 if "to-" in m.group(0) else 0.0)
|
| 978 |
if not any(wide_lo <= x <= hi + 0.01 for x in vals):
|
| 979 |
shown = ", ".join(format_percent(x, 1, sign=True) for x in vals[:3]) or "none"
|
| 980 |
+
return (f"'{m.group(0).replace('-to-', ' to ')}' fits no {fam.lower().replace('_', ' ')} claim "
|
| 981 |
+
f"(available: {shown})")
|
| 982 |
return None
|
| 983 |
|
| 984 |
|
|
|
|
| 1017 |
return None
|
| 1018 |
|
| 1019 |
|
| 1020 |
+
# a price move "to <per-share value>" whose direction the two values contradict ("compression to $443.00" above $403.30)
|
| 1021 |
+
_MOVE_DOWN = r"\b(?:compress\w*|compression|fall\w*|fell|declin\w*|drop\w*|de-?rat\w*|slid\w*|slump\w*|contract\w*)\b"
|
| 1022 |
+
_MOVE_UP = r"\b(?:ris\w*|rose|rall\w*|re-?rat\w*|climb\w*|expan\w*|recover\w*|gain\w*)\b"
|
| 1023 |
+
|
| 1024 |
+
|
| 1025 |
+
def price_move_violation(sentence, catalog):
|
| 1026 |
+
price = getattr(catalog.get("PRICE"), "value", None)
|
| 1027 |
+
if not isinstance(price, (int, float)):
|
| 1028 |
+
return None
|
| 1029 |
+
for m in TOKEN.finditer(sentence):
|
| 1030 |
+
c = catalog.get(m.group(1))
|
| 1031 |
+
if getattr(c, "stype", None) != "CURRENCY" or m.group(1) == "PRICE" or not isinstance(c.value, (int, float)):
|
| 1032 |
+
continue
|
| 1033 |
+
before = sentence[max(0, m.start() - 45):m.start()].lower()
|
| 1034 |
+
if not re.search(r"\bto\s*$", before):
|
| 1035 |
+
continue
|
| 1036 |
+
if re.search(_MOVE_DOWN, before) and c.value > price:
|
| 1037 |
+
return f"a decline 'to [[{m.group(1)}]]' ({c.rendered}) would be a rise: it is above the price ({catalog['PRICE'].rendered})"
|
| 1038 |
+
if re.search(_MOVE_UP, before) and c.value < price:
|
| 1039 |
+
return f"a rise 'to [[{m.group(1)}]]' ({c.rendered}) would be a fall: it is below the price ({catalog['PRICE'].rendered})"
|
| 1040 |
+
return None
|
| 1041 |
+
|
| 1042 |
+
|
| 1043 |
def public_language_violation(sentence, terms):
|
| 1044 |
"""Internal QA or state vocabulary (coherence checks, verification status, validators, the claim registry) in text a
|
| 1045 |
reader sees. The terms are the system's own internal states and fields, not a list of forbidden opinions."""
|
|
|
|
| 1514 |
sd = statement_domain_violation(sentence, rules.statements)
|
| 1515 |
if sd:
|
| 1516 |
return False, f"STATEMENT_DOMAIN: {sd}"
|
| 1517 |
+
mv_ = price_move_violation(sentence, rules.catalog)
|
| 1518 |
+
if mv_:
|
| 1519 |
+
return False, f"VALUATION_RELATION_LANGUAGE: {mv_}"
|
| 1520 |
+
raw = [w for w in re.findall(r"(?<!\[\[)\b[A-Z][A-Z0-9]+(?:_[A-Z0-9]+)+\b(?!\]\])", TOKEN.sub(" ", sentence))]
|
| 1521 |
+
if raw:
|
| 1522 |
+
return False, f"PUBLIC_LANGUAGE_ONLY: internal identifier '{raw[0]}' written as text"
|
| 1523 |
pl = public_language_violation(sentence, rules.internal_terms)
|
| 1524 |
if pl:
|
| 1525 |
return False, f"PUBLIC_LANGUAGE_ONLY: {pl}"
|
engine/tests/test_spgi_relations.py
CHANGED
|
@@ -1474,3 +1474,31 @@ def test_20_boundary_repeats_stage_terms_elliptical_subjects_and_coordinated_pre
|
|
| 1474 |
"Engine EPS for the next twelve months [[REL_FORECAST_EPS_CONSENSUS__PREDICATE]]."):
|
| 1475 |
ok, why = P.check_sentence(s_, rules)
|
| 1476 |
assert ok, (s_, why)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1474 |
"Engine EPS for the next twelve months [[REL_FORECAST_EPS_CONSENSUS__PREDICATE]]."):
|
| 1475 |
ok, why = P.check_sentence(s_, rules)
|
| 1476 |
assert ok, (s_, why)
|
| 1477 |
+
|
| 1478 |
+
|
| 1479 |
+
def test_21_price_moves_return_bands_pronoun_objects_and_raw_ids(good):
|
| 1480 |
+
"""Live 54c37e3e26ddaa90: "multiple compression to [[MARKET_COMPOSITE]] or below" ($443.00 is above the $403.30
|
| 1481 |
+
price), "supporting high-teens total return" (the bull case is +22.0%), PM prose rejected for "the market composite sits
|
| 1482 |
+
above it" and "the composite-vs-price discount", and a raw "REL_EXPECTATIONS_STORY" typed as text."""
|
| 1483 |
+
import dataclasses
|
| 1484 |
+
run, _ = good
|
| 1485 |
+
cat, rules = _rules(run)
|
| 1486 |
+
price = cat["PRICE"].value
|
| 1487 |
+
above = next((c for c, x in cat.items() if x.stype == C.CURRENCY and c != "PRICE" and isinstance(x.value, float)
|
| 1488 |
+
and x.value > price * 1.02 and c in (rules.offered or cat)), None)
|
| 1489 |
+
if above:
|
| 1490 |
+
ok, why = P.check_sentence(f"The bear case needs only modest multiple compression to [[{above}]] or below.", rules)
|
| 1491 |
+
assert not ok and "would be a rise" in why, why
|
| 1492 |
+
ok, why = P.check_sentence("Operating leverage supports high-teens total return.", rules)
|
| 1493 |
+
fits = any(0.15 <= abs(cat[c].value) <= 0.21 for c in ("SCENARIO_BULL_RETURN", "PM_RETURN", "HEADLINE_RETURN",
|
| 1494 |
+
"TARGET_VS_PRICE", "RETURN_12M", "SCENARIO_BEAR_RETURN",
|
| 1495 |
+
"SCENARIO_NEUTRAL_RETURN", "RETURN_3M")
|
| 1496 |
+
if c in cat and isinstance(cat[c].value, float))
|
| 1497 |
+
assert ok == fits, why
|
| 1498 |
+
vr = dataclasses.replace(rules, valuation_relations={"REL_PRICE_INTRINSIC": RL.ABOVE, "REL_PRICE_MARKET_COMPOSITE": RL.BELOW})
|
| 1499 |
+
for s_ in ("The price sits between the anchors: the story value is well below price while the market composite sits above it.",
|
| 1500 |
+
"I lean slightly bullish on the composite-vs-price discount."):
|
| 1501 |
+
ok, why = P.check_sentence(s_, vr)
|
| 1502 |
+
assert ok, (s_, why)
|
| 1503 |
+
ok, why = P.check_sentence("REL_EXPECTATIONS_STORY shows the price needs more than the story.", rules)
|
| 1504 |
+
assert not ok and why.startswith("PUBLIC_LANGUAGE_ONLY"), why
|