# https://blog.csdn.net/longmaohu/article/details/121782916?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168067822516800180651063%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168067822516800180651063&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-121782916-null-null.142^v81^insert_down1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=elmo%E4%BB%A3%E7%A0%81&spm=1018.2226.3001.4187
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# 加载ELMo模型 英文数据
elmo = hub.load("https://tfhub.dev/google/elmo/3")

# 预处理后的文本
text1_processed = ['I','love','you']
text2_processed = ['sorry','I','do','not','love','you']

# 生成词向量
embeddings1 = elmo.signatures["default"](tf.constant(text1_processed))["elmo"]
embeddings2 = elmo.signatures["default"](tf.constant(text2_processed))["elmo"]

# 计算文档向量
doc_vector1 = np.mean(embeddings1, axis=0)
doc_vector2 = np.mean(embeddings2, axis=0)

# 计算余弦相似度
similarity = cosine_similarity(doc_vector1.reshape(1, -1), doc_vector2.reshape(1, -1))

print("相似度：", similarity)
