資料來源: https://docs.python.org/3/library/stdtypes.html#str.find
data.txt:
I love you all. But you do love love me at all.\nYou always love Python.
程式碼:
filePath = r”C:\Python\myfind\data.txt”
def keywordIndexFromFile(patterns : str, filePath : str) -> list:
with open(filePath,”r”) as f:
dataStr = f.read()
#print(dataStr)
lst=[]
idx = dataStr.find(patterns)
#lst=[ idx ]
while idx != -1:
#idxTmp = dataStr.find(patterns,idx+1)
lst.append( idx )
idx = dataStr.find(patterns,idx+1)
return lst
print(keywordIndexFromFile(“love”,filePath))
參考解答:
詞頻計算:
# -*- coding: utf-8 -*-
from typing import Tuple
filePath = r”C:\Python\myfind\data.txt”
def keywordIndexFromFile(patterns : str,
filePath : str) -> Tuple[list[int],float]:
with open(filePath,”r”) as f:
dataStr = f.read()
#print(dataStr)
lst=[]
idx = dataStr.find(patterns)
#lst=[ idx ]
while idx != -1:
#idxTmp = dataStr.find(patterns,idx+1)
lst.append( idx )
idx = dataStr.find(patterns,idx+1)
lstAll = dataStr.split(” “)
#print(lstAll , len(lstAll))
termFrequency = len(lst) / len( lstAll )
return lst, termFrequency
lst, termFrequency = keywordIndexFromFile(“love”,filePath)
print(“Key word index:”,lst)
print(“詞頻: %.2f %%” %(termFrequency*100) )
參考解答:
改用**dict當作輸入參數: