words_sim.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. import codecs
  3. import os
  4. import time
  5. base_path = os.path.abspath(__file__)
  6. folder = os.path.dirname(base_path)
  7. data_path = os.path.join(folder, 'cilin_dict.txt')
  8. class SimCilin(object):
  9. def __init__(self):
  10. self.cilin_path = data_path
  11. self.sem_dict = self.load_semantic()
  12. def load_semantic(self):
  13. sem_dict = dict()
  14. for line in codecs.open(self.cilin_path, encoding='utf-8'):
  15. line = line.strip().split(' ')
  16. sem_type = line[0]
  17. words = line[1:]
  18. for word in words:
  19. if word not in sem_dict:
  20. sem_dict[word] = sem_type
  21. else:
  22. sem_dict[word] += ';' + sem_type
  23. for word, sem_type in sem_dict.items():
  24. sem_dict[word] = sem_type.split(';')
  25. return sem_dict
  26. def compute_word_sim(self, word1, word2):
  27. sems_word1 = self.sem_dict.get(word1, [])
  28. sems_word2 = self.sem_dict.get(word2, [])
  29. score_list = [self.compute_sem(sem_word1, sem_word2) for sem_word1 in sems_word1 for sem_word2 in sems_word2]
  30. if score_list:
  31. return max(score_list)
  32. else:
  33. return 0
  34. @staticmethod
  35. def compute_sem(sem1, sem2):
  36. sem1 = [sem1[0], sem1[1], sem1[2:4], sem1[4], sem1[5:7], sem1[-1]]
  37. sem2 = [sem2[0], sem2[1], sem2[2:4], sem2[4], sem2[5:7], sem2[-1]]
  38. score = 0
  39. for index in range(len(sem1)):
  40. if sem1[index] == sem2[index]:
  41. if index in [0, 1]:
  42. score += 3
  43. elif index == 2:
  44. score += 2
  45. elif index in [3, 4]:
  46. score += 1
  47. return score / 10
  48. if __name__ == '__main__':
  49. w1 = '歌手'
  50. w2 = '演员'
  51. ci_lin = SimCilin()
  52. start = time.perf_counter()
  53. v = 0.0
  54. for i in range(20000):
  55. v = ci_lin.compute_word_sim(w1, w2)
  56. end = time.perf_counter()
  57. print(end - start)
  58. print(v)