meeting.py 3.4 KB

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