Difference between revisions of "Guinea Pig"

From Cohen Courses
Jump to navigationJump to search
Line 7: Line 7:
 
code:
 
code:
  
# always start like this
+
<pre>
from gp import *
+
# always start like this
import sys
+
from gp import *
 +
import sys
  
# supporting routines can go here
+
# supporting routines can go here
def tokens(line):
+
def tokens(line):
    for tok in line.split():
+
    for tok in line.split():
        yield tok.lower()
+
        yield tok.lower()
  
#always subclass Planner
+
#always subclass Planner
class WordCount(Planner):
+
class WordCount(Planner):
  
    wc = ReadLines('corpus.txt') | FlattenBy(by=tokens) | Group(by=lambda x:x, reducingWith=ReduceToCount())
+
    wc = ReadLines('corpus.txt') | FlattenBy(by=tokens) | Group(by=lambda x:x, reducingWith=ReduceToCount())
  
# always end like this
+
# always end like this
if __name__ == "__main__":
+
if __name__ == "__main__":
    WordCount().main(sys.argv)
+
    WordCount().main(sys.argv)
 +
</pre>

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)