Difference between revisions of "Guinea Pig"
From Cohen Courses
Jump to navigationJump to search| Line 7: | Line 7: | ||
code: | code: | ||
| − | + | <pre> | |
| − | + | # always start like this | |
| − | + | from gp import * | |
| + | import sys | ||
| − | + | # supporting routines can go here | |
| − | + | def tokens(line): | |
| − | + | for tok in line.split(): | |
| − | + | yield tok.lower() | |
| − | + | #always subclass Planner | |
| − | + | class WordCount(Planner): | |
| − | + | wc = ReadLines('corpus.txt') | FlattenBy(by=tokens) | Group(by=lambda x:x, reducingWith=ReduceToCount()) | |
| − | + | # always end like this | |
| − | + | if __name__ == "__main__": | |
| − | + | WordCount().main(sys.argv) | |
| + | </pre> | ||
Revision as of 15:13, 9 May 2014
Quick Start
Running wordcount.py
Set up a directory that contains the file gp.py and a
second script called wordcount.py which contains this
code:
# always start like this
from gp import *
import sys
# supporting routines can go here
def tokens(line):
for tok in line.split():
yield tok.lower()
#always subclass Planner
class WordCount(Planner):
wc = ReadLines('corpus.txt') | FlattenBy(by=tokens) | Group(by=lambda x:x, reducingWith=ReduceToCount())
# always end like this
if __name__ == "__main__":
WordCount().main(sys.argv)