Python程式查詢字串中所有單詞的起始和結束索引
有時,我們需要單詞的起始索引以及該單詞的最後一個索引。一個句子由用空格分隔的單片語成。在這篇Python文章中,透過兩個不同的示例,給出了兩種查詢句子或給定字串中所有單詞的起始和結束索引的不同方法。在第一個示例中,在查詢空格以標記單詞的開頭時,遵循了對字串所有字元進行簡單迭代的過程。在示例2中,使用自然語言工具包來查詢字串中所有單詞的起始和結束索引。
示例1 - 透過迭代字串查詢字串中所有單詞的起始和結束索引。
演算法
步驟1 − 首先取一個字串並將其稱為givenStr。
步驟2 − 建立一個名為StartandEndIndex的函式,該函式將接收此givenStr並對其進行迭代,檢查空格並返回一個包含所有單詞的起始和結束索引的元組列表。
步驟3 − 使用split方法建立listofwords。
步驟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))
檢視結果 - 示例1
要檢視結果,請在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 − 它將返回一個單詞索引列表,但將包含空格。從最後一個單詞索引值中減去1以獲取不包含空格的單詞索引列表。
步驟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))
檢視結果 - 示例2
開啟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的必需模組。使用此模組並指定單詞列表中的標記,可以找到所有單詞的索引。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP