- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一个段落。我通过 sent_tokenize 将其分成句子:
variable = ['By the 1870s the scientific community and much of the general public had accepted evolution as a fact.',
'However, many favoured competing explanations and it was not until the emergence of the modern evolutionary synthesis from the 1930s to the 1950s that a broad consensus developed in which natural selection was the basic mechanism of evolution.',
'Darwin published his theory of evolution with compelling evidence in his 1859 book On the Origin of Species, overcoming scientific rejection of earlier concepts of transmutation of species.']
现在我将每个句子拆分成单词并将其附加到某个变量。我怎样才能找到具有最多相同单词的两组句子。我不知道该怎么做。如果我有 10 个句子,那么我将有 90 个检查(每个句子之间)。谢谢。
最佳答案
您可以使用 python sets 的交集.
如果你有这样的三个句子:
a = "a b c d"
b = "a c x y"
c = "a q v"
您可以检查两个句子中出现了多少相同的单词:
sameWords = set.intersection(set(a.split(" ")), set(c.split(" ")))
numberOfWords = len(sameWords)
有了它,您可以遍历您的句子列表,并找到其中包含最多相同单词的两个句子。这给了我们:
sentences = ["a b c d", "a d e f", "c x y", "a b c d x"]
def similar(s1, s2):
sameWords = set.intersection(set(s1.split(" ")), set(s2.split(" ")))
return len(sameWords)
currentSimilar = 0
s1 = ""
s2 = ""
for sentence in sentences:
for sentence2 in sentences:
if sentence is sentence2:
continue
similiarity = similar(sentence, sentence2)
if (similiarity > currentSimilar):
s1 = sentence
s2 = sentence2
currentSimilar = similiarity
print(s1, s2)
可能有一些 dynamic programming如果性能是一个问题,请解决这个问题。
关于python - 如何找到哪些句子有最多的共同词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19840079/
看来我又被一个简单的正则表达式卡住了。 我想要什么: 1 到 999 之间的数字 可选:逗号、符号 如果输入逗号,最少1位小数,最多3位小数点应该是presebt。 Allowed: 100 999,
我需要从两列中获取最大值并将其发送到第三列。这些列的大小都是统一的,但有时大小会有所不同,但它们都将从同一个单元格开始。例如: 5 8 - 6 2 - 6 5 - 带有破折号的列需要找到其他两个之间的
我在我的网站上有一张包含用户排名列的表格,排名是一个数字,我想选择排名最高的 3 个用户,所以我查看并搜索了我认为最好的查询是那:Link (正确答案的第二个查询),但我不明白查询,如果有人能一步一步
我正在尝试制作一个点击计数器,我想收集 24 小时内的总点击次数。无论最终用户位于哪个时区,这 24 小时都应该是固定值。在 24 小时内,数据库应更新为 +1 次点击计数,一旦达到 24 小时时间范
我有一个在典型共享主机上运行的 PHP + MySQL Web 应用程序,我想知道调用最多的查询是什么以及消耗的资源量是多少。这样,我将专注于最昂贵的查询以优化资源或检测优化不佳的查询。 例如: qu
这是我“尝试”从用户输入的数字中找到最大 2 个值的代码: #include using namespace std; void maximum2(int a, int& max1,int& max
我需要编写一个 Python 函数,从具有最多“o”字符的字符串中返回单词。例如,get_most_ooo_word(['aa ao ooo']) 应该返回 ['ooo'] 和 get_most_oo
我正在寻找一种哈希算法,以创建尽可能接近字符串的唯一哈希值 (max len = 255),从而生成一个长整数 (DWORD)。 我意识到26^255 >> 2^32,但也知道英语的单词数远少于2^3
我得到了一个仅由 's','t','u','v' 作为字符组成的字符串 T。我想找到长度为 |T| 的字符串数它最多与 T 不同 n 个位置。而且每个这样的字符串在三个不同的位置不能有相同的字符,这些
我有一群“专家”(大约 300 人)可以胜任一项工作。而且我有很多工作必须完成,比如说大约 500 个。我也有信息,一个专家能做某项工作有多“好”。这将导致一个 300 x 500 的矩阵来保存权重。
我正在尝试解决这个问题,虽然我可以使用蛮力解决它,但是以下优化算法为我提供了一些测试用例的错误结果。我尝试了但无法找到代码的问题,任何人都可以帮助我。 问题:给定一个字符串 S 和整数 K,找到整数
我需要一个混合长度的正则表达式验证,总长度为 6 个字符,其中 4-6 个大写/数字字符和 0-2 个空格。 我试过 ^[A-Z0-9]{4,6}+[\s]{0,2}$ 但它导致最大长度为 8 个字符
我有一个数组 {-1,2,3,4,-3,-2,1,5} 现在我想找到给定数组的最小连续总和子数组,最多 K 次交换。 在上面的数组中,最小连续和是-5,子数组是{-3,-2} 对于 K=1 我应该如何
我们有一个简单的表格如下: ------------------------------------------------------------------------ | Name |
如果哈希不能超过 4 个字符,并且这 4 个字符只能是小写字母或数字,那么创建 String 哈希的最佳方法是什么? 我要散列的字符串有 1-255 个字符。我知道在没有冲突的情况下创建 4-char
我希望使用 Multipeer Connectivity 框架,并感谢任何关于如何最好地进行的经验之谈。 我需要在“教练”设备和最多 45 个“玩家”设备之间建立连接。他们都在同一个空间,但无法预测
给定一个数组 a,什么是实现其组合直到第 n 的最佳方法?例如: a = %i[a b c] n = 2 # Expected => [[], [:a], [:b], [:c], [:a, b], [
这个问题在这里已经有了答案: Formatting floats without trailing zeros (21 个回答) 关闭8年前。 我想格式化最多包含 2 个小数位的 float 列表。但
我无法使用以下形式的命令登录到远程 docker 注册表: docker login –u my-username –p my-password registry.myclient.com 我得到的错
所以这是我的代码:服务器.java import java.io.*; import java.net.*; import java.util.*; class Server implements R
我是一名优秀的程序员,十分优秀!