我列出了學術文章摘要中出現頻率最高的 10 個詞。我想計算這些詞在我的資料集的觀察中出現的次數。
前10個詞是:
top10 = ['model','language','models','task', 'data', 'paper', 'results', 'information', 'text','performance']
前 3 個觀察結果的示例是:
column[0:3] = ['The models are showing a great performance.',
'The information and therefor the data in the text are good enough to fulfill the task.',
'Data in this way results in the best information and thus performance'.]
提供的代碼應回傳特定觀察中所有單詞的總出現次數串列。我試過下面的代碼,但它給出了錯誤:count() 最多需要 3 個引數 (10 個給定)。
我的代碼:
count = 0
for sentence in column:
for word in sentence.split():
count = word.lower().count('model','language','models','task', 'data', 'paper', 'results', 'information', 'text','performance')
我還想小寫所有單詞并洗掉標點符號。所以輸出應該是這樣的:
output = (2, 4, 4)
第一次觀察統計top10串列的2個詞,分別是models和performance
第二次觀察統計top10串列的4個詞,分別是資訊、資料、文本和任務
第三次觀察統計資料、結果、資料、資訊和性能的4個字
希望你能幫幫我!
uj5u.com熱心網友回復:
您可以使用正則運算式進行拆分,然后檢查它是否在前 10 名中。
count =[]
for i,sentence in enumerate(column):
c = 0
for word in re.findall('\w ',sentence):
c = int(word.lower() in top10)
count = [c]
計數 = [2, 4, 4]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374134.html
下一篇:如何放置空格介紹陣列
