Update core/scorer.py
Browse files- core/scorer.py +29 -42
core/scorer.py
CHANGED
|
@@ -1,48 +1,35 @@
|
|
|
|
|
| 1 |
from typing import Dict, Any, List
|
| 2 |
|
| 3 |
-
def
|
| 4 |
try:
|
| 5 |
-
return float(
|
| 6 |
except Exception:
|
| 7 |
-
return
|
| 8 |
|
| 9 |
def score_company(fin: Dict[str, Any]) -> Dict[str, Any]:
|
| 10 |
-
bs = fin.get("balance_sheet") or {}
|
| 11 |
-
is_ = fin.get("income_statement") or {}
|
| 12 |
-
cf = fin.get("cash_flows") or {}
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# キャッシュ: OCF/売上
|
| 37 |
-
ocf_rate = (ocf / sales * 100) if sales > 0 else 0
|
| 38 |
-
metrics.append({"metric": "営業CF率", "value": ocf_rate, "score": min(max(ocf_rate*3, 0), 100)})
|
| 39 |
-
|
| 40 |
-
# レバレッジ: 負債比率
|
| 41 |
-
debt_ratio = (total_liab / total_assets * 100) if total_assets > 0 else 0
|
| 42 |
-
debt_score = max(0, 100 - debt_ratio) # 低いほど良い
|
| 43 |
-
metrics.append({"metric": "負債比率(低いほど◎)", "value": debt_ratio, "score": min(debt_score, 100)})
|
| 44 |
-
|
| 45 |
-
total = round(sum(m["score"] for m in metrics) / len(metrics), 1)
|
| 46 |
-
grade = "S" if total >= 90 else "A" if total >= 80 else "B" if total >= 70 else "C" if total >= 60 else "D"
|
| 47 |
-
|
| 48 |
-
return {"total_score": total, "grade": grade, "details": metrics}
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
from typing import Dict, Any, List
|
| 3 |
|
| 4 |
+
def _ratio(num, den):
|
| 5 |
try:
|
| 6 |
+
return float(num) / float(den) if num not in (None,"") and den not in (None,"") and float(den)!=0 else None
|
| 7 |
except Exception:
|
| 8 |
+
return None
|
| 9 |
|
| 10 |
def score_company(fin: Dict[str, Any]) -> Dict[str, Any]:
|
| 11 |
+
bs = fin.get("balance_sheet", {}) or {}
|
| 12 |
+
is_ = fin.get("income_statement", {}) or {}
|
| 13 |
+
cf = fin.get("cash_flows", {}) or {}
|
| 14 |
+
|
| 15 |
+
gp_margin = _ratio(is_.get("gross_profit"), is_.get("sales"))
|
| 16 |
+
op_margin = _ratio(is_.get("operating_income"), is_.get("sales"))
|
| 17 |
+
equity_ratio= _ratio(bs.get("total_equity"), bs.get("total_assets"))
|
| 18 |
+
cur_ratio = _ratio(bs.get("current_assets"), bs.get("current_liabilities"))
|
| 19 |
+
ocf_sales = _ratio(cf.get("operating_cash_flow"), is_.get("sales"))
|
| 20 |
+
|
| 21 |
+
def s(x, w=1.0):
|
| 22 |
+
if x is None: return 50*w
|
| 23 |
+
v = max(0, min(1, x if x<=1 else x/2))
|
| 24 |
+
return (v*100)*w
|
| 25 |
+
|
| 26 |
+
details: List[Dict[str,Any]] = [
|
| 27 |
+
{"metric":"売上総利益率", "score": round(s(gp_margin),1)},
|
| 28 |
+
{"metric":"営業利益率", "score": round(s(op_margin),1)},
|
| 29 |
+
{"metric":"自己資本比率", "score": round(s(equity_ratio),1)},
|
| 30 |
+
{"metric":"流動比率(概算)","score": round(s(cur_ratio/2 if cur_ratio else None),1)},
|
| 31 |
+
{"metric":"営業CF/売上", "score": round(s(ocf_sales),1)},
|
| 32 |
+
]
|
| 33 |
+
total = round(sum(d["score"] for d in details)/len(details),1)
|
| 34 |
+
grade = "S" if total>=85 else "A" if total>=75 else "B" if total>=65 else "C" if total>=50 else "D"
|
| 35 |
+
return {"total_score": total, "grade": grade, "details": details}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|