meeting.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from typing import Dict, List, Tuple
  2. from httpx import AsyncClient
  3. from loguru import logger
  4. from app.services.duckling import Duckling
  5. from app.services.tencent_nlp import TencentNLP
  6. class MeetingInfoCatcher:
  7. def __init__(self, nlp_service: TencentNLP, duckling: Duckling):
  8. super(MeetingInfoCatcher, self).__init__()
  9. self.nlp_service = nlp_service
  10. self.duckling = duckling
  11. async def extract_time(self, sentence: str) -> Tuple[str, str, int]:
  12. start_time, end_time, duration = '', '', -1
  13. parsed = await self.duckling.parse(sentence)
  14. for dim in parsed:
  15. if dim['dim'] == 'time':
  16. start_time = dim['value']['from']['value']
  17. end_time = dim['value']['to']['value']
  18. if dim['dim'] == 'duration':
  19. duration = dim['value']['normalized']['value']
  20. return start_time, end_time, duration
  21. async def extract_room_size(self, sentence: str) -> str:
  22. dp_tokens = await self.nlp_service.get_dependency(sentence)
  23. size = ''
  24. for token in dp_tokens:
  25. if await self.nlp_service.get_word_similarity(token.Word, '会议室') > 0.8:
  26. index = token.Id
  27. for item in dp_tokens:
  28. if item.HeadId == index:
  29. if await self.nlp_service.get_word_similarity(item.Word, '小') > 0.9:
  30. size = 'small'
  31. if await self.nlp_service.get_word_similarity(item.Word, '中') > 0.9:
  32. size = 'medium'
  33. if await self.nlp_service.get_word_similarity(item.Word, '大') > 0.9:
  34. size = 'large'
  35. break
  36. return size
  37. async def extract_topic(self, sentence: str) -> str:
  38. summarization = await self.nlp_service.get_auto_summarization_result(sentence)
  39. return summarization
  40. async def extract_name(self, sentence: str) -> List[str]:
  41. _, ner_tokens = await self.nlp_service.get_lexical_analysis_result(sentence)
  42. name_list = []
  43. if ner_tokens:
  44. for token in ner_tokens:
  45. if token.Type == 'PER':
  46. name_list.append(token.Word)
  47. return name_list
  48. async def run(self, sentence: str) -> Tuple:
  49. similarity = await self.nlp_service.get_text_similarity_result('我要开会', [sentence])
  50. if similarity[-1].Score < 0.5:
  51. return '', '', -1, '', '', []
  52. else:
  53. start_time, end_time, interval = await self.extract_time(sentence)
  54. topic = await self.extract_topic(sentence)
  55. name_list = await self.extract_name(sentence)
  56. room_size = await self.extract_room_size(sentence)
  57. return start_time, end_time, interval, room_size, topic, name_list
  58. @logger.catch()
  59. async def get_caught_result(sentence: str) -> Tuple:
  60. async with AsyncClient() as client:
  61. duckling = Duckling(client)
  62. service = TencentNLP()
  63. catcher = MeetingInfoCatcher(service, duckling)
  64. return await catcher.run(sentence)