viligen

word_count

Feb 2nd, 2022
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import re
  2. result_counts_words = {}
  3.  
  4. with open("input.txt", "r") as input_file:
  5.     text = input_file.read()
  6. with open("words.txt", "r") as words_file:
  7.     words = words_file.read().split()
  8.     for word in words:
  9.         count = len(re.findall(fr"\b{word}\b", text, re.IGNORECASE))
  10.         result_counts_words[word] = count
  11. sorted_words = sorted(result_counts_words.items(), key=lambda kvp: -kvp[1])
  12. with open("output.txt", "a") as output_file:
  13.     for word, count in sorted_words:
  14.         output_file.write(f"{word} - {count}\n")
  15.  
  16.  
  17.  
Advertisement
Add Comment
Please, Sign In to add comment