如何使用 Python 查詢文字檔案中重複次數最多的單詞?


在本文中,我們將向您展示如何使用 Python 查詢給定文字檔案中重複次數最多的單詞。

假設我們有一個名為 ExampleTextFile.txt 的文字檔案,其中包含一些隨機文字。我們將返回給定文字檔案中重複次數最多的單詞。

ExampleTextFile.txt

Good Morning TutorialsPoint
This is TutorialsPoint sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome TutorialsPoint
Learn with a joy

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 匯入 Counter 函式(Counter 類是由 Python3 的 collections 模組提供的物件資料集的一種形式。Collections 模組向用戶公開專門的容器資料型別,作為 Python 通用內建函式(如字典、列表和元組)的替代方案。Counter 是一個子類,用於計算可雜湊物件。呼叫時,它會隱式建立可迭代雜湊表)來自 collections 模組

  • 建立一個變數來儲存文字檔案的路徑。

  • 建立一個列表來儲存所有單詞。

  • 使用 open() 函式(開啟檔案並返回檔案物件作為結果)以只讀模式開啟文字檔案,並將檔名和模式作為引數傳遞給它(此處“r”表示只讀模式)。

with open(inputFile, 'r') as filedata:
  • 使用 for 迴圈遍歷檔案中的每一行。

  • 使用 split() 函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格)將文字檔案內容拆分為單詞列表並將其儲存在一個變數中。

  • 使用 for 迴圈遍歷單詞列表。

  • 使用 append() 函式(將元素新增到列表的末尾),將每個單詞追加到列表中。

  • 使用 Counter() 函式(它以鍵值對的形式給出單詞的頻率),計算所有單詞的頻率(單詞出現的次數)。

  • 建立一個變數來儲存最大頻率。

  • 使用 for 迴圈迴圈遍歷上述單詞頻率字典。

  • 使用 if 條件語句和 in 關鍵字,檢查單詞的頻率是否大於最大頻率。

The in keyword works in two ways:
The in keyword is used to determine whether a value exists in a sequence (list, range, string etc).
It is also used to iterate through a sequence in a for loop
  • 如果單詞的頻率大於最大頻率。

  • 建立一個變數來儲存文字檔案中重複次數最多的單詞。

  • 列印文字檔案中重複次數最多的單詞。

  • 使用 close() 函式關閉輸入檔案(用於關閉已開啟的檔案)。

示例

以下程式遍歷文字檔案的行,並使用 collections 模組中的 counter 函式從文字檔案列印鍵值對的頻率 -

# importing Counter function from collections import Counter # input text file inputFile = "ExampleTextFile.txt" # Storing all the words newWordsList = [] # Opening the given file in read-only mode with open(inputFile, 'r') as filedata: # Traverse in each line of the file for textline in filedata: # Splitting the text file content into list of words wordsList = textline.split() # Traverse in the above list of words for word in wordsList: # Appending each word to the new list newWordsList.append(word) # Using the Counter() function, calculate the frequency of all the words wordsFrequency = Counter(newWordsList) # Taking a variable to store the maximum frequency value maxFrequency = 0 # Loop in the above words frequency dictionary for textword in wordsFrequency: # Checking whether the frequency of the word is greater than the maximum frequency if(wordsFrequency[textword] > maxFrequency): # If it is true then set maximum frequency to the corresponding frequency value of the word maxFrequency = wordsFrequency[textword] # As this is the word with maximum frequency store this word in a variable mostRepeatedWord = textword # Printing the most repeated word in a text file print("{",mostRepeatedWord,"} is the most repeated word in a text file") # Closing the input file filedata.close()

輸出

執行上述程式後,將生成以下輸出:

{ TutorialsPoint } is the most repeated word in a text file

在此程式中,我們從文字檔案中讀取一些隨機文字。我們讀取整個檔案,將其分解成單詞,並將文字檔案的所有單詞新增到列表中。我們使用 Counter() 方法計算文字檔案中所有單詞的頻率,它返回一個字典,其中鍵為單詞,值為單詞的頻率。然後我們遍歷字典中的單詞,檢查頻率是否大於最大頻率。如果是,則這是最常出現的單詞,因此我們將結果儲存在一個變數中,並使用當前單詞的頻率更新最大頻率。最後,我們顯示了最常出現的單詞。

結論

本文向我們展示瞭如何讀取檔案、逐行遍歷檔案以及檢索該行中的所有單詞。一旦我們得到它們,我們就可以反轉單詞、更改大小寫、檢查母音、檢索單詞長度等。我們還學習瞭如何使用 Counter() 方法確定單詞列表的頻率。此函式可用於確定字串、列表、元組等的頻率。

更新於: 2022年8月18日

9K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告