[LeetCode] 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"].

Note:

  • The length of words1 and words2 will not exceed 1000.
  • The length of pairs will not exceed 2000.
  • The length of each pairs[i] will be 2.
  • The length of each words[i] and pairs[i][j] will be in the range [1, 20].

这道题给了我们两个句子,问这两个句子是否是相似的。判定的条件是两个句子的单词数要相同,而且每两个对应的单词要是相似度,这里会给一些相似的单词对,这里说明了单词对的相似具有互逆性但是没有传递性。看到这里博主似乎已经看到了Follow up了,加上传递性就是一个很好的拓展。那么这里没有传递性,就使得问题变得很容易了,我们只要建立一个单词和其所有相似单词的集合的映射就可以了,比如说如果great和fine类似,且great和good类似,那么就有下面这个映射:

great -> {fine, good}

所以我们在逐个检验两个句子中对应的单词时就可以直接去映射中找,注意有可能遇到的单词对时反过来的,比如fine和great,所以我们两个单词都要带到映射中去查找,只要有一个能查找到,就说明是相似的,反之,如果两个都没查找到,说明不相似,直接返回false,参见代码如下: 

class Solution {

public:
    bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        if (words1.size() != words2.size()) return false;
        unordered_map<string, unordered_set<string>> m;
        for (auto pair : pairs) {
            m[pair.first].insert(pair.second);
        }
        for (int i = 0; i < words1.size(); ++i) {
            if (words1[i] == words2[i]) continue;
            if (!m[words1[i]].count(words2[i]) && !m[words2[i]].count(words1[i])) return false;
        }
        return true;
    }
};

 本文转自博客园Grandyang的博客,原文链接:[LeetCode] Sentence Similarity 句子相似度

,如需转载请自行联系原博主。

时间: 2024-08-01 15:31:44

[LeetCode] Sentence Similarity 句子相似度的相关文章

[LeetCode] Sentence Similarity II 句子相似度之二

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, words1 = ["great", "acting", "skills"] and words2 = [&

急求句子相似度算法 在线等!!!

问题描述 求助各位提供一个计算句子相似的算法(句子只包括主语,谓语,宾语),我不会编.请教各位谁能提供源代码. 解决方案 解决方案二:各位帮帮忙!!!在线等!!!解决方案三:使用编辑距离吧./***@authorkaynezhang**/publicclassTest{privatestaticintminimum(inta,intb,intc){intmi;mi=a;if(b<mi){mi=b;}if(c<mi){mi=c;}returnmi;}privatestaticintgetLsnD

[LeetCode] Employee Importance 员工重要度

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. Th

LSF-SCNN:一种基于CNN的短文本表达模型及相似度计算的全新优化模型

本篇文章是我在读期间,对自然语言处理中的文本相似度问题研究取得的一点小成果.如果你对自然语言处理 (natural language processing, NLP) 和卷积神经网络(convolutional neural network, CNN)有一定的了解,可以直接看摘要和LSF-SCNN创新与技术实现部分.如果能启发灵感,应用于更多的现实场景中带来效果提升,那才是这篇文章闪光的时刻.如果你没有接触过NLP和CNN,也不在担心,可以从头到尾听我娓娓道来.有任何问题,欢迎交流. 1. 摘要

[推荐系统]余弦计算相似度度量

余弦计算相似度度量 相似度度量(Similarity),即计算个体间的相似程度,相似度度量的值越小,说明个体间相似度越小,相似度的值越大说明个体差异越大. 对于多个不同的文本或者短文本对话消息要来计算他们之间的相似度如何,一个好的做法就是将这些文本中词语,映射到向量空间,形成文本中文字和向量数据的映射关系,通过计算几个或者多个不同的向量的差异的大小,来计算文本的相似度.下面介绍一个详细成熟的向量空间余弦相似度方法计算相似度 向量空间余弦相似度(Cosine Similarity) 余弦相似度用向

OJ题:句子逆转

将一个英文语句以单词为单位逆序排放.例如"I am a boy",逆序排放后为"boy a am I"所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符 接口说明 /** * 反转句子 *  * @param sentence 原句子 * @return 反转后的句子 */public String reverse(String sentence); 输入描述:将一个英文语句以单词为单位逆序排放. 输出描述:得到逆序的句子 输入例子:I am a b

大神们最近都在读这些论文 | 本周值得读 #44

#GAN# Triple Generative Adversarial Nets 从博弈角度来说,TripleGAN 的博弈涉及三方,判别器.生成器和分类器.其中,判别器和生成器有对抗:判别器和分类器(在训练前期)有对抗:生成器和分类器协助作用.可以从斗地主的角度来看,判别器是地主,生成器和分类器是农民. 它拆掉分类器,就是一个 CGAN.拆掉生成器,它就是一个半监督的 GAN.此外,我们还能从对偶学习的角度进行解读,生成器对 p(x|y) 进行建模,而分类器则对 p(y|x) 建模.两者在判别

基于 TensorFlow 的上下文机器人

本文讲的是基于 TensorFlow 的上下文机器人, 原文地址:Contextual Chatbots with Tensorflow 原文作者:gk_ 译文出自:掘金翻译计划 本文永久链接:https://github.com/xitu/gold-miner/blob/master/TODO/contextual-chat-bots-with-tensorflow.md 译者:edvardhua 校对者:lileizhenshuai, jasonxia23 基于 TensorFlow 的上下

AI人工智能专业词汇集

作为最早关注人工智能技术的媒体,机器之心在编译国外技术博客.论文.专家观点等内容上已经积累了超过两年多的经验.期间,从无到有,机器之心的编译团队一直在积累专业词汇.虽然有很多的文章因为专业性我们没能尽善尽美的编译为中文呈现给大家,但我们一直在进步.一直在积累.一直在提高自己的专业性. 两年来,机器之心编译团队整理过翻译词汇对照表「红宝书」,编辑个人也整理过类似的词典.而我们也从机器之心读者留言中发现,有些人工智能专业词汇没有统一的翻译标准,这可能是因地区.跨专业等等原因造成的.举个例子,Deep