import math
import numpy as np
import pandas as pd
from elmoformanylangs import Embedder
from sklearn.metrics.pairwise import cosine_similarity

e = Embedder('../../model/zhs.model')

# 相似度目标文本
df = pd.read_excel(r'../../data-dev/医生分词结果词性表0511.xlsx', sheet_name='Sheet1')
sents = df['word_list'].fillna('').tolist()

elmo_vecs = e.sents2elmo(sents, output_layer=-1)

# print(elmo_vecs)

# 词性，词索引
df1 = pd.read_excel(r'../../data-dev/医生分词结果词性表0511.xlsx', sheet_name='Sheet1')

# 定义权重字典 动词 名词 形容词 副词 连词 未知词
weight_dict = {'v': 1.2, 'n': 1.2, 'a': 0.8, 'd': 0.8, 'c': 0.6, 'u': 0.6}

# 字符向量转词向量,并处理权重
word_vecs_plus = []
for i, char_vecs in enumerate(elmo_vecs):
    # 定位
    pos_list = eval(df1.loc[i, 'pos_list'])
    pos_index = eval(df1.loc[i, 'pos_index'])
    words = []
    for j, (start, end) in enumerate(pos_index):
        word_vec = sum(char_vecs[start:end]) / (end - start)  # average character vectors
        pos = pos_list[j]
        if pos in weight_dict:
            weight = weight_dict[pos]
            word_vec *= weight
        word = {'vector': word_vec}
        words.append(word)
    word_vecs_plus.append(words)

# print(word_vecs_plus)

# 格式转换
word_vecs_plus_fix = []
for n in word_vecs_plus:
    my_array = np.zeros((0, 1024))
    for m in n:
        vector = m['vector']
        my_array = np.vstack([my_array, vector])
    word_vecs_plus_fix.append(my_array)

# 计算每个句子与第3个句子的余弦相似度，得到一个列表，存储所有的余弦相似度
target_vec = word_vecs_plus_fix[1511]
sims = []
for vec in word_vecs_plus_fix:
    vec.mean(axis=0)
    if isinstance(vec, float) and math.isnan(vec):
        sims.append('')
    else:
        sims.append(np.dot(vec.mean(axis=0), target_vec.mean(axis=0)) / (
                np.linalg.norm(vec.mean(axis=0)) * np.linalg.norm(target_vec.mean(axis=0))))

# 将余弦相似度添加到原DataFrame中，并将结果保存为Excel文件
df['similarity'] = sims
df.to_excel('../../data-dev/余弦相似度结果-518-0511.xlsx', sheet_name='Sheet1', index=False)

target_vec2740 = word_vecs_plus_fix[3619]
sims = []
for vec in word_vecs_plus_fix:
    vec.mean(axis=0)
    if isinstance(vec, float) and math.isnan(vec):
        sims.append('')
    else:
        sims.append(np.dot(vec.mean(axis=0), target_vec2740.mean(axis=0)) / (
                np.linalg.norm(vec.mean(axis=0)) * np.linalg.norm(target_vec2740.mean(axis=0))))

# 将余弦相似度添加到原DataFrame中，并将结果保存为Excel文件
df['similarity'] = sims
df.to_excel('../../data-dev/余弦相似度结果-2740-0511.xlsx', sheet_name='Sheet1', index=False)

target_vec5088 = word_vecs_plus_fix[4420]
sims = []
for vec in word_vecs_plus_fix:
    vec.mean(axis=0)
    if isinstance(vec, float) and math.isnan(vec):
        sims.append('')
    else:
        sims.append(np.dot(vec.mean(axis=0), target_vec5088.mean(axis=0)) / (
                np.linalg.norm(vec.mean(axis=0)) * np.linalg.norm(target_vec5088.mean(axis=0))))

# 将余弦相似度添加到原DataFrame中，并将结果保存为Excel文件
df['similarity'] = sims
df.to_excel('../../data-dev/余弦相似度结果-5088-0511.xlsx', sheet_name='Sheet1', index=False)

target_vec7464 = word_vecs_plus_fix[6730]
sims = []
for vec in word_vecs_plus_fix:
    vec.mean(axis=0)
    if isinstance(vec, float) and math.isnan(vec):
        sims.append('')
    else:
        sims.append(np.dot(vec.mean(axis=0), target_vec7464.mean(axis=0)) / (
                np.linalg.norm(vec.mean(axis=0)) * np.linalg.norm(target_vec7464.mean(axis=0))))

# 将余弦相似度添加到原DataFrame中，并将结果保存为Excel文件
df['similarity'] = sims
df.to_excel('../../data-dev/余弦相似度结果-7464-0511.xlsx', sheet_name='Sheet1', index=False)

target_vec9070 = word_vecs_plus_fix[10431]
sims = []
for vec in word_vecs_plus_fix:
    vec.mean(axis=0)
    if isinstance(vec, float) and math.isnan(vec):
        sims.append('')
    else:
        sims.append(np.dot(vec.mean(axis=0), target_vec9070.mean(axis=0)) / (
                np.linalg.norm(vec.mean(axis=0)) * np.linalg.norm(target_vec9070.mean(axis=0))))

# 将余弦相似度添加到原DataFrame中，并将结果保存为Excel文件
df['similarity'] = sims
df.to_excel('../../data-dev/余弦相似度结果-9070-0511.xlsx', sheet_name='Sheet1', index=False)




