酒店评论属性与关键词相似度分析

本文使用Word2Vec模型分析酒店评论中的属性和关键词相似度,并探讨如何提高词向量模型的准确性。

定义属性和关键词

attributes = ['员工素质', '设施服务', '清洁程度', '舒适度', '性价比', '位置']
# attributes = [ '性价比']
total_keywords_list = ['地理位置', 'location', '房间', '前台', '隔音', 'staff', '舒服', '榻榻米', '房間', '酒吧', '设施', '服务', '地方', '不错', 'hotel', '楼顶', '成都', 'experience', '氛围', '干净', '工作人员', 'night', 'hostel', 'guest', 'bathroom']

加载预训练的中文词向量模型

# model = gensim.models.Word2Vec.load("path_to_pretrained_model")

定义属性关键词的代替词

replace_words = {
    "员工素质": ['工作人员', '员工', '人员','素质',"staff"],
    "设施服务": ["设施","设备", "服务",'service'],
    "清洁程度": ["卫生","清洁","clean"],
    "舒适度": ["舒适", "舒服","comfort"],
    "性价比": ["价值", "性价比",'value'],
    "位置": [ "地理位置",'location']
}

计算属性关键词和代替词的词向量

attribute_vectors =[]
for attr in attributes:
    if attr in model.wv.key_to_index:
       attribute_vectors.append(model.wv[attr])
    else:##属性在分词后的文本里面没有,使用替代词
        attr_words = replace_words.get(attr, [])  #属性的替代词
        attr_vecs = [] #替代词的向量集合,便于平均
        for w in attr_words: #遍历属性和其同义词
            if w in model.wv.key_to_index: #有
                attr_vecs.append(model.wv[w]) #获得替代词向量
        if attr_vecs: #非空
            attr_vec = np.mean(attr_vecs, axis=0) #求平均词向量
            model.wv.add_vector(attr, attr_vec)
            attribute_vectors.append(attr_vec)
        else:
            attribute_vectors.append(None)
# print(attribute_vectors)            

使用model.wv.similarity()函数计算属性和关键词之间的相似度

attribute_keyword={}
for attribute in attributes: ##找出关键词与属性相似度为0.8以上的词,字典输出
    att_key=[]
    for keyword in total_keywords_list:
        try:
            similarity = model.wv.similarity(attribute, keyword)
            if similarity>0.9:
                att_key.append(keyword)
        except:
            pass  
    attribute_keyword[attribute]=att_key     
   
print("一级属性对于的二级影响因素:",attribute_keyword)

为什么结果model.wv.similarity('舒适度','舒适')才0.5,而model.wv.similarity('员工素质','房间')居然0.9609442,这不符合常理啊!为什么怎么办呢,怎么样获得准确的词向量模型,写出代码内容:

可能是因为词向量模型中没有包含足够的关于'舒适度'和'舒适'之间的语义相关性的信息。这可以通过增加训练数据或使用更复杂的模型来解决。

要获得准确的词向量模型,可以尝试以下步骤:

  1. 增加训练数据。使用更多的文本数据训练模型可以提高模型的准确性和覆盖范围。

  2. 优化模型参数。调整模型参数,例如vector_size、window、min_count等,可以改善模型的性能。

  3. 使用更复杂的模型。例如,使用BERT、ELMo或GPT等预训练的深度学习模型可以获得更准确的词向量表示。

以下是一些示例代码,演示如何使用更复杂的模型来获得准确的词向量。

使用BERT模型:

from transformers import BertModel, BertTokenizer

# 加载预训练的BERT模型和分词器
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
model = BertModel.from_pretrained('bert-base-chinese')

# 将文本转换为BERT输入格式
input_ids = torch.tensor(tokenizer.encode("舒适度", add_special_tokens=True)).unsqueeze(0)  # Batch size 1
outputs = model(input_ids)
word_embedding = outputs.last_hidden_state[0][1:-1] # 取出“舒适度”对应的词向量

使用ELMo模型:

import tensorflow as tf
import tensorflow_hub as hub

# 加载预训练的ELMo模型
elmo = hub.load("https://tfhub.dev/google/elmo/3")

# 将文本转换为ELMo输入格式
sentences = ["舒适度"]
embeddings = elmo(sentences, signature="default", as_dict=True)["elmo"]

# 取出“舒适度”对应的词向量
word_embedding = embeddings[0][1]

使用GPT模型:

import torch
import transformers

# 加载预训练的GPT模型和分词器
tokenizer = transformers.GPT2Tokenizer.from_pretrained('gpt2')
model = transformers.GPT2Model.from_pretrained('gpt2')

# 将文本转换为GPT输入格式
input_ids = torch.tensor(tokenizer.encode("舒适度")).unsqueeze(0)  # Batch size 1
outputs = model(input_ids)
word_embedding = outputs.last_hidden_state[0][1] # 取出“舒适度”对应的词向量
酒店评论属性与关键词相似度分析

原文地址: https://www.cveoy.top/t/topic/onJU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录