Word Frequency

Back to homepage

Project Overview

Create a program to analyze the frequency of words in a string. You could do a lot of different things; print out how often each word occurs, detect the language of the input, even make a visual word cloud. Be creative with it!

This challenge started 2021 Sep 21 and ended 2021 Sep 28.

Submissions

Kat Crawford

import matplotlib.pyplot as plt
import numpy as np

a = input("Enter a string: ")

characters = [",",".","!","?"]

def parse_string(character, temp_a):
    a_list = temp_a.split(character)
    string1 = ""
    for i in a_list:
        string1 += i
    return string1

current_string = a

for i in characters:
    current_string = parse_string(i,current_string)

word_list = current_string.split(" ")

frequency = {}
for item in word_list:
    if (item in frequency):
        frequency[item] += 1
    else:
        frequency[item] = 1

plt.rcdefaults()
fig, ax = plt.subplots()

words = frequency.keys()
y_pos = np.arange(len(words))
performance = frequency.values()
error = np.random.rand(len(words))

ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(words)
ax.invert_yaxis()
ax.set_xlabel('Times Written')
ax.set_title('Frequency of Words')

plt.show()

Bennett Griggs

text = raw_input("Enter string: ").lower()
new_text = ""
for x in text:
    if x.isalnum() or x.isspace():
            new_text = new_text + x
            split_text = new_text.split(" ")
            dictionary = {}
            for x in split_text:
                try:
                        dictionary[x] += 1
                            except:
                                    dictionary[x] = 1
                                    print(dictionary)