Python 如何包含选定的子字符串?

Python 如何包含选定的子字符串?,python,string,Python,String,我正在搜索一个大字符串中的目标文本。我的代码选择字符串中的文本,并在前面显示40个字符,在前面显示40个字符。相反,我希望在目标文本前面显示两个句子,后面显示两个句子。我的代码: import re sentence = "In addition, participation in life situations can be somewhat impaired because of communicative disabilities associated with the disorder

我正在搜索一个大字符串中的目标文本。我的代码选择字符串中的文本,并在前面显示40个字符,在前面显示40个字符。相反,我希望在目标文本前面显示两个句子,后面显示两个句子。我的代码:

import re

sentence = "In addition, participation in life situations can be somewhat impaired because of communicative disabilities associated with the disorder and parents’ lack of resources for overcoming this aspect of the disability (i.e. communication devices). The attitudes of service providers are also important. The Australian Rett syndrome research program is based on a biopsychosocial model which integrates aspects of both medical and social models of disability and functioning. The investigation of environmental factors such as equipment and support available to individuals and families and the social capital of the communities in which they live is likely to be integral to understanding the burden of this disorder. The program will use the ICF framework to identify those factors determined to be most beneficial and cost effective in optimising health, function and quality of life for the affected child and her family."

sub = "biopsychosocial model"

def find_all_substrings(string, sub):
    starts = [match.start() for match in re.finditer(re.escape(sub), string.lower())]
    return starts 

substrings = find_all_substrings(sentence, sub)
for pos in substrings: print(sentence[pos-40:pos+40])

如何在目标文本前面显示两个句子,在后面显示两个句子?

您可以先将文本拆分为几个句子,然后查找包含要查找的子字符串的所有句子(及其索引)。然后把找到的句子切掉

下面是一个示例(使用):

这将找到包含子字符串的每个句子的索引(放置在
匹配的\u索引中),然后
显示的\u句子
包含匹配句子前后的句子(根据
n\u发送的数字\u填充

然后显示的句子是:

['The attitudes of service providers are also important. The Australian Rett syndrome research program is based on a biopsychosocial model which integrates aspects of both medical and social models of disability and functioning. The investigation of environmental factors such as equipment and support available to individuals and families and the social capital of the communities in which they live is likely to be integral to understanding the burden of this disorder.']

注意nltk如何拆分句子:有时它做得有点奇怪(例如在“先生”中拆分句点)。是关于如何调整句子标记器。

您可以先将文本拆分为句子,然后查找所有句子(及其索引)包含您要查找的子字符串。然后只需将找到的句子周围的句子切分

下面是一个示例(使用):

这将找到包含子字符串的每个句子的索引(放置在
匹配的\u索引中),然后
显示的\u句子
包含匹配句子前后的句子(根据
n\u发送的数字\u填充

然后显示的句子是:

['The attitudes of service providers are also important. The Australian Rett syndrome research program is based on a biopsychosocial model which integrates aspects of both medical and social models of disability and functioning. The investigation of environmental factors such as equipment and support available to individuals and families and the social capital of the communities in which they live is likely to be integral to understanding the burden of this disorder.']

注意nltk如何拆分句子:有时它做得有点奇怪(例如,在“先生”中拆分句点)。是关于如何调整句子标记器。

你的答案比我脑子里想的容易多了……你的答案比我脑子里想的容易多了。。。