Upload engine/predict_yearly.py with huggingface_hub
Browse files- engine/predict_yearly.py +28 -75
engine/predict_yearly.py
CHANGED
|
@@ -8062,91 +8062,39 @@ def _edgar_officer_changes(ticker, months=30):
|
|
| 8062 |
|
| 8063 |
|
| 8064 |
def _edgar_first_8k_mention(ticker, key, words, since):
|
| 8065 |
-
"""
|
| 8066 |
-
|
| 8067 |
-
|
| 8068 |
-
|
| 8069 |
-
cik = _EDGAR_CIK.get(ticker.upper())
|
| 8070 |
-
if not cik or not key:
|
| 8071 |
-
return None
|
| 8072 |
-
best = None
|
| 8073 |
-
for w in words:
|
| 8074 |
-
r = _rq.get("https://efts.sec.gov/LATEST/search-index",
|
| 8075 |
-
params={"q": f'"{key}" "{w}"', "ciks": f"{cik:010d}", "forms": "8-K", "dateRange": "custom", "startdt": since, "enddt": "2099-12-31"},
|
| 8076 |
-
headers={"User-Agent": _EDGAR_UA}, timeout=25)
|
| 8077 |
-
for h in (r.json().get("hits") or {}).get("hits") or []:
|
| 8078 |
-
d = (h.get("_source") or {}).get("file_date")
|
| 8079 |
-
if d and (best is None or d < best):
|
| 8080 |
-
best = d
|
| 8081 |
-
return best
|
| 8082 |
-
except Exception: # noqa: BLE001
|
| 8083 |
-
return None
|
| 8084 |
-
|
| 8085 |
-
|
| 8086 |
-
def _edgar_deal_status(ticker, business, since):
|
| 8087 |
-
"""Resolve whether an announced spin-off / divestiture / acquisition has CLOSED, from the
|
| 8088 |
-
company's own SEC filings (EDGAR full-text search + the filing text), instead of model
|
| 8089 |
-
recall. OSTTRA's $3.1B sale closed 2025-10-10; recall still had it 'pending' a year on.
|
| 8090 |
-
Returns {"status": "completed", "date" (closing date from the sentence, else filing date),
|
| 8091 |
-
"filed", "form", "snippet", "url"} or None (= unknown)."""
|
| 8092 |
import re as _re, html as _html
|
| 8093 |
try:
|
| 8094 |
import requests as _rq
|
| 8095 |
-
except ImportError:
|
| 8096 |
-
return None
|
| 8097 |
-
try:
|
| 8098 |
cik = _EDGAR_CIK.get(ticker.upper())
|
| 8099 |
-
if not cik:
|
| 8100 |
-
return None
|
| 8101 |
-
_generic = {"segment", "division", "business", "unit", "stake", "joint", "venture", "with", "post-trade",
|
| 8102 |
-
"services", "data", "global", "group", "inc", "inc.", "corp", "the", "and", "firm", "company"}
|
| 8103 |
-
toks = [t.strip("(),.'\"") for t in str(business or "").split()]
|
| 8104 |
-
toks = [t for t in toks if len(t) > 3 and t.lower() not in _generic]
|
| 8105 |
-
if not toks:
|
| 8106 |
return None
|
| 8107 |
-
keys = []
|
| 8108 |
-
for k in (toks[0], max(toks, key=lambda t: (t.isupper(), len(t)))):
|
| 8109 |
-
if k not in keys:
|
| 8110 |
-
keys.append(k)
|
| 8111 |
hdr = {"User-Agent": _EDGAR_UA}
|
| 8112 |
-
|
| 8113 |
-
|
| 8114 |
-
for key in keys:
|
| 8115 |
-
pat = _re.compile(rf"((?:completed|closed|consummated|completion of|closing of)[^.]{{0,160}}\b{_re.escape(key)}\b|"
|
| 8116 |
-
rf"\b{_re.escape(key)}\b[^.]{{0,200}}\b(?:was|were|has been|have been|had been|is now)?\s*(?:completed|closed|consummated|complete))", _re.I)
|
| 8117 |
try:
|
| 8118 |
r = _rq.get("https://efts.sec.gov/LATEST/search-index",
|
| 8119 |
-
params={"q": f'"{key}"', "ciks": f"{cik:010d}", "dateRange": "custom", "startdt": since, "enddt": "2099-12-31"},
|
| 8120 |
headers=hdr, timeout=25)
|
| 8121 |
-
|
|
|
|
|
|
|
| 8122 |
except Exception:
|
| 8123 |
continue
|
| 8124 |
-
|
| 8125 |
-
|
| 8126 |
-
|
| 8127 |
-
|
| 8128 |
-
|
| 8129 |
-
|
| 8130 |
-
|
| 8131 |
-
|
| 8132 |
-
|
| 8133 |
-
|
| 8134 |
-
|
| 8135 |
-
continue
|
| 8136 |
-
t = _html.unescape(_re.sub(r"\s+", " ", _re.sub(r"<[^>]+>", " ", t)))
|
| 8137 |
-
for m in pat.finditer(t):
|
| 8138 |
-
sn = m.group(1)
|
| 8139 |
-
if neg.search(sn):
|
| 8140 |
-
continue
|
| 8141 |
-
ctx = t[max(0, m.start() - 220): m.end() + 60]
|
| 8142 |
-
dm_ = None
|
| 8143 |
-
for dmm in _datepat.finditer(ctx):
|
| 8144 |
-
try:
|
| 8145 |
-
dm_ = datetime.strptime(dmm.group(0).replace(",", ""), "%B %d %Y").strftime("%Y-%m-%d")
|
| 8146 |
-
except ValueError:
|
| 8147 |
-
pass
|
| 8148 |
-
return {"status": "completed", "date": dm_ or src.get("file_date"), "filed": src.get("file_date"),
|
| 8149 |
-
"form": src.get("form"), "snippet": sn[:220].strip(), "url": url}
|
| 8150 |
return None
|
| 8151 |
except Exception: # noqa: BLE001
|
| 8152 |
return None
|
|
@@ -8688,7 +8636,12 @@ Examples:
|
|
| 8688 |
_rstb = (all_data.get("restatement") or {}).get("business")
|
| 8689 |
if _rstb and _rstb != "a divested business":
|
| 8690 |
_key0 = next((t.strip("(),.") for t in _rstb.split() if len(t.strip("(),.")) > 3), None)
|
| 8691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8692 |
if _ann:
|
| 8693 |
all_data["restatement"]["announced"] = _ann
|
| 8694 |
print(f" [edgar] {_key0} separation first appears in an 8-K on {_ann}")
|
|
|
|
| 8062 |
|
| 8063 |
|
| 8064 |
def _edgar_first_8k_mention(ticker, key, words, since):
|
| 8065 |
+
"""Announcement date of a spin-off/divestiture: the earliest 8-K whose TEXT contains an
|
| 8066 |
+
announcement sentence naming the unit ("announced ... plan to separate Mobility"). A plain
|
| 8067 |
+
term co-occurrence search dated Mobility to a 2024 earnings release; the announcement was
|
| 8068 |
+
the 8-K of 2025-04-29."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8069 |
import re as _re, html as _html
|
| 8070 |
try:
|
| 8071 |
import requests as _rq
|
|
|
|
|
|
|
|
|
|
| 8072 |
cik = _EDGAR_CIK.get(ticker.upper())
|
| 8073 |
+
if not cik or not key:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8074 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8075 |
hdr = {"User-Agent": _EDGAR_UA}
|
| 8076 |
+
cands = {}
|
| 8077 |
+
for w in words:
|
|
|
|
|
|
|
|
|
|
| 8078 |
try:
|
| 8079 |
r = _rq.get("https://efts.sec.gov/LATEST/search-index",
|
| 8080 |
+
params={"q": f'"{key}" "{w}"', "ciks": f"{cik:010d}", "forms": "8-K", "dateRange": "custom", "startdt": since, "enddt": "2099-12-31"},
|
| 8081 |
headers=hdr, timeout=25)
|
| 8082 |
+
for h in (r.json().get("hits") or {}).get("hits") or []:
|
| 8083 |
+
d = (h.get("_source") or {}).get("file_date")
|
| 8084 |
+
if d: cands[h.get("_id")] = d
|
| 8085 |
except Exception:
|
| 8086 |
continue
|
| 8087 |
+
sent_pat = _re.compile(rf"[^.]*\b(announc\w*|intend\w*|plan\w*|explor\w*|pursu\w*|decid\w*|approv\w*)[^.]*\b(separat\w*|spin[- ]?off|divest\w*|sale of)[^.]*\b{_re.escape(key)}\b[^.]*\."
|
| 8088 |
+
rf"|[^.]*\b{_re.escape(key)}\b[^.]*\b(separat\w*|spin[- ]?off|divest\w*)[^.]*\b(announc\w*|intend\w*|plan\w*|explor\w*|pursu\w*|decid\w*|approv\w*)[^.]*\.", _re.I)
|
| 8089 |
+
for _id, d in sorted(cands.items(), key=lambda kv: kv[1])[:6]:
|
| 8090 |
+
try:
|
| 8091 |
+
adsh, fn = str(_id).split(":")
|
| 8092 |
+
t = _rq.get(f"https://www.sec.gov/Archives/edgar/data/{cik}/{adsh.replace('-', '')}/{fn}", headers=hdr, timeout=20).text
|
| 8093 |
+
except Exception:
|
| 8094 |
+
continue
|
| 8095 |
+
t = _html.unescape(_re.sub(r"\s+", " ", _re.sub(r"<[^>]+>", " ", t)))
|
| 8096 |
+
if sent_pat.search(t):
|
| 8097 |
+
return d
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8098 |
return None
|
| 8099 |
except Exception: # noqa: BLE001
|
| 8100 |
return None
|
|
|
|
| 8636 |
_rstb = (all_data.get("restatement") or {}).get("business")
|
| 8637 |
if _rstb and _rstb != "a divested business":
|
| 8638 |
_key0 = next((t.strip("(),.") for t in _rstb.split() if len(t.strip("(),.")) > 3), None)
|
| 8639 |
+
_eff0 = (all_data["restatement"].get("effective") or datetime.now().strftime("%Y-%m-%d"))[:10]
|
| 8640 |
+
try:
|
| 8641 |
+
_since0 = (datetime.strptime(_eff0, "%Y-%m-%d") - timedelta(days=600)).strftime("%Y-%m-%d")
|
| 8642 |
+
except ValueError:
|
| 8643 |
+
_since0 = (datetime.now() - timedelta(days=600)).strftime("%Y-%m-%d")
|
| 8644 |
+
_ann = _edgar_first_8k_mention(ticker, _key0, ("separation", "spin-off", "spinoff", "divestiture"), _since0)
|
| 8645 |
if _ann:
|
| 8646 |
all_data["restatement"]["announced"] = _ann
|
| 8647 |
print(f" [edgar] {_key0} separation first appears in an 8-K on {_ann}")
|