title_similarity.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. @author: luojunhui
  3. """
  4. import aiohttp
  5. empty_list = []
  6. def jcd_title_similarity(ori_title, search_title):
  7. """
  8. simple ways to calculate the similarity of titles
  9. :param ori_title:
  10. :param search_title:
  11. :return:
  12. """
  13. set1 = set(ori_title)
  14. set2 = set(search_title)
  15. intersection = len(set1 & set2)
  16. union = len(set1 | set2)
  17. return intersection / union
  18. async def nlp_title_similarity(ori_title, search_title_list):
  19. """
  20. nlp title similarity
  21. """
  22. headers = {"Content-Type": "application/json"}
  23. body = {
  24. "data": {
  25. "text_list_a": [ori_title],
  26. "text_list_b": search_title_list,
  27. },
  28. "function": "similarities_cross",
  29. "use_cache": "False"
  30. }
  31. url = 'http://61.48.133.26:6060/nlp'
  32. async with aiohttp.ClientSession() as session:
  33. async with session.post(url, headers=headers, json=body) as response:
  34. response_text = await response.text()
  35. if response_text:
  36. res = await response.json()
  37. score_list = res['score_list_list'][0]
  38. return score_list
  39. else:
  40. return empty_list