#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Co-routines quick question code fix


    This is supposed to do the following:

    generators2.py: please do the following:
    Implement a generator, wordsFromFile(fileName) that produces each word of a file, assuming words are space delimited.
    Implement a generator, noPunctuation(wordGenerator) that draws words, which possibly include punctuation, from wordGenerator, and removes any punctuation (commas, periods, dashes, colons, or semi-colons). It should draw only one word from wordgenerator and emit only one word for each request it receives.
    Implement a generator, allCaps(wordGenerator) that draws words from wordGenerator, and ensures thata ll letters are capitalized. It should draw only one word from wordgenerator and emit only one word for each request it receives.
    Instantiate each of these three generators to form a pipeline that reads words from a file, removes punctuation, capitalizes them, and adds them to a Set of words. It should then print the set of words.


    Within a file called coroutines2.py, repeate the exercise described for generators2.py, but use a pipeline of coroutines. In effect, the "first" corroutine in the pipeline will strip punctuation and the last will print the word. You can read the words from the file however you'd like -- including by using the generator you wrote earlier


    For some reason, my code isnt replacing the punctuation. help?

    Code:
    #coroutines2.py
    def printWord():
      while True:
        word = (yield)
        print word
    
    def capatalize(targetCR):
      while True:
        word = (yield)
        targetCR.send(word.upper())
    
    def noPunct(targetCR):
      while True:
        word = (yield)
        targetCR.send(word.replace(("." or "," or ":" or ";" or "-"),""))
    
    pw = printWord(); pw.next()
    c = capatalize(pw); c.next()
    np = noPunct(c); np.next()
    
    
    sentence = open("fox.txt")
    data = sentence.read()
    sentence.close()
    for word in data.split():
            np.send(word)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    thanks for the help
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    ("." or "," or ":" or ";" or "-") is a logical expression that evaluates as '.' because "." is True.

    Try
    Code:
    import re, functools
    
    remove_punctuation = functools.partial(re.compile('[.,:;-]').sub,'')
    
    #...
        targetCR.send(remove_punctuation(word))
    There, I've never used (not in the last 8 years, anyway) functools.partial.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Thanks! Also, if i wanted to make the words all be placed into a set, how can i accomplish that?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    If L is iterable,
    set(L) # makes a set of the objects in L
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo