Beispiel #1
0
def goGoPaparazzo():

    # get a pretty date time string
    timestamp = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")

    hour = datetime.now().hour
    #if hour < 8 or hour > 17:
    #    print "Yawn, I'm asleep. Wake me up when it's daytime"
    #    return

    print "Activating Paparazzo at", timestamp

    grammar = Grammar.from_file("grammar.txt")
    while True:
        message = grammar.generate()
        if len(message) < 252:  # allow space for picture URL
            break
    print "Message:", message

    # capture image to capture.jpg
    call(["./capture-image.sh"], shell=True)

    # stop here if image capture failed
    if not os.path.exists("capture.jpg"):
        print "Error - no capture.jpg recorded"
        return

    # post to twitter
    twitter = Twython(app_key=settings.app_key,
                      app_secret=settings.app_secret,
                      oauth_token=settings.oauth_token,
                      oauth_token_secret=settings.oauth_token_secret)
    twitter.update_status_with_media(status=message,
                                     media=open("capture.jpg", 'rb'))

    # archive the image and text
    shutil.move("capture.jpg", "history/%s.jpg" % timestamp)
    with open("history/%s.txt" % timestamp, "w") as f:
        f.write("%s\n" % message)
Beispiel #2
0
def main(args):
    if args.max_length:
        # Generate words in language
        max_length = int(args.max_length)
        words = Generator(Grammar.from_file(
            args.grammar_file)).brute_force_generator(max_length)
        print(' '.join(words))
        return

    if not args.word and not args.word_file:
        print('Please specify one of --word_file or --word argument')
        return
    word = args.word
    if not word:
        with open(args.word_file) as f:
            word = f.read()
    try:
        if predict(args.grammar_file, word):
            print('The word belongs to grammar :)')
        else:
            print('The word does not belong to grammar :(')
    except GrammarIncorrectException as e:
        print(f'Incorrect grammar: {str(e)}')
Beispiel #3
0
from grammar import Grammar

grammar = Grammar.from_file("jokes.txt")

print(grammar.generate())
Beispiel #4
0
def predict(grammar_file: str, word: str):
    word = word.strip().replace(' ', '')
    grammar = Grammar.from_file(grammar_file)
    algo = Algo()
    algo.fit(grammar)
    return algo.predict(word)