Spaces:
Sleeping

Corin1998 commited on
Commit
b57b658
·
verified ·
1 Parent(s): 7c60df5

Update app/parsers/pdf_score.py

Browse files
Files changed (1) hide show
  1. app/parsers/pdf_score.py +32 -51
app/parsers/pdf_score.py CHANGED
@@ -3,65 +3,46 @@ import pdfplumber
3
  from typing import Dict, Any
4
  from .ocr import ocr_image_pages
5
 
6
-
7
  TEXT_THRESHOLD = 60
8
 
9
-
10
  PATTERNS = [
11
- # 例: "企業総合評点 43/100点"、"総合 43 / 100" 等を想定
12
- r"総合[\s\S]{0,10}?(\d{1,3})\s*/\s*100",
13
- r"企業総合評点[\s\S]{0,10}?(\d{1,3})",
14
  ]
15
 
16
-
17
  LABEL_KEYS = [
18
- "経営者能力", "成長性", "財務内容", "資金調達力", "企業規模",
19
- "経営方針", "信用度", "世評", "取引実績", "競争力", "技術力"
20
  ]
21
 
22
-
23
-
24
-
25
  def _extract_from_text(text: str) -> Dict[str, Any]:
26
- score = None
27
- for pat in PATTERNS:
28
- m = re.search(pat, text)
29
- if m:
30
- try:
31
- s = int(m.group(1))
32
- if 0 <= s <= 100:
33
- score = float(s)
34
- break
35
- except Exception:
36
- pass
37
-
38
-
39
- labels = {}
40
- # ラベルの有無をキーワード検出(印の形は多様なのでMVPでは存在判定のみに)
41
- for key in LABEL_KEYS:
42
- if key in text:
43
- labels[key] = {"present": True}
44
-
45
-
46
- return {"total_score": score, "labels": labels}
47
-
48
-
49
-
50
 
51
  def extract_pdf_score(pdf_bytes: bytes) -> Dict[str, Any]:
52
- """PDFから企業総合評点(0-100)を推定し、主要ラベルの存在を返す"""
53
- texts = []
54
- with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
55
- for p in pdf.pages:
56
- t = p.extract_text() or ""
57
- texts.append(t)
58
- joined = "\n".join(texts)
59
-
60
-
61
- # 十分なテキストがなければOCR
62
- if len(joined.strip()) < TEXT_THRESHOLD:
63
- ocrd = ocr_image_pages(pdf_bytes)
64
- joined = "\n".join([x.get("text", "") for x in ocrd])
65
-
66
-
67
- return _extract_from_text(joined)
 
3
  from typing import Dict, Any
4
  from .ocr import ocr_image_pages
5
 
 
6
  TEXT_THRESHOLD = 60
7
 
 
8
  PATTERNS = [
9
+ r"総合[\s\S]{0,10}?(\d{1,3})\s*/\s*100",
10
+ r"企業総合評点[\s\S]{0,10}?(\d{1,3})",
 
11
  ]
12
 
 
13
  LABEL_KEYS = [
14
+ "経営者能力", "成長性", "財務内容", "資金調達力", "企業規模",
15
+ "経営方針", "信用度", "世評", "取引実績", "競争力", "技術力"
16
  ]
17
 
 
 
 
18
  def _extract_from_text(text: str) -> Dict[str, Any]:
19
+ score = None
20
+ for pat in PATTERNS:
21
+ m = re.search(pat, text)
22
+ if m:
23
+ try:
24
+ s = int(m.group(1))
25
+ if 0 <= s <= 100:
26
+ score = float(s)
27
+ break
28
+ except Exception:
29
+ pass
30
+
31
+ labels = {}
32
+ for key in LABEL_KEYS:
33
+ if key in text:
34
+ labels[key] = {"present": True}
35
+
36
+ return {"total_score": score, "labels": labels}
 
 
 
 
 
 
37
 
38
  def extract_pdf_score(pdf_bytes: bytes) -> Dict[str, Any]:
39
+ texts = []
40
+ with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
41
+ for p in pdf.pages:
42
+ t = p.extract_text() or ""
43
+ texts.append(t)
44
+ joined = "\n".join(texts)
45
+ if len(joined.strip()) < TEXT_THRESHOLD:
46
+ ocrd = ocr_image_pages(pdf_bytes)
47
+ joined = "\n".join([x.get("text", "") for x in ocrd])
48
+ return _extract_from_text(joined)