Skip to content

Instantly share code, notes, and snippets.

@henriquebastos
Created June 4, 2019 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save henriquebastos/20ea240e3e5076821ad144abd877d7e6 to your computer and use it in GitHub Desktop.
Save henriquebastos/20ea240e3e5076821ad144abd877d7e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/
"""Mimic pyquick exercise -- optional extra exercise.
Google's Python Class
Read in the file specified on the command line.
Do a simple split() on whitespace to obtain all the words in the file.
Rather than read the file line by line, it's easier to read
it into one giant string and split it once.
Build a "mimic" dict that maps each word that appears in the file
to a list of all the words that immediately follow that word in the file.
The list of words can be be in any order and should include
duplicates. So for example the key "and" might have the list
["then", "best", "then", "after", ...] listing
all the words which came after "and" in the text.
We'll say that the empty string is what comes before
the first word in the file.
With the mimic dict, it's fairly easy to emit random
text that mimics the original. Print a word, then look
up what words might come next and pick one at random as
the next work.
Use the empty string as the first word to prime things.
If we ever get stuck with a word that is not in the dict,
go back to the empty string to keep things moving.
Note: the standard python module 'random' includes a
random.choice(list) method which picks a random element
from a non-empty list.
For fun, feed your program to itself as input.
Could work on getting it to put in linebreaks around 70
columns, so the output looks better.
"""
import argparse
import random
from collections import defaultdict
def read_words(file):
return file.read().split()
def glossary(words):
"""Returns mimic dict mapping each word to list of words which follow it."""
first, last, empty = words[0], words[-1], ''
d = defaultdict(list, {
empty: [first],
last: [empty],
})
for w, n in zip(words[:-1], words[1:]):
d[w].append(n)
return d
def mimic(d, first=''):
"""Given mimic dict and start word, prints 200 random words."""
l = []
w = first
for _ in range(200):
w = random.choice(d[w])
l.append(w)
return ' '.join(l)
# Provided main(), calls mimic_dict() and mimic()
def main():
parser = argparse.ArgumentParser(description='Gerador de lero-lero.')
parser.add_argument('file', type=argparse.FileType())
args = parser.parse_args()
print(mimic(glossary(read_words(args.file))))
if __name__ == '__main__':
main()
@ocvs
Copy link

ocvs commented Jun 4, 2019

Top. Funcionou perfeito no Windows também!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment