Skip to content

Instantly share code, notes, and snippets.

@alexanderchuranov
Created March 31, 2015 19:06
Show Gist options
  • Select an option

  • Save alexanderchuranov/a3de4362eb62606c86fe to your computer and use it in GitHub Desktop.

Select an option

Save alexanderchuranov/a3de4362eb62606c86fe to your computer and use it in GitHub Desktop.
Calculates number of occurrences of words and groups of words in the input
#!/usr/bin/python
# Usage:
#
# words-stat word-freq
# words-stat group-freq <group-size>
#
# word-freq
#
# Calculates number of occurrences of each word in the input
#
# group-freq <group-size>
#
# Calucalates number of occurrences of groups of words of the
# specified size in the input. Groups that occur less than 2 times
# are not reported.
# Original:
#
# sed 's/[.,] / /g'
# | tr ' \f\v\t\r\n' '\n'
# | grep -v '[^a-zA-Z]'
# | tr '\n' ' '
# | awk '{ for (i = 1; i < NF; ++i) { j = i + 1; print $i " " $j; } }'
# | tr 'A-Z' 'a-z' | sort | uniq -c
from collections import deque
import string
import sys
class WordFrequency:
def __init__(self):
self.counts = {}
def consume(self, word):
if word not in self.counts:
self.counts[word] = 0
self.counts[word] = self.counts[word] + 1
def report(self, lower_bound=0):
l = filter(
lambda pair: pair[0] > lower_bound,
zip(self.counts.values(), self.counts.keys()))
l.sort()
for count, word in l:
print count, word
class GroupFrequency(WordFrequency):
def __init__(self, size=2):
WordFrequency.__init__(self)
self.prev = []
self.size = size
def consume(self, word):
self.prev.append(word)
if len(self.prev) > self.size:
self.prev = self.prev[1:]
if len(self.prev) == self.size:
WordFrequency.consume(self, string.join(self.prev))
def report(self):
WordFrequency.report(self, 1)
def remove_punctuation(word):
lastchar = word[len(word) - 1]
if lastchar == ',' or lastchar == '.':
word = word[:len(word) - 1]
return word
def process_input(op):
for line in sys.stdin:
for word in line.split():
word = remove_punctuation(word)
if word.isalpha():
op.consume(word.lower())
op.report()
def parse_group_frequency_command(argv):
if len(argv) == 0:
raise Exception('Usage: group-freq <group-size>')
size = argv[0]
if not size.isdigit():
raise Exception('expected an integer, got "' + size + '"')
return GroupFrequency(int(size))
def parse_command(argv):
if (len(argv) == 0):
raise Exception('Usage: word-stat <measurement-name>')
cmd = argv[0]
if cmd == "word-freq":
return WordFrequency()
elif cmd == "group-freq":
return parse_group_frequency_command(argv[1:])
else:
raise Exception('Unknown command "' + cmd + '"')
# main
op = parse_command(sys.argv[1:])
process_input(op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment