Python 计算关键字和文本文件中每个单词之间的度量值

Python 计算关键字和文本文件中每个单词之间的度量值,python,python-3.x,cosine-similarity,Python,Python 3.x,Cosine Similarity,我有两个.txt文件,一个包含200.000个单词,第二个包含100个关键字,每行一个。我想计算100个关键字和我的200.000个单词中的每个单词之间的余弦相似度,并为每个关键字显示得分最高的50个单词 下面是我所做的,请注意,Bertclient是我用来提取向量的: from sklearn.metrics.pairwise import cosine_similarity from bert_serving.client import BertClient bc = BertClient(

我有两个.txt文件,一个包含200.000个单词,第二个包含100个关键字,每行一个。我想计算100个关键字和我的200.000个单词中的每个单词之间的余弦相似度,并为每个关键字显示得分最高的50个单词

下面是我所做的,请注意,Bertclient是我用来提取向量的:

from sklearn.metrics.pairwise import cosine_similarity
from bert_serving.client import BertClient
bc = BertClient()

# Process words
with open("./words.txt", "r", encoding='utf8') as textfile:
    words = textfile.read().split()
    
with open("./100_keywords.txt", "r", encoding='utf8') as keyword_file:
    for keyword in keyword_file:
        vector_key = bc.encode([keyword])
        for w in words:
            vector_word = bc.encode([w])
            cosine_lib = cosine_similarity(vector_key,vector_word)
            print (cosine_lib)

它一直在运行,但不会停止。你知道我该怎么纠正吗?

我对伯特一无所知……但导入和运行有些可疑。我觉得你没有把它安装好。我尝试pip安装它并运行以下程序:

from sklearn.metrics.pairwise import cosine_similarity
from bert_serving.client import BertClient
bc = BertClient()
print ('done importing')
它从未结束。看一下bert的dox,看看是否需要做其他事情

在您的代码中,通常最好先进行所有读取,然后进行处理,因此先导入两个列表,分别检查一些值,如:

# check first five
print(words[:5])
此外,您需要寻找一种不同的方法来进行比较,而不是嵌套循环。您现在意识到,您每次都在为每个关键字转换每个单词,这不是必需的,而且可能非常慢。我建议您要么使用字典将单词与编码配对,要么列出单词的元组列表,如果您对此更满意的话,使用它进行编码

在你让伯特站起来跑步后,如果这不合理,请给我回复

-编辑-

下面是一段代码,其工作原理与您想要执行的类似。根据您的需要,您可以选择很多方法来保存结果等,但这应该可以让您从开始

from operator import itemgetter

# fake bert  ... just return something like length
def bert(word):
    return len(word)

# a fake compare function that will compare "bert" conversions
def bert_compare(x, y):
    return abs(x-y)

# Process words
with open("./word_data_file.txt", "r", encoding='utf8') as textfile:
    words = textfile.read().split()

# Process keywords
with open("./keywords.txt", "r", encoding='utf8') as keyword_file:
    keywords = keyword_file.read().split()

# encode the words and put result in dictionary
encoded_words = {}
for word in words:
    encoded_words[word] = bert(word)

encoded_keywords = {}
for word in keywords:
    encoded_keywords[word] = bert(word)

# let's use our bert conversions to find which keyword is most similar in
# length to the word

for word in encoded_words.keys():
    result = []   # make a new result set for each pass
    for kword in encoded_keywords.keys():
        similarity = bert_compare(encoded_words.get(word), encoded_keywords.get(kword))
        # stuff the answer into a tuple that can be sorted
        result.append((word, kword, similarity))
    result.sort(key=itemgetter(2))
    print(f'the keyword with the closest size to {result[0][0]} is {result[0][1]}')

您好,谢谢您的回复。我的bert正在运行,因为我在其他地方测试过它,我有我需要的单词向量。你需要在你的cmd上用你的Bert模型的路径启动服务器。。。你可以在这里找到关于它的必要文档,如果你喜欢,你可以阅读它:我还测试了cozine相似性函数,它与bert客户端完美配合。我现在需要的是如何对每个单词进行循环,以及如何对结果进行排序。结果还表明,我的文本文件将被一个包含200.000多个关键字的文件替换,这意味着我需要计算新文件中前100个关键字和200000个关键字之间的余弦相似性。我只想感谢你花时间写下你的答案,尽管你甚至不能重新创建它。这正是我所需要的,而且效果很好。我的代码中缺少这种组织,我正在努力。再次感谢你让我的周末更加美好