我有一个字符串:
The exam is for testing your skills. The exam includes the following:
1) Aptitude
2)synonyms
3)Reasoning
因此,我使用以下代码使用字符串方法来识别单词的索引:
string.find('exam')
它为我提供了字符串中单词的索引。在这里,我必须确定每个句子末尾的分隔符。例如:
The exam is for testing your skills. [here it is '.']
The exam includes the following: [here it is ':']
那么,如何根据单词搜索来识别句子结尾的分隔符呢?
发布于 2019-09-23 15:39:44
你的问题陈述有些含糊,因为从句可以用",",":",";"
结束,但不能结束句子。若要解决此问题,请确定要查找的标点符号并将其设置为列表。
下面的代码标识了所有关键字的起始位置。然后,它定位您所识别的标点符号之一的第一个实例,并将其返回。
import re
text = '''
The exam is for testing your skills. The exam includes the following:
1) Aptitude
2)synonyms
3)Reasoning'''
targets =[m.start() for m in re.finditer('exam', text)]
end_punct = ['!','.','?',':',';']
for target in targets:
subtext = text[target:]
print(subtext)
for char in subtext:
if char in end_punct:
print(char)
break
示例返回:
#Returns:
.
:
https://stackoverflow.com/questions/58059927
复制相似问题