Skip to content

Commit f5dcf83

Browse files
committed
add dict.py
1 parent 69540e5 commit f5dcf83

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

dict.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import turtle
2+
3+
count = 10
4+
data = []
5+
words = []
6+
yScale = 6
7+
xScale = 30
8+
9+
################# Turtle Start ####################
10+
def drawLine(t, x1, y1, x2, y2):
11+
t.penup()
12+
t.goto (x1, y1)
13+
t.pendown()
14+
t.goto (x2, y2)
15+
16+
def drawText(t, x, y, text):
17+
t.penup()
18+
t.goto (x, y)
19+
t.pendown()
20+
t.write(text)
21+
22+
def drawGraph(t):
23+
drawLine (t, 0, 0, 360, 0)
24+
drawLine (t, 0, 300, 0, 0)
25+
26+
for x in range(count):
27+
x=x+1
28+
drawText(t, x*xScale-4, -20, (words[x-1]))
29+
drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])
30+
drawBar(t)
31+
32+
def drawRectangle(t, x, y):
33+
x = x*xScale
34+
y = y*yScale
35+
drawLine(t, x-5, 0, x-5, y)
36+
drawLine(t, x-5, y, x+5, y)
37+
drawLine(t, x+5, y, x+5, 0)
38+
drawLine(t, x+5, 0, x-5, 0)
39+
40+
def drawBar(t):
41+
for i in range(count):
42+
drawRectangle(t, i+1, data[i])
43+
################# Turtle End ####################
44+
45+
46+
def processLine(line, wordCounts):
47+
line = replacePunctuations(line)
48+
words = line.split()
49+
for word in words:
50+
if word in wordCounts:
51+
wordCounts[word] += 1
52+
else:
53+
wordCounts[word] = 1
54+
55+
def replacePunctuations(line):
56+
for ch in line:
57+
if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
58+
line = line.replace(ch, " ")
59+
return line
60+
61+
def main():
62+
filename = input("enter a filename:").strip()
63+
infile = open(filename, "r")
64+
65+
wordCounts = {}
66+
for line in infile:
67+
processLine(line.lower(), wordCounts)
68+
69+
pairs = list(wordCounts.items())
70+
71+
items = [[x,y]for (y,x)in pairs]
72+
items.sort()
73+
74+
for i in range(len(items)-1, len(items)-count-1, -1):
75+
print(items[i][1]+"\t"+str(items[i][0]))
76+
data.append(items[i][0])
77+
words.append(items[i][1])
78+
79+
infile.close()
80+
81+
turtle.title('test')
82+
turtle.setup(900, 750, 0, 0)
83+
t = turtle.Turtle()
84+
t.hideturtle()
85+
t.width(3)
86+
drawGraph(t)
87+
88+
if __name__ == '__main__':
89+
main()

0 commit comments

Comments
 (0)