我有两张单子
l1=["humans" ,"are","intelligent"]
l2=["men","are","brilliant"]
和一个相似矩阵
m=[["humans","men"],["intelligent","smart"],["smart","brilliant"]]
上述矩阵表明,(“人类”和“男人”)是相似的词,(“智能”和“智能”)
传递属性也成立,因此(智能和卓越)也类似
给出了两个句子
"men are brilliant"
"humans are intelligent "
写一段代码检查两个句子是否相似
Tags:
由于transitive属性为true,因此需要在属性为true的组合列表中进行不同的组合
例如,在您的案例中:
["intelligent", "smart"],["smart", "brilliant"]
这两个列表必须合并因此,当您组合这两个列表时,您会得到
["intelligent", "smart", "brilliant"]
现在应该可以找到它的不同组合,您可以使用itertools.combinations_with_replacement
进行此操作在找到组合之前,您需要知道此属性适用于哪个列表,因此为此,我们可以使用返回的
set.isdisjoint
True
如果两个列表包含不同的元素,则返回False
现在拆分句子并删除矩阵中不存在的非关键字单词,然后压缩它们并检查是否在使用上述方法构建的新列表中
下面是我的想法(你可以改进)的演示:
如果句子相似,上述程序将打印
True
我考虑的另一种方法是使用集合并集并删除交集。但是如果您的内部列表包含更多元素,则此方法无法正常工作
相关问题 更多 >
编程相关推荐