tencent_nlp.py 3.1 KB

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