-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcal.py
More file actions
249 lines (182 loc) · 5.65 KB
/
Copy pathwordcal.py
File metadata and controls
249 lines (182 loc) · 5.65 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
import sys, os
from nltk import word_tokenize
from nltk.corpus import stopwords
from optparse import OptionParser
from collections import Counter
def convert_File_to_Array(filename): # Makes an array containing the file in an array.
array = []
for line in filename.readlines():
array.append(line.strip("\n"))
return array
def convert_File_to_Words(filename):
# Add stopwords in this function to filter out low information words.
array = word_tokenize(filename.read().lower())
return array
def sorting_algorithm(array): # Makes a sorted array out of the previous array.
sortedlist = []
for element in array:
i = 0
while i < len(element):
sortedlist.append(element[i])
i+=1
return sortedlist
def noduplicate(array): # Makes it so that the array has no duplicates.
dict = {}
i = 0
noduplist = []
while i < len(array):
if array[i] in noduplist:
dict[array[i]] += 1
else:
dict[array[i]] = 1
noduplist.append(array[i])
i+=1
return dict
def analyze_data(array): # Calculates percentage of the text.
print("Character:\tAmount:\tPercentage:\n")
totalsum = 0;
for element in sorted(array):
totalsum += array[element]
for element in sorted(array):
print("'" + element + "'","\t\t", array[element], "\t", (array[element]/totalsum)*100.0)
def wordanalysis(filename, style):
if filename:
# user_selection(filename)
f = open(filename, 'r')
words = Counter(convert_File_to_Words(f))
# print(words.most_common(10))
outputfile = open('output.txt', style)
word_count = 0
for i in words.most_common(len(words)):
if "," in i[0]:
pass
elif "." in i[0]:
pass
elif ")" in i[0]:
pass
elif "(" in i[0]:
pass
elif "'" in i[0]:
pass
elif "`" in i[0]:
pass
elif ":" in i[0]:
pass
elif ";" in i[0]:
pass
elif "{" in i[0]:
pass
elif "}" in i[0]:
pass
elif ">" in i[0]:
pass
elif "<" in i[0]:
pass
elif "=" in i[0]:
pass
elif "^" in i[0]:
pass
elif "[" in i[0]:
pass
elif "]" in i[0]:
pass
elif "-" in i[0]:
pass
elif "_" in i[0]:
pass
elif i[0].isdigit():
pass
else:
# print(i[0], "-", i[1])
word_count += i[1]
print("Word Count:", word_count)
for i in words.most_common(len(words)):
if "," in i[0]:
pass
elif "." in i[0]:
pass
elif ")" in i[0]:
pass
elif "(" in i[0]:
pass
elif "'" in i[0]:
pass
elif "`" in i[0]:
pass
elif ":" in i[0]:
pass
elif ";" in i[0]:
pass
elif "{" in i[0]:
pass
elif "}" in i[0]:
pass
elif ">" in i[0]:
pass
elif "<" in i[0]:
pass
elif "=" in i[0]:
pass
elif "^" in i[0]:
pass
elif "[" in i[0]:
pass
elif "]" in i[0]:
pass
elif "-" in i[0]:
pass
elif "_" in i[0]:
pass
elif i[0].isdigit():
pass
else:
# print(i[0], "-", i[1])
string = str(i[0]) + "," + str(i[1]) + "," + str(round(i[1]/word_count*100, 4))
outputfile.write(string)
outputfile.write("\n")
f.close()
else:
user_selection()
def user_selection(filename=False): # Decide what to do with the program.
print("\n\nDeveloper: Salman Hossain\nVersion: 1.0.1a\n\n--------------WordCal--------------\n\n1) Analyze a file\n2) Quit Program\n\n What would you like to do?\n")
user_choice = input(" > ")
if (user_choice == '1'):
if filename != False:
userfile = filename
else:
print("What is the file that you want to analyze?")
userfile = input(" > ")
elif (user_choice == '2'):
print("Exiting Program...")
sys.exit(1)
else:
print("ERROR: Invalid Option.")
user_selection()
try: # Part that does all the analyzing
filename = open(userfile, 'r')
except IndexError:
print("ERROR: You have provided an invalid filename.")
sys.exit(1)
newlist = convert_File_to_Array(filename)
sortedlist = sorting_algorithm(newlist)
noduplist = noduplicate(sortedlist)
# print(newlist)
# print(sortedlist)
# print(noduplist)
analyze_data(noduplist)
wordanalysis(userfile, 'w')
filename.close()
def main():
parser = OptionParser()
parser.add_option("-f", "--filename")
parser.add_option("-s", "--style", default='w')
options, arguments = parser.parse_args()
filename = options.filename
style = options.style
if filename:
wordanalysis(filename, style)
else:
user_selection()
if __name__=="__main__":
main()