forked from GezoKun/OOP-201920
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4_word_cloud.py
67 lines (55 loc) · 2.05 KB
/
lab4_word_cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# course: Object-oriented programming, year 2, semester 1
# academic year: 201920
# author: B. Schoen-Phelan
# date: 10-10-2019
# purpose: Lab 4
class WordCloud:
# initialises everything
# everything gets kicked off here
def __init__(self):
self.my_dict = self.create_dict()
# you might like to run the following line only
# if there wasn't a problem creating the dictionary
self.create_html(self.my_dict)
# this function creates the actual html file
# takes a dictionary as an argument
# it helps to multiply the key/occurance of word number with 10
# in order to get a decent size output in the html
def create_html(self, the_dict):
fo = open("output.html", "w")
fo.write('<!DOCTYPE html>\
<html>\
<head lang="en">\
<meta charset="UTF-8">\
<title>Tag Cloud Generator</title>\
</head>\
<body>\
<div style="text-align: center; vertical-align: middle; font-family: arial; color: white; background-color:black; border:1px solid black">')
# your code goes here!
fo.write('<span style="font-size: 10px"> HELLO </span>')
fo.write('</div>\
</body>\
</html>')
# opens the input file gettisburg.txt
# remember to open in in the correct mode
# reads the file line by line
# creates the dictionary containing the word itself
# and how often it occurs in a sentence
# makes a call to add_to_dict where the dictionary
# is actually populated
# returns a dictionary
def create_dict(self):
my_dict = {}
# your code goes here:
return my_dict
# helper function that is called from
# create_dict
# receives a word and a dictionary
# does the counting of the key we are at and adds 1
# if this word already exists. Otherwise sets the
# word occurance counter to 1
# returns a dictionary
def add_to_dict(self, word, the_dict):
# your code goes here
return the_dict
wc = WordCloud()