-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
50 lines (44 loc) · 2.09 KB
/
validators.py
File metadata and controls
50 lines (44 loc) · 2.09 KB
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
import re
from typing import List, Tuple
from .schemas import Patch, Event, Update
INVEST_TOKENS = ['invest','funded','wired','purchase','bought','loaned']
DIST_TOKENS = ['distribution','profit distribution','tax distribution','dividend','received a wire','received','interest payment','interest received']
REPAY_TOKENS = ['repay','repayment','principal payment','paid back','buyback']
CONV_TOKENS = ['convert','converted','conversion']
OUR_PARTIES = [r'Radical Investments', r'Mark Cuban Companies', r'Mark Cuban', r'\bMCC\b']
def party_is_us(text: str) -> bool:
return any(re.search(p, text or '', flags=re.I) for p in OUR_PARTIES) or bool(re.search(r'received (?:a )?wire', text or '', re.I))
def classify_confidence(e: Event) -> float:
c = 0.0
if e.date: c += 0.3
if e.type in ['investment','distribution','repayment','conversion']: c += 0.2
if e.amount is not None: c += 0.2
if e.note and party_is_us(e.note): c += 0.3
return round(min(c, 1.0), 2)
def validate_patch(p: Patch) -> Tuple[bool, float, List[str], List[str], Patch]:
warnings, errors = [], []
# Basic checks
if not p.company_id:
errors.append("company_id missing")
# Normalize events: drop non-our-party cash events
norm_events = []
for e in p.events_to_append:
if e.type in ['investment','distribution','repayment']:
if not party_is_us(e.note or ''):
warnings.append(f"Event on {e.date} filtered (not our cash party): {e.note}")
continue
conf = classify_confidence(e)
norm_events.append(e)
# Confidence heuristic: average over events/updates
scores = [classify_confidence(e) for e in norm_events] or [0.0]
confidence = round(sum(scores)/len(scores), 2)
normalized = Patch(
company_id=p.company_id,
dossier_overrides=p.dossier_overrides,
events_to_append=norm_events,
updates_to_append=p.updates_to_append,
documents_to_register=p.documents_to_register,
contacts_to_set=p.contacts_to_set
)
ok = len(errors) == 0
return ok, confidence, warnings, errors, normalized