Python 查找字符串中所有单词的起始和结束索引程序
有时,我们需要一个单词的起始索引和结束索引。一个句子由用空格分隔的单词组成。本Python文章将使用两个不同的示例,介绍在句子或给定字符串中找到所有单词的起始和结束索引的两种不同方法。在第一个示例中,通过遍历字符串中的所有字符,找到空格来标记单词的开头。在示例2中,使用自然语言处理工具包来找到字符串中所有单词的起始和结束索引。
示例1 – 通过遍历字符串来查找字符串中所有单词的起始和结束索引
步骤
步骤1: 首先输入一个字符串,并将其命名为givenStr。
步骤2: 创建一个名为StartandEndIndex的函数,该函数将遍历givenStr,检查空格,并返回一个由起始和结束索引组成的元组列表。
步骤3: 使用split方法创建一个单词列表。
步骤4: 使用上述两个列表的值创建一个字典。
步骤5: 运行程序并检查结果。
Python文件包含以下内容
#function for given word indices
def StartandEndIndex(givenStr):
indexList = []
startNum = 0
lengthOfSentence=len(givenStr)
#iterate though the given string
for indexitem in range(0,lengthOfSentence):
#check if there is a separate word
if givenStr[indexitem] == " ":
indexList.append((startNum, indexitem - 1))
indexitem += 1
startNum = indexitem
if startNum != len(givenStr):
indexList.append((startNum, len(givenStr) - 1))
return indexList
givenStr = 'Keep your face always toward the sunshine and shadows will fall behind you'
#call the function StartandEndIndex(givenStr)
#and get the list having starting and ending indices of all words
indexListt = StartandEndIndex(givenStr)
# make a list of words separately
listofwords= givenStr.split()
print("\nThe given String or Sentence is ")
print(givenStr)
print("\nThe list of words is ")
print(listofwords)
#make a dictionary using words and their indices
resDict = {listofwords[indx]: indexListt[indx] for indx in range(len(listofwords))}
print("\nWords and their indices : " + str(resDict))
结果
要查看结果,请在cmd窗口中运行Python文件。
The given String or Sentence is
Keep your face always toward the sunshine and shadows will fall behind you
The list of words is
['Keep', 'your', 'face', 'always', 'toward', 'the', 'sunshine', 'and', 'shadows', 'will', 'fall', 'behind', 'you']
Words and their indices : {'Keep': (0, 3), 'your': (5, 8), 'face': (10, 13), 'always': (15, 20), 'toward': (22, 27), 'the': (29, 31), 'sunshine': (33, 40), 'and': (42, 44), 'shadows': (46, 52), 'will': (54, 57), 'fall': (59, 62), 'behind': (64, 69), 'you': (71, 73)}
图 1:在命令窗口中显示结果。
示例 2:使用nltk(自然语言工具包)在字符串中找到所有单词的起始和结束索引
步骤
步骤 1 - 首先使用pip命令安装nltk。然后从中导入align_tokens。
步骤 2 - 将给定的字符串givenStr使用split函数拆分成单词,并将其称为listofwords。
步骤 3 - 现在使用align_tokens以listofwords为标记和给定的字符串givenStr。
步骤 4 - 它会返回一个单词索引列表,但会包含空格。从最后一个单词索引值中减去一,以得到不包含空格的单词索引列表。
步骤 5 - 使用上述两个列表的值构建一个字典。
步骤 6 - 运行程序,然后检查结果。
Python文件包含如下内容
#Use pip install nltk to install this library
#import align tokens
from nltk.tokenize.util import align_tokens
#specify a string for testing
givenStr = 'Keep your face always toward the sunshine and shadows will fall behind you'
#make a list of words
listofwords= givenStr.split()
print("\nThe given String or Sentence is ")
print(givenStr)
print("\nThe list of words is ")
print(listofwords)
#this will include blank spaces with words while giving indices
indices_includingspace= align_tokens(listofwords, givenStr)
indices_withoutspace=[]
#reduce the last index number of the word indices
for item in indices_includingspace:
#convert tuple to list
lst = list(item)
lst[1]=lst[1] - 1
#convert list to tuple again
tup = tuple(lst)
indices_withoutspace.append(tup)
print(indices_withoutspace)
#make the dictionary of all words in a string with their indices
resDict = {listofwords[indx]: indices_withoutspace[indx] for indx in range(len(listofwords))}
print("\nWords and their indices : " + str(resDict))
结果
打开cmd窗口,运行python文件以查看结果。
The given String or Sentence is
Keep your face always toward the sunshine and shadows will fall behind you
The list of words is
['Keep', 'your', 'face', 'always', 'toward', 'the', 'sunshine', 'and', 'shadows', 'will', 'fall', 'behind', 'you']
[(0, 3), (5, 8), (10, 13), (15, 20), (22, 27), (29, 31), (33, 40), (42, 44), (46, 52), (54, 57), (59, 62), (64, 69), (71, 73)]
Words and their indices : {'Keep': (0, 3), 'your': (5, 8), 'face': (10, 13), 'always': (15, 20), 'toward': (22, 27), 'the': (29, 31), 'sunshine': (33, 40), 'and': (42, 44), 'shadows': (46, 52), 'will': (54, 57), 'fall': (59, 62), 'behind': (64, 69), 'you': (71, 73)}
图2:显示单词及其索引。
在本 Python 文章中,通过两个不同的示例介绍了在字符串中查找所有单词的开始和结束索引的方法。在示例1中,使用迭代遍历字符串中的所有字符来完成此操作。在此过程中,空格被选择为标记新单词的起始位置。在示例2中,使用了一个名为nltk或自然语言工具包的库。首先,使用pip进行安装。然后导入所需的名为align_tokens的模块。使用该模块,以及指定来自单词列表的令牌,可以找到所有单词的索引。