Difference between revisions of "Guinea Pig"

From Cohen Courses
Jump to navigationJump to search
(Created page with "= Quick Start = == Running wordcount.py == Set up a directory that contains the file <code>gp.py</code> and a second script called <code>wordcount.py</code> which contains t...")
 
Line 6: Line 6:
 
second script called <code>wordcount.py</code> which contains this
 
second script called <code>wordcount.py</code> which contains this
 
code:
 
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)

Revision as of 16: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)