|
@@ -6,6 +6,7 @@ from operator import itemgetter
|
|
|
|
|
|
import jieba
|
|
|
import numpy as np
|
|
|
+
|
|
|
from .words_sim import SimCilin
|
|
|
|
|
|
data = pkgutil.get_data(__package__, 'cilin_words.txt')
|
|
@@ -53,6 +54,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
|
|
|
|
|
|
|
|
@@ -64,6 +69,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:
|
|
@@ -93,15 +99,3 @@ def merge(word_list):
|
|
|
for w in word_list:
|
|
|
s += w.split('/')[0]
|
|
|
return s
|
|
|
-
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- str1 = '我喜欢吃苹果'
|
|
|
- str2 = '他喜欢肯红薯'
|
|
|
- str1_seg = jieba.cut(str1)
|
|
|
- str2_seg = jieba.cut(str2)
|
|
|
- str1_new = [x for x in str1_seg]
|
|
|
- str2_new = [x for x in str2_seg]
|
|
|
- str_l = ['我喜欢吃梨', '你喜欢吃苹果', '他喜欢吃橙子']
|
|
|
- print(get_similarity(str1_new, str2_new))
|
|
|
- print(most_similar_items(str1, str_l, 5))
|