Update app/parsers/pdf_score.py
Browse files- 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 |
-
|
| 12 |
-
r"総合[\s\S]{0,10}?(\d{1,3})
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|