-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcount_words.py
More file actions
37 lines (22 loc) · 775 Bytes
/
Copy pathcount_words.py
File metadata and controls
37 lines (22 loc) · 775 Bytes
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
import sys, re
from collections import Counter
most_common = True
num_words = 10
counter = Counter(word.lower()
for line in sys.stdin
for word in line.strip().split() if word)
for word, count in counter.most_common(num_words) if most_common else list(reversed(counter.most_common())):
sys.stdout.write(str(count))
sys.stdout.write("\t")
sys.stdout.write(word)
sys.stdout.write("\n")
#these supposed to be another separated example...
regex = sys.argv[1]
count_lines, count_words = 0, 0
for line in sys.stdin:
print 'something'
count_lines += 1
if re.search(regex, line):
count_words += 1
#sys.stdout.write(line)
print count_words, 'times', sys.argv[1], 'in', count_lines, 'lines'