Переглянути джерело

根据词林的字符串相似度匹配函数

jxing 5 роки тому
батько
коміт
f8cb92f424
2 змінених файлів з 33 додано та 22 видалено
  1. 7 2
      src/str_similar/text_sim.py
  2. 26 20
      src/str_similar/words_sim.py

+ 7 - 2
src/str_similar/text_sim.py

@@ -56,6 +56,10 @@ def get_similarity(s1, s2):
             sim_list.sort()
             all_sim_2.append(sim_list[-1])
 
+    if not all_sim_1:
+        all_sim_1 = [0]
+    if not all_sim_2:
+        all_sim_2 = [0]
     return (np.mean(all_sim_1) + np.mean(all_sim_2)) / 2
 
 
@@ -67,6 +71,7 @@ def most_similar_items(src_s, sentences, n=3):
     :param n: return number
     :return:
     """
+    src_s = [x for x in jieba.cut(src_s)]
     sentences = segmentation(sentences)
     temp = list()
     for item in sentences:
@@ -105,6 +110,6 @@ if __name__ == '__main__':
     str2_seg = jieba.cut(str2)
     str1_new = [x for x in str1_seg]
     str2_new = [x for x in str2_seg]
-    str_l = ['我喜欢吃梨', '你喜欢吃苹果', '他喜欢吃橙子']
+    str_l = ['我喜欢吃梨', '你喜欢吃苹果', '他喜欢吃橙子', 'ffff']
     print(get_similarity(str1_new, str2_new))
-    print(most_similar_items(str1, str_l, 2))
+    print(most_similar_items(str1, str_l, 5))

+ 26 - 20
src/str_similar/words_sim.py

@@ -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)