python - 使用 Word2Vec 的文本相似度

标签 python pandas word2vec similarity

我想使用 Word2Vec 来检查文本的相似性。
我目前正在使用另一种逻辑:

from fuzzywuzzy import fuzz

def sim(name, dataset):
    matches = dataset.apply(lambda row: ((fuzz.ratio(row['Text'], name) ) = 0.5), axis=1)
   return 
(名字是我的专栏)。
为了应用此功能,我执行以下操作:
df['Sim']=df.apply(lambda row: sim(row['Text'], df), axis=1)
你能告诉我如何用 Word2Vec 替换 Fuzzy.ratio 以比较数据集中的文本吗?
数据集示例:
Text
Hello, this is Peter, what would you need me to help you with today? 
I need you
Good Morning, John here, are you calling regarding your cell phone bill? 
Hi, this this is John. What can I do for you?
...
第一个文本和最后一个文本非常相似,尽管它们用不同的词来表达相似的概念。
我想创建一个新列,用于为每一行放置相似的文本。
我希望你能帮助我。

最佳答案

TLDR; 跳到最后一节(第 4 部分)了解代码实现
1. 模糊 vs 词嵌入
与模糊匹配不同,模糊匹配基本上是 edit distancelevenshtein distance为了在字母表级别匹配字符串,word2vec(以及其他模型,例如 fasttext 和 GloVe)表示 n 维欧几里得空间中的每个单词。表示每个词的向量称为词向量或词嵌入。
这些词嵌入是 n-dimensional vector大量词汇的表示。可以将这些向量相加以创建句子嵌入的表示。具有相似语义的词的句子将具有相似的向量,因此它们的句子嵌入也会相似。阅读有关 word2vec 内部如何工作的更多信息 here .
enter image description here
假设我有一个包含 2 个词的句子。 Word2Vec 将这里的每个单词表示为某个欧几里得空间中的向量。总结起来,就像标准向量加法会导致同一空间中的另一个向量。这可能是使用单个词嵌入表示句子的不错选择。
注意:还有其他组合词嵌入的方法,例如加权和与 tf-idf 权重,或者直接使用句子嵌入和称为 Doc2Vec 的算法。阅读更多相关信息 here .
2.词向量/句子向量之间的相似性

“You shall know a word by the company it keeps”


与词(上下文)一起出现的词通常在语义/含义上相似。 word2vec 的伟大之处在于,具有相似上下文的单词的单词向量在欧几里得空间中彼此更接近。这使您可以进行聚类或简单的距离计算等操作。
enter image description here
找出 2 个词向量的相似程度的一个好方法是 cosine-similarity .阅读更多 here .
3. 预训练的 word2vec 模型(和其他)
word2vec 和此类模型的绝妙之处在于,在大多数情况下,您无需在数据上训练它们。 您可以使用预先训练好的词嵌入 它已经接受了大量数据的训练,并根据单词与句子中其他单词的共现对单词之间的上下文/语义相似性进行编码。
您可以使用 cosine_similarity 检查这些句子嵌入之间的相似性。
4. 示例代码实现
我使用已经在维基百科上训练过的手套模型(类似于 word2vec),其中每个单词都表示为 50 维向量。您可以从这里选择我使用的模型以外的其他模型 - https://github.com/RaRe-Technologies/gensim-data
from scipy import spatial
import gensim.downloader as api

model = api.load("glove-wiki-gigaword-50") #choose from multiple models https://github.com/RaRe-Technologies/gensim-data

s0 = 'Mark zuckerberg owns the facebook company'
s1 = 'Facebook company ceo is mark zuckerberg'
s2 = 'Microsoft is owned by Bill gates'
s3 = 'How to learn japanese'

def preprocess(s):
    return [i.lower() for i in s.split()]

def get_vector(s):
    return np.sum(np.array([model[i] for i in preprocess(s)]), axis=0)


print('s0 vs s1 ->',1 - spatial.distance.cosine(get_vector(s0), get_vector(s1)))
print('s0 vs s2 ->', 1 - spatial.distance.cosine(get_vector(s0), get_vector(s2)))
print('s0 vs s3 ->', 1 - spatial.distance.cosine(get_vector(s0), get_vector(s3)))
#Semantic similarity between sentence pairs
s0 vs s1 -> 0.965923011302948
s0 vs s2 -> 0.8659112453460693
s0 vs s3 -> 0.5877998471260071

关于python - 使用 Word2Vec 的文本相似度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65852710/

相关文章:

python - 使用 Wand 以预定义图案填充图像

python - Pandas 按列值选择行,奇怪的行为

python - Pandas 删除某些列中相对于其他列重复的行

nlp - Gensim word2vec中的most_similar和similar_by_vector之间的区别?

machine-learning - Word2Vec如何确保反义词在向量空间中相距较远

svm - 训练 SVM 分类器(词嵌入与句子嵌入)

python - 无法转换 Pandas 数据帧时间戳

python - 我可以无损恢复 json.dumps() 已转换为字符串的整数键吗?

pandas - 如何从ordereddict生成pandas数据框?

python - 在 Pandas 聚合函数中命名返回的列?