tencent_nlp.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import json
  2. from loguru import logger
  3. from tencentcloud.common import credential
  4. from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
  5. TencentCloudSDKException,
  6. )
  7. from tencentcloud.common.profile.client_profile import ClientProfile
  8. from tencentcloud.common.profile.http_profile import HttpProfile
  9. from tencentcloud.nlp.v20190408 import models, nlp_client
  10. from app.core.config import settings
  11. def get_tencent_nlp_client() -> nlp_client.NlpClient:
  12. try:
  13. cred = credential.Credential(
  14. settings.TENCENT_SECRET_ID_V1, settings.TENCENT_SECRET_KEY_V1
  15. )
  16. http_profile = HttpProfile()
  17. http_profile.reqMethod = "GET"
  18. http_profile.endpoint = settings.TENCENT_NLP_ENDPOINT
  19. client_profile = ClientProfile()
  20. client_profile.httpProfile = http_profile
  21. client = nlp_client.NlpClient(cred, "ap-guangzhou", client_profile)
  22. return client
  23. except TencentCloudSDKException as e:
  24. logger.error(e)
  25. class TencentNLP:
  26. def __init__(self):
  27. cred = credential.Credential(
  28. settings.TENCENT_SECRET_ID_V1, settings.TENCENT_SECRET_KEY_V1
  29. )
  30. http_profile = HttpProfile()
  31. http_profile.reqMethod = "GET"
  32. http_profile.endpoint = settings.TENCENT_NLP_ENDPOINT
  33. client_profile = ClientProfile()
  34. client_profile.httpProfile = http_profile
  35. client = nlp_client.NlpClient(cred, "ap-guangzhou", client_profile)
  36. self.client = client
  37. async def get_lexical_analysis_result(self, text: str) -> tuple[list, list]:
  38. req = models.LexicalAnalysisRequest()
  39. params = {"Text": text}
  40. req.from_json_string(json.dumps(params))
  41. resp = self.client.LexicalAnalysis(req)
  42. return resp.PosTokens, resp.NerTokens
  43. async def get_auto_summarization_result(self, text: str) -> str:
  44. req = models.AutoSummarizationRequest()
  45. params = {"Text": text}
  46. req.from_json_string(json.dumps(params))
  47. resp = self.client.AutoSummarization(req)
  48. return resp.Summary
  49. async def get_text_similarity_result(
  50. self, src_text: str, target_text: list[str]
  51. ) -> list:
  52. req = models.TextSimilarityRequest()
  53. params = {"SrcText": src_text, "TargetText": target_text}
  54. req.from_json_string(json.dumps(params))
  55. resp = self.client.TextSimilarity(req)
  56. return resp.Similarity
  57. async def get_dependency(self, text: str) -> list:
  58. req = models.DependencyParsingRequest()
  59. params = {"Text": text}
  60. req.from_json_string(json.dumps(params))
  61. resp = self.client.DependencyParsing(req)
  62. return resp.DpTokens
  63. async def get_word_similarity(self, src_word: str, target: str) -> float:
  64. try:
  65. req = models.WordSimilarityRequest()
  66. params = {"SrcWord": src_word, "TargetWord": target}
  67. req.from_json_string(json.dumps(params))
  68. resp = self.client.WordSimilarity(req)
  69. except TencentCloudSDKException:
  70. return 0
  71. return resp.Similarity