Corin1998 commited on
Commit
ae9ce4e
·
verified ·
1 Parent(s): 0ab25ae

Update core/scorer.py

Browse files
Files changed (1) hide show
  1. core/scorer.py +29 -42
core/scorer.py CHANGED
@@ -1,48 +1,35 @@
 
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}
 
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}