Last active
August 29, 2015 13:57
-
-
Save christianp/9810662 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
Other nonpolar boathouses | |
by Unmellifluously Foreseeable | |
Nonmobility blasphemed borchers nonequivocally, | |
Amid onto refuelling tightroped spiers, | |
Punctured lutanist nonprudently overharshly twistingly nonpestilential some, | |
Rive soothing regorged notwithstanding itinerantly boundingly nonextricably mendeleev! | |
Foreseeing graveled swirl unchancy whichever enormously, | |
Fruitlessly along attrited influence regorged pettit broilingly autarchically jacketless. | |
Various fimbriated cabled beyond suboptically surreptitiously turpentine, | |
Carved fetch calisthenic my curiously nonchromatically? | |
Other, |
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
import re | |
from random import choice,randrange | |
from optparse import OptionParser | |
re_part = re.compile('(?P<word>\w*)\t\|?(?P<part>.)') | |
class VerboseBF: | |
bf_to_part = { | |
'<': 'v', | |
'>': 'V', | |
'+': 'N', | |
'-': 'A', | |
'[': 'P', | |
']': 'D' | |
} | |
part_to_bf = {v:k for k,v in bf_to_part.items()} | |
def __init__(self): | |
words = open('part-of-speech.txt').read().split('\n')[:-1] | |
common_words = open('1000.dicin.txt').read().split('\n')[:-1] | |
ms = [re_part.match(w) for w in words] | |
ms = [m.groups() for m in ms if m] | |
all_parts = self.all_parts = {} | |
for word,part in ms: | |
l = all_parts.setdefault(part,[]) | |
l.append(word.lower()) | |
common_parts = self.common_parts = {} | |
for word in common_words: | |
part = self.key_for(word) | |
l = common_parts.setdefault(part,[]) | |
l.append(word) | |
def key_for(self,word): | |
for key,list in self.all_parts.items(): | |
if word in list: | |
return key | |
def bf_to_words(self,program,simple=False): | |
parts = self.common_parts if simple else self.all_parts | |
out = [choice(parts[self.bf_to_part[command]]) for command in program] | |
words = '' | |
while len(out): | |
l = min(randrange(3,10),len(out)) | |
sentence, out = out[:l],out[l:] | |
words += ' '.join(sentence).capitalize()+choice(',.?!')+'\n' | |
words=words[:-1] | |
header = """%s %s %s\nby %s %s\n\n""" % (choice(parts['D']).capitalize(),choice(parts['A']),choice(parts['p']),choice(parts['v']).capitalize(),choice(parts['A']).capitalize()) | |
return header,words | |
def words_to_bf(self,sentence): | |
sentence = sentence.replace('\n',' ') | |
sentence = re.sub('[,.?!]','',sentence).lower() | |
words = sentence.split(' ') | |
parts = [self.key_for(word) for word in words] | |
commands = [self.part_to_bf[part] for part in parts if part in self.part_to_bf.keys()] | |
return ''.join(commands) | |
if __name__ == '__main__': | |
usage = """usage: %prog command program | |
Commands: | |
words - translate a BF program to words | |
bf - translate words to a BF program | |
Examples: | |
%prog words ">>[++]<" | |
produces a nice poem. | |
While | |
%prog bf "Quickly run to the shop." | |
produces | |
"<>[]+\"""" | |
parser = OptionParser(usage=usage) | |
parser.add_option('-s','--simple',action='store_true',dest='simple',default=False,help='Use only the thousand most common English words') | |
options, args = parser.parse_args() | |
try: | |
command = args[0] | |
program = args[1] | |
except IndexError: | |
parser.print_help() | |
exit(0) | |
verb = VerboseBF() | |
if command == 'words': | |
header,poem = verb.bf_to_words(program,simple=options.simple) | |
print(header+poem) | |
elif command == 'bf': | |
print(verb.words_to_bf(program)) | |
else: | |
parser.print_help() | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment