I got this code that prints the most common words of a txt file. I want it to print and count words with '
in them. How can I do this?
words = open(input('Enter the name of the file: ')).read().lower().split()
number_of_words = int(input('Enter how many top words you want to see: '))
uniques = []
stop_words = ["a", "an", "and", "in", "is", "the"]
for word in words:
check_special = False
if word.isalnum():
check_special = True
if word not in uniques and word not in stop_words and check_special:
uniques.append(word)
counts = []
for unique in uniques:
count = 0
for word in words:
if word == unique:
count += 1
counts.append((count, unique))
counts.sort()
counts.reverse()
counts_dict = {count: [] for count, word in counts}
for count, word in counts:
counts_dict[count].append(word)
count_num_word = 0
for count in counts_dict:
if count_num_word >= number_of_words:
break
print('The following words appeared %d times each: %s' % (count, ', '.join(sorted(counts_dict[count]))))
count_num_word += 1
word.isalnum()
, since it will exclude words with punctuation in them.'
, you'll need to add a check that all the non-alphanumeric characters are'
.