123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- # -*- coding: utf-8 -*-
- import codecs
- import os
- import time
- base_path = os.path.abspath(__file__)
- folder = os.path.dirname(base_path)
- data_path = os.path.join(folder, 'cilin_dict.txt')
- class SimCilin(object):
- def __init__(self):
- self.cilin_path = data_path
- self.sem_dict = self.load_semantic()
- def load_semantic(self):
- sem_dict = dict()
- for line in codecs.open(self.cilin_path, encoding='utf-8'):
- line = line.strip().split(' ')
- sem_type = line[0]
- words = line[1:]
- for word in words:
- if word not in sem_dict:
- sem_dict[word] = sem_type
- else:
- sem_dict[word] += ';' + sem_type
- for word, sem_type in sem_dict.items():
- sem_dict[word] = sem_type.split(';')
- return sem_dict
- def compute_word_sim(self, word1, word2):
- sems_word1 = self.sem_dict.get(word1, [])
- sems_word2 = self.sem_dict.get(word2, [])
- score_list = [self.compute_sem(sem_word1, sem_word2) for sem_word1 in sems_word1 for sem_word2 in sems_word2]
- if score_list:
- return max(score_list)
- else:
- return 0
- @staticmethod
- def compute_sem(sem1, sem2):
- sem1 = [sem1[0], sem1[1], sem1[2:4], sem1[4], sem1[5:7], sem1[-1]]
- sem2 = [sem2[0], sem2[1], sem2[2:4], sem2[4], sem2[5:7], sem2[-1]]
- score = 0
- for index in range(len(sem1)):
- if sem1[index] == sem2[index]:
- if index in [0, 1]:
- score += 3
- elif index == 2:
- score += 2
- elif index in [3, 4]:
- score += 1
- return score / 10
- if __name__ == '__main__':
- w1 = '歌手'
- w2 = '演员'
- ci_lin = SimCilin()
- start = time.perf_counter()
- v = 0.0
- for i in range(20000):
- v = ci_lin.compute_word_sim(w1, w2)
- end = time.perf_counter()
- print(end - start)
- print(v)
|