nlp_client.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import re
  2. from typing import Any
  3. import requests
  4. from app.config import settings
  5. class NLPClient:
  6. def extract(self, text: str) -> dict[str, Any]:
  7. if settings.use_remote_nlp_service:
  8. return self._extract_remote(text)
  9. return self._extract_local_rule_based(text)
  10. def _extract_remote(self, text: str) -> dict[str, Any]:
  11. payload = {"text": text}
  12. response = requests.post(settings.nlp_service_url, json=payload, timeout=60)
  13. response.raise_for_status()
  14. return response.json()
  15. def _extract_local_rule_based(self, text: str) -> dict[str, Any]:
  16. cause = "工资报酬争议" if "工资" in text else "劳动争议"
  17. entry_match = re.search(r"(入职|到岗)[::]?\s*([0-9]{4}[./-][0-9]{1,2}[./-][0-9]{1,2})", text)
  18. leave_match = re.search(r"(离职|解除)[::]?\s*([0-9]{4}[./-][0-9]{1,2}[./-][0-9]{1,2})", text)
  19. amount_match = re.search(r"([0-9]{3,8}(?:\.[0-9]{1,2})?)\s*元", text)
  20. law_refs = re.findall(r"《[^》]+》", text)
  21. return {
  22. "parties": {
  23. "employer_nature": "企业",
  24. "worker_position": "待识别",
  25. },
  26. "case_cause": {"type": cause},
  27. "facts": {
  28. "entry_date": entry_match.group(2) if entry_match else None,
  29. "leave_date": leave_match.group(2) if leave_match else None,
  30. "overtime": "加班" in text,
  31. "termination_reason": "待识别",
  32. },
  33. "claims": {
  34. "amount": float(amount_match.group(1)) if amount_match else None,
  35. "types": ["工资", "经济补偿"] if "补偿" in text else ["工资"],
  36. },
  37. "laws": list(set(law_refs)),
  38. }