analyse2.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import re
  4. import pickle
  5. import math
  6. from multiprocessing import Manager, Process
  7. # 分词结果
  8. CUT_WORD_RESULT = "./data/分词结果_bak.txt"
  9. # 分词缓存
  10. CUT_WORD_CACHE = "./data/pkl/word_root_cache.pkl"
  11. # 分析保存结果
  12. ANALYSE_OUTPUT_FILE = "./data/category/%s.txt"
  13. # 正则表达式中的特殊符号
  14. SPECIAL_SIMBOL = [".", "?", "^", "$", "*", "+", "\\", "[", "]", "|", "{", "}", "(", ")"]
  15. def merge_word_root(word_root_a, word_root_b):
  16. """
  17. 合并词根
  18. """
  19. return list(set(word_root_a).union(set(word_root_b)))
  20. def gen_word_vector(word_a, word_b, word_root_union):
  21. """
  22. 生成词向量
  23. """
  24. a_word_vector, b_word_vector = [], []
  25. for word in word_root_union:
  26. if word in SPECIAL_SIMBOL :
  27. word = "\\" + word
  28. a_word_vector.append(len(re.findall(word, word_a)))
  29. b_word_vector.append(len(re.findall(word, word_b)))
  30. return a_word_vector, b_word_vector
  31. def vector_multi(a_vector, b_vector):
  32. """
  33. 向量相乘求和
  34. """
  35. return sum(map(lambda a_b: a_b[0]*a_b[1], zip(a_vector, b_vector)))
  36. def vector_square_sum(word_vector):
  37. """
  38. 向量平方求和
  39. """
  40. sum = 0
  41. for i in word_vector:
  42. sum = sum + i * i
  43. return sum
  44. def vector_cos(v_multi, a_v_ss, b_v_ss):
  45. """
  46. 计算余弦值
  47. """
  48. return v_multi / (math.sqrt(a_v_ss) * math.sqrt(b_v_ss))
  49. def cal_cos(a_word, b_word, word_dict):
  50. """
  51. 计算两个长尾关键词的余弦值
  52. """
  53. a_word_root = word_dict[a_word]
  54. b_word_root = word_dict[b_word]
  55. # 合并词根,用于生成词向量
  56. union_word_root = merge_word_root(a_word_root, b_word_root)
  57. # 生成词向量
  58. a_vector, b_vector = gen_word_vector(a_word, b_word, union_word_root)
  59. # 词向量相乘求和
  60. ab_vector_multi = vector_multi(a_vector, b_vector)
  61. # 向量平方求和
  62. a_vector_squar_sum = vector_square_sum(a_vector)
  63. b_vector_squar_sum = vector_square_sum(b_vector)
  64. cos_val = vector_cos(ab_vector_multi, a_vector_squar_sum, b_vector_squar_sum)
  65. return cos_val
  66. def process(global_word_root, global_del_cache, a_key, keys):
  67. container = []
  68. total_num = len(keys)
  69. for j, b_key in enumerate(keys):
  70. if j % 100000 == 0 :
  71. print("处理进度:%d / %d" % (j, total_num))
  72. cos_val = cal_cos(a_key, b_key, global_word_root)
  73. if cos_val > 0.8 and b_key not in global_del_cache :
  74. print("%s 与 %s 的余弦值:%f " % (a_key, b_key, cos_val))
  75. container.append(b_key)
  76. global_del_cache.append(b_key)
  77. with open(ANALYSE_OUTPUT_FILE % a_key, "w", encoding="UTF-8") as f:
  78. f.write(a_key)
  79. f.write("\n")
  80. for b_key in container:
  81. f.write(b_key)
  82. f.write("\n")
  83. def load_word_root_cache():
  84. """
  85. 加载分词缓存
  86. """
  87. word_root_cache = {}
  88. if os.path.exists(CUT_WORD_CACHE):
  89. print("存在缓存,开始加载")
  90. with open(CUT_WORD_CACHE, "rb") as f:
  91. word_root_cache = pickle.load(f)
  92. return word_root_cache
  93. print('不存在缓存,开始构建分词字典')
  94. with open(CUT_WORD_RESULT, "r", encoding="UTF-8") as f:
  95. lines = f.readlines()
  96. for line in lines:
  97. index = line.index(",")
  98. word_root_cache[line[:index]] = line[index+1:]
  99. print("构建完成,保存到本地")
  100. with open(CUT_WORD_CACHE, "wb") as f:
  101. pickle.dump(word_root_cache, f)
  102. return word_root_cache
  103. def main():
  104. word_root_cache = load_word_root_cache();
  105. keys = [key for key in word_root_cache.keys()]
  106. manager = Manager()
  107. global_word_root = manager.dict(word_root_cache)
  108. global_del_cache = manager.list()
  109. p = Process(target=process, args=(global_word_root, global_del_cache, keys[0], keys[1:]))
  110. p.join()
  111. if __name__ == "__main__":
  112. main()