- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想用 vanilla python 制作一个简单的抄袭检查器。在 python 中不使用外部库
因此,如果相同的单词连续出现超过 4 次,我想打印输出(连续的相同单词)
我尝试了下面的代码。但它会显示每个相同的单词,即使这些单词连续相同的次数不超过 4 次。
b1='i guess that osaka city is just a souless city it is obviously weird'.split(' ')
a1='all of the meaning less time i guess thinking that osaka city is huge a souless city it is obviously weird'.split(' ')
# expected_result
#['that osaka city is','a souless city it is obviously weird']
temp1=[]
for b in b1:
for a in a1:
if b == a :
temp1.append(b)
if len(temp1)>=4:
print(' '.join(temp1))
else:
print('==')
然而结果是
i guess that osaka city city is is a souless city city it is is obviousl
y
i guess that osaka city city is is a souless city city it is is obviousl
y
i guess that osaka city city is is a souless city city it is is obviousl
y weird
而且......这就是我想做的
#### Example;
# INPUT
a = 'Hello my name is Osaka, today I learned about Mochi
is just a shit of snowman'
b = 'Hello my name is Kidney, bullshit, mann yesterday I learned about Katzu is just a shit of snowman'
# EXPECTED OUTPUT
['Hello my name is','is just a shit of snowman']
最佳答案
您将 a1
中的每个单词与 b1
中的每个单词进行比较。所有匹配的单词都会添加到 temp1
中。但你永远不会检查单词的序列。这就是为什么您会得到 b1
中 a1
的所有单词。
这里有一个比较序列的简单想法:获取 a1
和 b1
中的每对索引,并在字符匹配时尝试前进。如果找到 4 个或更多匹配字符,则输出字符:
B='i guess that osaka city is just a souless city it is obviously weird'.split(' ')
A='all of the meaning less time i guess thinking that osaka city is huge a souless city it is obviously weird'.split(' ')
for i in range(len(A)):
for j in range(len(B)):
m, n = i, j
while m<len(A) and n<len(B) and A[m] == B[n]:
m, n = m+1, n+1
if m-i >= 4:
print((i, j), A[i:m])
如果你在“Vanilla Python”中承认itertools
(我知道VanillaJS,但我不确定“Vanilla Python”是什么意思),你可以这样写:
import itertools
for i, j in itertools.product(range(len(A)), range(len(B))):
L = [u for u,v in itertools.takewhile(lambda u_v : u_v[0]==u_v[1], zip(A[i:], B[j:]))]
if len(L)>=4:
print((i,j), L)
输出
(9, 2) ['that', 'osaka', 'city', 'is']
(14, 7) ['a', 'souless', 'city', 'it', 'is', 'obviously', 'weird']
(15, 8) ['souless', 'city', 'it', 'is', 'obviously', 'weird']
(16, 9) ['city', 'it', 'is', 'obviously', 'weird']
(17, 10) ['it', 'is', 'obviously', 'weird']
你会得到一些垃圾,因为 if ['a', 'souless', 'city', 'it', 'is', 'obviously', 'weird']
是最长的比赛开始在 (14, 7)
处,我们知道从 (15, 8)
开始的列表也将是匹配的。让我们添加一个排除
集来删除这些子列表:
exclude = set()
for i in range(len(A)):
for j in range(len(B)):
if (i,j) in exclude:
exclude.remove((i,j))
continue
m, n = i, j
while m<len(A) and n<len(B) and A[m] == B[n]:
m, n = m+1, n+1
if m-i >= 4:
print((i, j), A[i:m])
exclude.update((i+k, j+k) for k in range(1, m-i))
print ("exclude = ", exclude)
输出:
(9, 2) ['that', 'osaka', 'city', 'is']
exclude = {(12, 5), (11, 4), (10, 3)}
(14, 7) ['a', 'souless', 'city', 'it', 'is', 'obviously', 'weird']
exclude = {(20, 13), (16, 9), (17, 10), (15, 8), (19, 12), (18, 11)}
这个方法可行,但速度很慢:时间复杂度为 O(|A|*|B|*最长匹配)。可以使用以下方法来节省一些检查:为列表 |B| 构建一个字典 word -> [positions]
以避免重新检查 B
中的所有索引A
的每个单词:
positions_by_word_in_B = {}
for j, word in enumerate(B):
positions_by_word_in_B.setdefault(word, []).append(j)
输出:
{'i': [0], 'guess': [1], 'that': [2], 'osaka': [3], 'city': [4, 9], 'is': [5, 11], 'just': [6], 'a': [7], 'souless': [8], 'it': [10], 'obviously': [12], 'weird'
: [13]}
主循环变为:
for i in range(len(A)):
for j in positions_by_word_in_B.get(A[i], []):
# all positions of A[i] in B, maybe none
时间复杂度降至 O(|B| + |A|*|A 的单词在 B|*最长匹配中的最大出现次数)。您还可以在 len(A)-4 而不是 len(A)-1 处停止迭代。
如果您想检查大量文档是否存在抄袭,这可能仍然太慢。
关于python - vanilla python - (抄袭检查器)如何检测原始句子中相同的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55467841/
假设我有一个数据集,每行包含一个句子,该句子来自一个非常大的调查(德语和法语)中的一个开放式问题。大多数句子(答案)是合乎逻辑的;即有意义的单词组合。但是,也有一些粗心的受访者只是简单地填写了各种不合
我的 MySQL 数据库中有一个表,其结构如下: CREATE TABLE `papers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varch
在新的 Edge 浏览器(基于 chromium)中,有一个文本到语音的选项,在阅读页面时它会突出显示正在阅读的句子和单词,就像这样 - 过去我有一个简单的 Windows TTS 应用程序,我通过将
我有一本书的图像文件。我正在编写一个 Web 应用程序,用于加载书籍并一次显示一页。我想知道如何在页面中选择一个句子并显示一条消息。据我所知,它必须具有图像坐标。 请参阅http://epaper.d
我使用的 GPS 输出多个 NMEA 语句,可用于定位数据。 (GPGGA 和 GPRMC)。有什么理由我应该使用一个而不是另一个吗?我应该检查它们并比较数据吗?我可以随便挑一个使用吗? 在这一点上,
我想使用TinyGPS++在 Arduino 上解析 NMEA 数据并在 OLED 显示屏上显示信息。但是,NMEA 数据将通过 USB 接收,而不是使用软件串行和 TX/RX 引脚。 我按照 Tin
我需要删除其中的所有空格。 例如:这是我的代码O/P:Thisismycode 这是我到目前为止的代码。 import java.util.Scanner; public class nospace{
我对 python 很陌生,我不知道如何解决以下问题: 我有两个数据框,我想使用某种 VLOOKUP 函数来将句子与特定关键字相匹配。在下面的示例中,(df1) 3e 句子应与“banana”(df2
这个问题已经有答案了: How slicing in Python works (38 个回答) Python list slice syntax used for no obvious reason
我想在我的表格作者的句子列中找到以 # 开头的单词。我不知道我在寻找什么词,因为我只知道它以 # 开头。 表:作者(姓名,句子) 作者 |句子 艾伯特 |我#want to be #discussin
句子
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve
我目前正在经历免费代码营的第一个 JS 挑战。 我在标题为“句子首字母大写”的挑战中遇到了问题。在这个挑战中,我需要编写一个函数,将给定字符串中单词的每个第一个字母大写,并将所有其他字母小写。 Her
假设我有一个文本,看起来像这样: Some sentence in which a fox jumps over some fence. Another sentence in which a
我是 C++ 的初学者,我想了解有关字符的更多信息,但我遇到了问题。我试图制作一个程序,它复制一个句子并在空格 (' ') 之间添加一个新行 ('\n'),就像一个单词一个单词地分开一个句子. int
我需要将一个句子(例如“Hello world”)复制到一个字符串列表中,意思是复制到一个字符数组中,其中每 2 个单词由 '\0' 分隔。请注意,单词被定义为一行中没有空格的任意数量的字符。 因此,
我有这样一个字符串, my_str ='·in this match, dated may 1, 2013 (the "the match") is between brooklyn centenni
我在列表中有一堆句子,我想使用 nltk 库来阻止它。我可以一次提取一个句子,但是我在从列表中提取句子并将它们重新组合在一起时遇到了问题。我缺少一个步骤吗? nltk 库很新。谢谢! import n
我有一个词和文本。我必须找到所有包含该词的提案。你有什么想法吗? piblic List GetSnetences(string word) { // search all proposals
我正在通过 doc2vec 模型使用 gensim 库在 Python 中构建 NLP 聊天应用程序。我有硬编码的文档并给出了一组训练示例,我通过抛出用户问题来测试模型,然后第一步找到最相似的文档。在
我有以下代码,每 10 个单词拆分一行。 #!/bin/bash while read line do counter=1; for word in $line do
我是一名优秀的程序员,十分优秀!