예제 #1
0
def generate_phrase_1():
    selections = [VERBS, ADJECTIVES, NOUNS, ADVERBS, TRANSITIVE_VERBS, VERBS, ADJECTIVES, NOUNS]
    entropy = sum([log(len(item), 2) for item in selections])
    conjugations = ['part', None, None,
                    None, [random_item_from_list([PAST, PRESENT])],
                    'part', None, None]
    entropy += 1
    print('%.2f bits of entropy' % entropy)
    sub_list = [random_item_from_list(item) for item in selections]
    for idx, word in enumerate(sub_list):
        if conjugations[idx]:
            sub_list[idx] = conjugate(word, *conjugations[idx])

    return ('the %s %s %s %s %s the %s %s %s' % tuple(sub_list)).replace('_', ' ')
예제 #2
0
def generate_phrase_2():
    '''Return a phrase and its entropy (in bits) of the form
       (# adj noun) (adverb verb) (adjective noun punctuation)

    E.g.,
       17 MODERATE TRAYS At once live outed wORTH bOSSES
    '''
    selections = [ADJECTIVES, NOUNS,
                  ADVERBS, TRANSITIVE_VERBS,
                  ADJECTIVES, NOUNS, TERMINAL_PUNCTUATION]
    entropy = sum([log(len(item), 2) for item in selections])
    conjugations = [None, None,
                    None, [random_item_from_list([PAST, PRESENT]), 3, PLURAL],
                    None, None,
                    None]
    sub_list = [random_item_from_list(item) for item in selections]
    for idx, word in enumerate(sub_list):
        if conjugations[idx]:
            sub_list[idx] = conjugate(word, *conjugations[idx])
    entropy += 1

    sub_list[1] = pluralize(sub_list[1])
    sub_list[5] = pluralize(sub_list[5])

    entropy += log(997, 2)

    for idx, item in enumerate(sub_list):
        rnd = randint(4)
        if rnd == 1:
            sub_list[idx] = item.capitalize()
        if rnd == 2:
            sub_list[idx] = item.upper()
        if rnd == 3:
            sub_list[idx] = item[0] + item[1:].upper()

        entropy += 2

    phrase = ('%i %s %s %s %s %s %s%s' % tuple([randint(997) + 2] + sub_list)).replace('_', ' ')

    # Insert a random symbol into the sentence
    insert_point = randint(len(phrase) + 1)
    entropy += log(len(phrase) + 1, 2) + log(len(SYMBOLS), 2)
    phrase = phrase[:insert_point] + random_item_from_list(SYMBOLS) + phrase[insert_point:]

    insert_point = randint(len(phrase) + 1)
    entropy += log(len(phrase) + 1, 2) + log(len(SYMBOLS), 2)
    phrase = phrase[:insert_point] + random_item_from_list(SYMBOLS) + phrase[insert_point:]

    return phrase, entropy