contacts.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Phase 6 migration from interface/getAttributes.py.
  2. This module hosts the ``getProjectContacts`` function migrated verbatim from
  3. ``BiddingKG.dl.interface.getAttributes`` as part of the Phase 6 assembly
  4. re-organization. The function is responsible for extracting the
  5. "项目联系人" (project contacts) attribute from a list of entities and
  6. sentences.
  7. """
  8. from __future__ import absolute_import
  9. import re
  10. __all__ = ["getProjectContacts"]
  11. def getProjectContacts(list_entity, list_sentence):
  12. # project_contacts "项目联系人"提取
  13. temp_person_entitys = [ent for ent in list_entity if ent.entity_type=='person' and ent.label in [1,2,3]]
  14. temp_person_entitys = sorted(temp_person_entitys,key=lambda x:(x.sentence_index,x.wordOffset_begin))
  15. project_contacts_patterns = ['项目.?联系[人方]','项目.?联系.?方式', '项目.?负责人']
  16. project_contacts_patterns_prob = [0.9, 0.85, 0.8]
  17. project_contacts_patterns_res = []
  18. for ent in temp_person_entitys:
  19. sent_idx = ent.sentence_index
  20. word_begin = ent.wordOffset_begin
  21. # word_end = ent.wordOffset_end
  22. in_att = ent.in_attachment
  23. if word_begin >= 5: # > len('项目联系人')
  24. left_text = list_sentence[sent_idx].sentence_text[max(0, word_begin - 15):word_begin]
  25. # print('left_text', left_text)
  26. for pattern, prob in zip(project_contacts_patterns, project_contacts_patterns_prob):
  27. if re.search(pattern, left_text):
  28. project_contacts_patterns_res.append([ent, sent_idx, word_begin, prob if not in_att else prob / 2])
  29. project_contacts_patterns_res = sorted(project_contacts_patterns_res, key=lambda x: (x[3], -x[1], -x[2]),
  30. reverse=True)
  31. # print('project_contacts_patterns_res', project_contacts_patterns_res)
  32. project_contacts_list = []
  33. phone_set = set()
  34. have_in_text = False
  35. if project_contacts_patterns_res:
  36. for item in project_contacts_patterns_res:
  37. in_att = item[0].in_attachment
  38. contacts_person = item[0].entity_text
  39. contacts_phone = item[0].person_phone[0].entity_text if item[0].person_phone else ""
  40. if contacts_phone:
  41. if not in_att:
  42. have_in_text = True
  43. if in_att and have_in_text: # 正文已提取,则排除附件的
  44. break
  45. if contacts_phone not in phone_set:
  46. phone_set.add(contacts_phone)
  47. project_contacts_list.append([contacts_person,contacts_phone])
  48. return {'project_contacts':project_contacts_list}