-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordsort.py
executable file
·96 lines (85 loc) · 5.42 KB
/
wordsort.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
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
#!/usr/bin/env python
import argparse # parse sys.argv for the target textfile to analyze
try:
parser = argparse.ArgumentParser() # create parser object
parser.add_argument('filename') # give the parser obj the file
args = parser.parse_args() # use string 'args' to create output file
with open(args.filename) as sourcefile: # open sourcefile
suffixstr = args.filename[-4:] # last 4 chars is ".txt"
prefixstr = args.filename[0:-4] # the chars before ".txt"
outputfilename = prefixstr + "-count" + suffixstr
print("\n" + "Analysing {}".format(args.filename))
print("Results saved in " + '"' + outputfilename + '"' + "\n")
data = sourcefile.read()
data = data.lower() # make everything lower case
for ch in '"''!@#$%^&*()-_=+,<.>/?;:[{]}~`\|':
data = data.replace(ch," ") # replace punctuation with whitespace
# do something with the data eg count & list words in freq order
wordDict = {} # create empty dictionary
for word in data.split():
if word not in wordDict:
wordDict[word] = 1 # add the word
else:
wordDict[word] = wordDict[word] + 1 # increment count
commonest = sorted(wordDict.items(), key=lambda x: (x[1], x[0]), reverse=True)
for word, freq in commonest:
print("%-13s %d" % (word, freq))
# write results
with open(outputfilename, "w") as outputfile:
args = ("Total words: ", len(data.split()), " Unique words: ", len(commonest))
summary = ("{} {} {} {}".format(*args))
print("\n" + summary + "\n")
outputfile.write(summary)
for word, freq in commonest:
outputfile.write("\n\n%-13s %d \n" % (word, freq))
except IOError as e:
print("Error: %s not found." % args.filename)
print("Usage>> ./wordsort.py sometext.txt")
# Python lists have a built-in sort() method that modifies the list in-placs
# and a sorted() built-in function that builds a new sorted list from an
# iterable the sorted() function returns a list, even if that is not the type
# that was passed in the iterable can be a dictionary. A dictionary is
# actually a mapping rather than a list - so, an unordered set of key:value
# pairs. Each key is unique within the dictionary the iterable here is all of
# the items contained in the dictionary wordDict, you iterate through the
# (word:no of occurrences of the word, key:value pairs) in wordDict the second
# parameter in the sorted function, the key parameter, should not be confused
# with the key in the key:value pairs in the dictionary, they are different
# things in the sorted function, the key (in this example) specifies (another)
# function (a lambda) to be called on each key:value mapping prior to making
# the sorting comparison x is all of the key:value mappings in wordDict, and
# x[1] just tells the sorted function to sort on the second element in each
# pair, e.g., the value, rather than x[0] the key if the values are the same
# then x[0] tellts the sorted function to sort on the first element in each
# pair, e.g., the word, alphabetically, but this is reversed by reverse = True
# i.e., pass a function that looks up the value associated with a key... to
# sort just the keys, based on their associated values, that's what lambda is
# doing here lambda's are anonymous functions. Function parameter(s) are
# specified before the colon. The function expression is specified after the
# colon. what is expressed is what is returned reverse = True returns largest
# to smallest order Commonest is the sorted list of words, sorted from most
# common to least common, extracted from wordDict
# Python lists have a built-in sort() method that modifies the list in-placs
# and a sorted() built-in function that builds a new sorted list from an
# iterable the sorted() function returns a list, even if that is not the type
# that was passed in the iterable can be a dictionary. A dictionary is
# actually a mapping rather than a list - so, an unordered set of key:value
# pairs. Each key is unique within the dictionary the iterable here is all of
# the items contained in the dictionary wordDict, you iterate through the
# (word:no of occurrences of the word, key:value pairs) in wordDict the second
# parameter in the sorted function, the key parameter, should not be confused
# with the key in the key:value pairs in the dictionary, they are different
# things in the sorted function, the key (in this example) specifies (another)
# function (a lambda) to be called on each key:value mapping prior to making
# the sorting comparison x is all of the key:value mappings in wordDict, and
# x[1] just tells the sorted function to sort on the second element in each
# pair, e.g., the value, rather than x[0] the key if the values are the same
# then x[0] tellts the sorted function to sort on the first element in each
# pair, e.g., the word, alphabetically, but this is reversed by reverse = True
# i.e., pass a function that looks up the value associated with a key... to
# sort just the keys, based on their associated values, that's what lambda is
# doing here lambda's are anonymous functions. Function parameter(s) are
# specified before the colon. The function expression is specified after the
# colon. what is expressed is what is returned reverse = True returns largest
# to smallest order Commonest is the sorted list of words, sorted from most
# common to least common, extracted from wordDict