| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- """Phase 6 migration from interface/getAttributes.py.
- This module hosts the ``getProjectContacts`` function migrated verbatim from
- ``BiddingKG.dl.interface.getAttributes`` as part of the Phase 6 assembly
- re-organization. The function is responsible for extracting the
- "项目联系人" (project contacts) attribute from a list of entities and
- sentences.
- """
- from __future__ import absolute_import
- import re
- __all__ = ["getProjectContacts"]
- def getProjectContacts(list_entity, list_sentence):
- # project_contacts "项目联系人"提取
- temp_person_entitys = [ent for ent in list_entity if ent.entity_type=='person' and ent.label in [1,2,3]]
- temp_person_entitys = sorted(temp_person_entitys,key=lambda x:(x.sentence_index,x.wordOffset_begin))
- project_contacts_patterns = ['项目.?联系[人方]','项目.?联系.?方式', '项目.?负责人']
- project_contacts_patterns_prob = [0.9, 0.85, 0.8]
- project_contacts_patterns_res = []
- for ent in temp_person_entitys:
- sent_idx = ent.sentence_index
- word_begin = ent.wordOffset_begin
- # word_end = ent.wordOffset_end
- in_att = ent.in_attachment
- if word_begin >= 5: # > len('项目联系人')
- left_text = list_sentence[sent_idx].sentence_text[max(0, word_begin - 15):word_begin]
- # print('left_text', left_text)
- for pattern, prob in zip(project_contacts_patterns, project_contacts_patterns_prob):
- if re.search(pattern, left_text):
- project_contacts_patterns_res.append([ent, sent_idx, word_begin, prob if not in_att else prob / 2])
- project_contacts_patterns_res = sorted(project_contacts_patterns_res, key=lambda x: (x[3], -x[1], -x[2]),
- reverse=True)
- # print('project_contacts_patterns_res', project_contacts_patterns_res)
- project_contacts_list = []
- phone_set = set()
- have_in_text = False
- if project_contacts_patterns_res:
- for item in project_contacts_patterns_res:
- in_att = item[0].in_attachment
- contacts_person = item[0].entity_text
- contacts_phone = item[0].person_phone[0].entity_text if item[0].person_phone else ""
- if contacts_phone:
- if not in_att:
- have_in_text = True
- if in_att and have_in_text: # 正文已提取,则排除附件的
- break
- if contacts_phone not in phone_set:
- phone_set.add(contacts_phone)
- project_contacts_list.append([contacts_person,contacts_phone])
- return {'project_contacts':project_contacts_list}
|