'difficult' 确定 python 中 2 个字符串之间的接近度
'difficult' Determine proximity between 2 strings in python
我有 2 个字符串 loss of gene
和 aquaporin protein
。在一行中,我想查找这两个是否存在于我的文件的一行中,在 5 个单词的附近。
有任何想法吗?我进行了广泛的搜索,但找不到任何东西。
另外,由于这些是多词字符串,我不能对这两个词使用 abs(array.index)(这可以用单个词)。
谢谢
我不确定这是否是您想要的,但我会试一试!
在Python中,您可以使用"in"来检查一个字符串是否在另一个字符串中。我假设您已经有办法从文件中存储一行:
"loss of gene" in fileLine -> returns boolean (either True or False)
有了这个,你可以检查 "loss of gene" 和 "aquaporin protein" 是否在你的文件中。一旦你确认它们都在那里,你可以通过将文本行分成一个列表来检查它们的接近度:
wordsList = fileLine.split()
如果在您的文本文件中有字符串:
"The aquaporin protein sometimes may exhibit a loss of gene"
拆分后变为:
["The","aquaporin","protein","sometimes","may","exhibit","a","loss","of","gene"]
我不确定这是否是一个有效的句子,但为了举例,让我们忽略它:P
将文本行拆分为单词列表并确认单词在其中后,您可以使用 python![=15= 中列表附带的索引函数获取它们的接近度]
wordsList.index("protein") -> returns index 2
找到索引 "protein" 后,您可以检查索引 "loss" 的位置,然后减去它们以查明它们是否在 5 个单词的邻近范围内。
可以使用索引函数来判断"loss of gene"是在"aquaporin protein"之前还是之后。如果 "loss of gene" 先出现,索引 "gene" 和 "aquaporin" 并减去这些索引。如果 "aquaporin protein" 先出现,索引 "protein" 和 "loss" 并减去这些索引。
如果单词以不同的顺序出现,您将需要做更多的工作以确保正确减去索引,但这应该涵盖问题的实质。祝查哈特好运!
您可以尝试以下方法:
首先通过将文本转换为小写来清理您的文本,仅保留字符并在每个单词之间强制执行一个 space。
接下来,搜索结果文本中的每个短语,并记下起始索引和匹配短语的长度。对这个索引列表进行排序。
接下来通过确保所有找到的索引不是 -1
.
来确保文本中存在所有短语
如果全部找到,请计算第一个短语结尾和最后一个短语开头之间的单词数。为此,将文本切片从第一个短语的末尾开始到第二个短语的开头,并将其拆分为单词。
脚本如下:
import re
text = "The Aquaporin protein, sometimes 'may' exhibit a big LOSS of gene."
text = ' '.join(re.findall(r'\b(\w+)\b', text.lower()))
indexes = sorted((text.find(x), len(x)) for x in ['loss of gene', 'aquaporin protein'])
if all(i[0] != -1 for i in indexes) and len(text[indexes[0][0] + indexes[0][1] : indexes[-1][0]].split()) <= 5:
print "matched"
要将其扩展到处理包含短语列表的文件,可以使用以下方法:
import re
log = 'loss of gene'
phrases = ['aquaporin protein', 'another protein']
with open('input.txt') as f_input:
for number, line in enumerate(f_input, start=1):
# Sanitise the line
text = ' '.join(re.findall(r'\b(\w+)\b', line.lower()))
# Only process lines containing 'loss of gene'
log_index = text.find(log)
if log_index != -1:
for phrase in phrases:
phrase_index = text.find(phrase)
if phrase_index != -1:
if log_index < phrase_index:
start, end = (log_index + len(log), phrase_index)
else:
start, end = (phrase_index + len(phrase), log_index)
if len(text[start:end].split()) <= 5:
print "line {} matched - {}".format(number, phrase)
break
这将为您提供以下类型的输出:
line 1 matched - aquaporin protein
line 5 matched - another protein
请注意,这只会发现每行一对短语。
我有 2 个字符串 loss of gene
和 aquaporin protein
。在一行中,我想查找这两个是否存在于我的文件的一行中,在 5 个单词的附近。
有任何想法吗?我进行了广泛的搜索,但找不到任何东西。
另外,由于这些是多词字符串,我不能对这两个词使用 abs(array.index)(这可以用单个词)。
谢谢
我不确定这是否是您想要的,但我会试一试!
在Python中,您可以使用"in"来检查一个字符串是否在另一个字符串中。我假设您已经有办法从文件中存储一行:
"loss of gene" in fileLine -> returns boolean (either True or False)
有了这个,你可以检查 "loss of gene" 和 "aquaporin protein" 是否在你的文件中。一旦你确认它们都在那里,你可以通过将文本行分成一个列表来检查它们的接近度:
wordsList = fileLine.split()
如果在您的文本文件中有字符串:
"The aquaporin protein sometimes may exhibit a loss of gene"
拆分后变为:
["The","aquaporin","protein","sometimes","may","exhibit","a","loss","of","gene"]
我不确定这是否是一个有效的句子,但为了举例,让我们忽略它:P
将文本行拆分为单词列表并确认单词在其中后,您可以使用 python![=15= 中列表附带的索引函数获取它们的接近度]
wordsList.index("protein") -> returns index 2
找到索引 "protein" 后,您可以检查索引 "loss" 的位置,然后减去它们以查明它们是否在 5 个单词的邻近范围内。
可以使用索引函数来判断"loss of gene"是在"aquaporin protein"之前还是之后。如果 "loss of gene" 先出现,索引 "gene" 和 "aquaporin" 并减去这些索引。如果 "aquaporin protein" 先出现,索引 "protein" 和 "loss" 并减去这些索引。
如果单词以不同的顺序出现,您将需要做更多的工作以确保正确减去索引,但这应该涵盖问题的实质。祝查哈特好运!
您可以尝试以下方法:
首先通过将文本转换为小写来清理您的文本,仅保留字符并在每个单词之间强制执行一个 space。
接下来,搜索结果文本中的每个短语,并记下起始索引和匹配短语的长度。对这个索引列表进行排序。
接下来通过确保所有找到的索引不是
-1
. 来确保文本中存在所有短语
如果全部找到,请计算第一个短语结尾和最后一个短语开头之间的单词数。为此,将文本切片从第一个短语的末尾开始到第二个短语的开头,并将其拆分为单词。
脚本如下:
import re
text = "The Aquaporin protein, sometimes 'may' exhibit a big LOSS of gene."
text = ' '.join(re.findall(r'\b(\w+)\b', text.lower()))
indexes = sorted((text.find(x), len(x)) for x in ['loss of gene', 'aquaporin protein'])
if all(i[0] != -1 for i in indexes) and len(text[indexes[0][0] + indexes[0][1] : indexes[-1][0]].split()) <= 5:
print "matched"
要将其扩展到处理包含短语列表的文件,可以使用以下方法:
import re
log = 'loss of gene'
phrases = ['aquaporin protein', 'another protein']
with open('input.txt') as f_input:
for number, line in enumerate(f_input, start=1):
# Sanitise the line
text = ' '.join(re.findall(r'\b(\w+)\b', line.lower()))
# Only process lines containing 'loss of gene'
log_index = text.find(log)
if log_index != -1:
for phrase in phrases:
phrase_index = text.find(phrase)
if phrase_index != -1:
if log_index < phrase_index:
start, end = (log_index + len(log), phrase_index)
else:
start, end = (phrase_index + len(phrase), log_index)
if len(text[start:end].split()) <= 5:
print "line {} matched - {}".format(number, phrase)
break
这将为您提供以下类型的输出:
line 1 matched - aquaporin protein
line 5 matched - another protein
请注意,这只会发现每行一对短语。