0
我定義了此函數以在文本文件中查找關鍵字,但現在我想要獲取包含文件中關鍵詞之前和之後單詞的元組我不知道該怎麼做如何從文本文件中獲取特定單詞之前和之後的單詞
def findProperWords(paragraphs, excludedWords):
key = [] # an empty list
count = 0
for paragraph in paragraphs: #calling of every paaragraph in the textfile
count += 1 #counting each paragraph
words = list(paragraph.split(' '))
# spliting each paragraph into a list of words
for keys in words:
if len(keys) > 0:
if keys[0] == keys[0].upper():
#checking for words that start with capital letters
if keys.lower() not in excludedWords:
key.append(keys) # creating a list of the key words
index = paragraph.find(keys)
# finding the position of each key word in the textile
只有一個小提示:有一個內置的方法來檢查大寫字母:'if keys [0] .isupper():'。並且'如果len(keys)> 0:'可以簡單地寫成'if key:' – VPfB