|
@@ -1,23 +1,29 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
-import codecs
|
|
|
-import os
|
|
|
-import time
|
|
|
+# import codecs
|
|
|
+# import os
|
|
|
+# import time
|
|
|
+#
|
|
|
+# base_path = os.path.abspath(__file__)
|
|
|
+# folder = os.path.dirname(base_path)
|
|
|
+import pkgutil
|
|
|
|
|
|
-base_path = os.path.abspath(__file__)
|
|
|
-folder = os.path.dirname(base_path)
|
|
|
-data_path = os.path.join(folder, 'cilin_dict.txt')
|
|
|
+# cilin_data = pkgutil.get_data(__package__, 'cilin_dict.txt')
|
|
|
+cilin_data = pkgutil.get_data('relations.src.str_similar', 'cilin_dict.txt')
|
|
|
+# data_path = os.path.join(folder, 'cilin_dict.txt')
|
|
|
|
|
|
|
|
|
class SimCilin(object):
|
|
|
|
|
|
def __init__(self):
|
|
|
- self.cilin_path = data_path
|
|
|
+ self.cilin_data = cilin_data
|
|
|
self.sem_dict = self.load_semantic()
|
|
|
|
|
|
- def load_semantic(self):
|
|
|
+ @staticmethod
|
|
|
+ def load_semantic():
|
|
|
sem_dict = dict()
|
|
|
- for line in codecs.open(self.cilin_path, encoding='utf-8'):
|
|
|
+ lines = cilin_data.decode().split('\n')
|
|
|
+ for line in lines:
|
|
|
line = line.strip().split(' ')
|
|
|
sem_type = line[0]
|
|
|
words = line[1:]
|
|
@@ -56,14 +62,14 @@ class SimCilin(object):
|
|
|
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)
|
|
|
+# 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)
|