Created
October 4, 2017 17:03
-
-
Save sloat/33155be3faf3367209ef479cc381a29e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
import nltk | |
import os | |
import pickle | |
from nltk.corpus import brown | |
import click | |
@click.command() | |
@click.argument('minlength', type=click.INT, default=30) | |
def main(minlength): | |
if os.path.exists(os.path.expanduser('~/bin/brown_pos.pkl')): | |
words_pos = pickle.load(open(os.path.expanduser('~/bin/brown_pos.pkl'), | |
'rb')) | |
else: | |
words_pos = {} | |
for word, tag in brown.tagged_words(): | |
if tag in words_pos: | |
words_pos[tag].append(word) | |
else: | |
words_pos[tag] = [word] | |
pickle.dump(words_pos, open(os.path.expanduser('~/bin/brown_pos.pkl'), 'wb')) | |
newpara = [] | |
while len(newpara) < minlength: | |
paragraph = random.choice(brown.tagged_paras())[0] | |
for word, tag in paragraph: | |
w = random.choice(words_pos[tag]) | |
newpara.append(w) | |
output = ' '.join(newpara) | |
output = output.replace(' ,', ',').replace(' .', '.').replace(' ?', '?').\ | |
replace('`` ', '"').replace(" ''", '"').replace(' ;', ';').\ | |
replace(' !', '!') | |
click.echo(output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment