Sentence Similarity问题及解法

问题描述:

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar.

However, similarity is symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"].


问题分析:

将相似词保存到map中去,每一个词所有的相似词都保留在一个vector中,遍历words1和words2时,只需要对比两个词是否为同义词即可。


过程详见代码:

class Solution {
public:
    bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        int n1 = words1.size();
		int n2 = words2.size();
		if (n1 != n2) return 0;
		unordered_map<string, vector<string>> map;
		for (auto p : pairs)
		{
			map[p.first].push_back(p.second);
			map[p.second].push_back(p.first);
		}
		for (int i = 0; i < n1; i++)
		{
			if (words1[i] == words2[i]) continue;
			vector<string> t = map[words1[i]];
			int flg = 0;
			for (int j = 0; j < t.size(); j++)
			{
				if (t[j] == words2[i])
				{
					flg = 1;
					break;
				}
			}
			if (!flg) return false;
		}
		return true;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sentence Transformer是一个Python框架,用于生成句子、文本和图像的嵌入(Embedding)表示。通过使用该框架,可以将文本转化为向量表示,从而在文本相似度计算、文本分类等任务中使用。这个框架使用了预训练的语言模型,例如MiniLM等,来生成高质量的文本嵌入,以捕捉句子的语义信息。 使用Sentence Transformer的过程是比较简单的。首先,需要导入框架并加载相应的模型。然后,将待处理的句子作为一个字符串列表传入模型的encode方法中,即可获取到对应的嵌入向量。最后,可以根据需要对这些嵌入向量进行进一步的处理和应用。 通过使用Sentence Transformer,我们可以方便地将文本转化为向量表示,并在各种NLP任务中发挥作用,比如文本相似度计算、文本分类、信息检索等。这个框架提供了一种简单而有效的方式来处理文本数据,并获得具有丰富语义信息的嵌入表示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SentenceTransformers库介绍](https://blog.csdn.net/m0_47256162/article/details/129380499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [5分钟 NLP系列 — SentenceTransformers 库介绍](https://blog.csdn.net/m0_46510245/article/details/122516399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值