168k views
2 votes
Use Python programming to count all unique words for all files in a given directory. The output must be words and their corresponding frequency. Note: • To get all files under a directory, use os.listdir(directory name) • To check if a character is an alpha character, you can use the following example expression (3 is not a word character): o >>> str = "hello3hello" o >>> str = str.lower() # to be all lowercase first o >>> str[5] > ‘a’ and str[5] < ‘z’ o >>> False • To debug your code, please create a folder with at least 3 text files. You don’t need to submit your example folder and text files.

1 Answer

3 votes

Answer:

import re

word_Frequency = {}

#ITime to input

input_file = open('input.txt', 'r')

#The program considers UpperCase and LowerCase words as different

input_string = input_file.read()

#Searching all words containing [a-z]/[A-z] value as string

all_match_words = re.findall(r'\b[a-zA-Z]{1,15}\b', input_string)

for word in all_match_words:

count = word_Frequency.get(word,0)

word_Frequency[word] = count + 1

frequency_list = word_Frequency.keys()

for words in frequency_list:

print(words,word_Frequency[words])

User Leandro Tuttini
by
5.8k points