This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wf.py
134 lines (115 loc) · 4.67 KB
/
wf.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
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
#!/usr/bin/python3
import argparse
import re
import os
from collections import defaultdict
def letter(path):
counts = defaultdict(lambda: 0)
with open(path, encoding='utf8') as f:
for c in f.read().lower():
if not c.isalpha():
continue
counts[c] += 1
yield 'File: %s\n' % path
result = sorted(counts.items(), key=lambda i: (-i[1], i[0]))
if limit is not None:
result = result[:limit]
for (k, v) in result:
yield '%40s\t%d\n' % (k, v)
yield '\n'
def word(path):
counts = defaultdict(lambda: 0)
with open(path, encoding='utf8') as f:
pattern = re.compile(r'(?<![a-zA-Z0-9])[a-zA-Z][a-zA-Z0-9]*')
s = f.read()
result = pattern.findall(s)
for w in result:
w = w.lower()
counts[verbs.get(w, w)] += 1
for w in stopwords:
counts.pop(w, None)
yield 'File: %s\n' % path
result = sorted(counts.items(), key=lambda i: (-i[1], i[0]))
if limit is not None:
result = result[:limit]
for (k, v) in result:
yield '%40s\t%d\n' % (k, v)
yield '\n'
def phrase(path):
counts = defaultdict(lambda: 0)
with open(path, encoding='utf8') as f:
pattern = re.compile(r'[a-zA-Z0-9\s]+')
word_p = re.compile(r'(?<![a-zA-Z0-9])[a-zA-Z][a-zA-Z0-9]*')
s = f.read()
for sen in pattern.findall(s):
words = []
for w in word_p.findall(sen):
w = w.lower()
if w not in stopwords:
words.append(verbs.get(w, w))
for i in range(len(words) - phrase_len + 1):
counts[tuple(words[i:i + phrase_len])] += 1
yield 'File: %s\n' % path
result = sorted(counts.items(), key=lambda x: (-x[1], x[0]))
if limit is not None:
result = result[:limit]
for (k, v) in result:
yield '%40s\t%d\n' % (' '.join(k), v)
yield '\n'
class PhraseAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, 'function', self.const)
setattr(namespace, self.dest, values)
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument('paths', metavar='<path>', nargs='+',
help='File to process.')
parser.add_argument('-f', dest='function', action='store_const', const=word,
help='Count word occurrences.')
parser.add_argument('-c', dest='function', action='store_const', const=letter,
help='Count character occurrences.')
parser.add_argument('-p', dest='phrase_len', action=PhraseAction, const=phrase, metavar='<len>', type=int,
help='Count phrase occurrences, treating any <len> contiguous words as a phrase.')
parser.add_argument('-n', dest='limit', metavar='<num>', type=int,
help='Output only the top <num> items.')
parser.add_argument('-x', dest='stopwords', metavar='<stop-words>',
help='Use <stop-words> as a list of stop words, which are ignored in the counting.'
' Effective only with -f or -p specified.')
parser.add_argument('-v', dest='verbs', metavar='<verb-dict>',
help='Use <verb-dict> as a verb dictionary that can be used with -p or -q.')
parser.add_argument('-d', dest='directory', action='store_true',
help='Treat <path> as the path to a directory and operate on each file inside the directory.')
parser.add_argument('-s', dest='recursive', action='store_true',
help='Recurse into sub-directories. Must be used with -d.')
args = parser.parse_args(args)
global phrase_len
phrase_len = args.phrase_len
global limit
limit = args.limit
global stopwords
stopwords = set()
if args.stopwords is not None:
with open(args.stopwords, encoding='utf8') as f:
for line in f:
stopwords.add(line.strip().lower())
global verbs
verbs = {}
if args.verbs:
with open(args.verbs, encoding='utf8') as f:
for line in f:
verb, _, words = line.strip().partition(' -> ')
for w in words.split(','):
verbs[w.lower()] = verb.lower()
for path in args.paths:
if args.directory:
for (p, _, l) in os.walk(path):
for i in l:
yield from args.function(i)
if not args.recursive:
break
else:
yield from args.function(path)
if __name__ == '__main__':
from sys import argv
for output in main(argv[1:]):
print(output, end='')