Create scorer.py
Browse files- core/scorer.py +48 -0
core/scorer.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, List
|
| 2 |
+
|
| 3 |
+
def _safe(v):
|
| 4 |
+
try:
|
| 5 |
+
return float(v) if v not in (None, "", "null") else 0.0
|
| 6 |
+
except Exception:
|
| 7 |
+
return 0.0
|
| 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 |
+
total_assets = _safe(bs.get("total_assets"))
|
| 15 |
+
total_liab = _safe(bs.get("total_liabilities"))
|
| 16 |
+
equity = _safe(bs.get("total_equity"))
|
| 17 |
+
sales = _safe(is_.get("sales"))
|
| 18 |
+
op_income = _safe(is_.get("operating_income"))
|
| 19 |
+
net_income = _safe(is_.get("net_income"))
|
| 20 |
+
ocf = _safe(cf.get("operating_cash_flow"))
|
| 21 |
+
|
| 22 |
+
metrics: List[Dict[str, Any]] = []
|
| 23 |
+
|
| 24 |
+
# 安全性: 自己資本比率
|
| 25 |
+
eq_ratio = (equity / total_assets * 100) if total_assets > 0 else 0
|
| 26 |
+
metrics.append({"metric": "自己資本比率", "value": eq_ratio, "score": min(max(eq_ratio, 0), 100)})
|
| 27 |
+
|
| 28 |
+
# 収益性: 営業利益率
|
| 29 |
+
op_margin = (op_income / sales * 100) if sales > 0 else 0
|
| 30 |
+
metrics.append({"metric": "営業利益率", "value": op_margin, "score": min(max(op_margin*4, 0), 100)})
|
| 31 |
+
|
| 32 |
+
# 収益性: 最終利益率
|
| 33 |
+
net_margin = (net_income / sales * 100) if sales > 0 else 0
|
| 34 |
+
metrics.append({"metric": "当期純利益率", "value": net_margin, "score": min(max(net_margin*4, 0), 100)})
|
| 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}
|