def generate_phrase(self):
     random_length = randrange(self.min_words, self.max_words + 1)
     # filtered_words = [w for w in words if en.is_noun(w)] #filter nouns
     selection = []
     for i in range(random_length):
         word = choice(self.blackboard.pool.nouns)
         selection.append(word)
     return en.quantify(selection)  # make enumeration
Пример #2
0
    def lookAround(self):
        """Print a description of the scene location, including a list
		of all the items it contains."""
        readable = termToReadable(self.location)
        print("You are in a {}.".format(readable), end=" ")

        describeLocation(self.location)

        item_names = [termToReadable(x) for x in self.items]
        print("There's {} in the {}.".format(en.quantify(item_names),
                                             readable))
Пример #3
0
 def test_quantify(self):
     # Assert quantification algorithm.
     for a, s in (
       (   2 * ["carrot"], "a pair of carrots"),
       (   4 * ["carrot"], "several carrots"),
       (   9 * ["carrot"], "a number of carrots"),
       (  19 * ["carrot"], "a score of carrots"),
       (  23 * ["carrot"], "dozens of carrots"),
       ( 201 * ["carrot"], "hundreds of carrots"),
       (1001 * ["carrot"], "thousands of carrots"),
       ({"carrot": 4, "parrot": 2}, "several carrots and a pair of parrots")):
         self.assertEqual(en.quantify(a), s)
     print("pattern.en.quantify()")
Пример #4
0
 def test_quantify(self):
     # Assert quantification algorithm.
     for a, s in (
       (   2 * ["carrot"], "a pair of carrots"),
       (   4 * ["carrot"], "several carrots"),
       (   9 * ["carrot"], "a number of carrots"),
       (  19 * ["carrot"], "a score of carrots"),
       (  23 * ["carrot"], "dozens of carrots"),
       ( 201 * ["carrot"], "hundreds of carrots"),
       (1001 * ["carrot"], "thousands of carrots"),
       ({"carrot": 4, "parrot": 2}, "several carrots and a pair of parrots")):
         self.assertEqual(en.quantify(a), s)
     print "pattern.en.quantify()"
Пример #5
0
def random_imperative(noun=None, get_related=True, verb=None, adj=None):
    if noun:
        n = get_related_or_not(noun, True, 'NN') if get_related else noun
    else:
        n = random.choice(NOUNS)
    if verb:
        v = get_related_or_not(verb, True, 'VB')
        if v is None:
            v = verb
    else:
        v = random.choice(VERBS)
    if not adj:
        adj = random.choice(ADJS) if coin_flip(0.5) else ''
    c = ''

    if coin_flip(0.7):
        n = pluralize(n)
        c = random.choice(C2)
    else:
        i = random.randint(1, 5)
        n = quantify(adj + ' ' + n, amount=i)
        adj = ''

    if coin_flip(0.25):
        a = ''
        v = conjugate(v)
    elif coin_flip(0.33):
        v = conjugate(v, 'part') # present participle
        a = random.choice(A1)
        c = random.choice(C)
    elif coin_flip(0.5):
        v = conjugate(v)
        a = random.choice(A2)
    else:
        v = conjugate(v)
        a = random.choice(A3)
    
    phrase = '{0} {1} {2} {3} {4}'.format(a, v, c, adj, n)
    phrase = phrase[1:] if phrase.startswith(' ') else phrase
    return re.sub(' +', ' ', phrase)
Пример #6
0
from pattern.en import suggest
print(suggest("Fracture"))

# ### Working with Numbers

from pattern.en import number, numerals

print(number("one hundred and twenty two"))
print(numerals(256.390, round=2))

from pattern.en import quantify

print(
    quantify([
        'apple', 'apple', 'apple', 'banana', 'banana', 'banana', 'mango',
        'mango'
    ]))

from pattern.en import quantify

print(quantify({'strawberry': 200, 'peach': 15}))
print(quantify('orange', amount=1200))

# ## Pattern Library Functions for Data Mining

# For macOS SSL issue when downloading file(s) from external sources
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

# ### Accessing Web Pages
Пример #7
0
# The number() command returns an int or float from a written representation.
# This is useful, for example, in combination with a parser
# to transform "CD" parts-of-speech to actual numbers.
# The algorithm ignores words that aren't recognized as numerals.
print(number("two thousand five hundred and eight"))
print(number("two point eighty-five"))
print("")

# The numerals() command returns a written representation from an int or float.
print(numerals(1.249, round=2))
print(numerals(1.249, round=3))
print("")

# The quantify() commands uses pluralization + approximation to enumerate words.
# This is useful to generate a human-readable summary of a set of strings.
print(quantify(["goose", "goose", "duck", "chicken", "chicken", "chicken"]))
print(quantify(["penguin", "polar bear"]))
print(quantify(["carrot"] * 1000))
print(quantify("parrot", amount=1000))
print(quantify({"carrot": 100, "parrot": 20}))
print("")

# The quantify() command only works with words (strings).
# To quantify a set of Python objects, use reflect().
# This will first create a human-readable name for each object and then quantify these.
print(reflect([0, 1, {}, False, reflect]))
print(reflect(os.path))
print(reflect([False, True], quantify=False))
print(quantify(["bunny rabbit"] + reflect([False, True], quantify=False)))
#singularity
print pluralize('child')
print singularize('wolves')

#
print 
print lexeme('run')
print lemma('running')
print conjugate('purred', '3sg')
print PAST in tenses('purred') # 'p' in tenses() also works.
print (PAST, 1, PL) in tenses('purred') 

print 'Quantification'

print quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken'])
print quantify('carrot', amount=90)
print quantify({'carrot': 100, 'parrot': 20})

print 'ngrams'
print ngrams("I am eating a pizza.", n=2)


#parse
s = parse('I eat pizza with a fork.')
pprint(s)

#tag
for word, t in tag('The cat felt happy.'):
    print word +' is ' +t     
    
Пример #9
0
# The number() command returns an int or float from a written representation.
# This is useful, for example, in combination with a parser 
# to transform "CD" parts-of-speech to actual numbers.
# The algorithm ignores words that aren't recognized as numerals.
print number("two thousand five hundred and eight")
print number("two point eighty-five")
print

# The numerals() command returns a written representation from an int or float.
print numerals(1.249, round=2)
print numerals(1.249, round=3)
print

# The quantify() commands uses pluralization + approximation to enumerate words.
# This is useful to generate a human-readable summary of a set of strings.
print quantify(["goose", "goose", "duck", "chicken", "chicken", "chicken"])
print quantify(["penguin", "polar bear"])
print quantify(["carrot"] * 1000)
print quantify("parrot", amount=1000)
print quantify({"carrot": 100, "parrot": 20})
print

# The quantify() command only works with words (strings).
# To quantify a set of Python objects, use reflect().
# This will first create a human-readable name for each object and then quantify these.
print reflect([0, 1, {}, False, reflect])
print reflect(os.path)
print reflect([False, True], quantify=False)
print quantify(
    ["bunny rabbit"] + \
    reflect([False, True], quantify=False))
Пример #10
0
from pattern.de import gender, MALE, FEMALE, NEUTRAL
print(gender('Katze'))
print(gender('Mesa'))

from pattern.en import verbs, conjugate, PARTICIPLE

print('google') in verbs.infinitives
print('googled') in verbs.inflections

print(conjugate('googled', tense=PARTICIPLE, parse=False))
print(conjugate('googled', tense=PARTICIPLE, parse=True))

from pattern.en import quantify

print(quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken']))
print(quantify({'carrot': 100, 'parrot': 20}))
print(quantify('carrot', amount=1000))

from pattern.en import quantify

print(quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken']))
print(quantify({'carrot': 100, 'parrot': 20}))
print(quantify('carrot', amount=1000))

from pattern.en import ngrams
print(ngrams("I am eating pizza.", n=2))  # bigrams

from pattern.en import parse
print(parse('I ate pizza.').split())
Пример #11
0
print(suggest("Heroi"))

print(suggest("Whitle"))
#In the script above we have a word Whitle which is incorrectly spelled. In the output, you will see possible suggestions for this word.
#According to the suggest method, there is a 0.64 probability that the word is "While", similarly there is a probability of 0.29 that the word is "White", and so on.

print(suggest("Fracture"))
#From the output, you can see that there is a 100% chance that the word is spelled correctly.
#%%% Quantify
#Quantify function is used to provide a word count estimation of the words given.
#quantify function is used to get a word count estimation of the items in the list, which provides a phrase for referring to the group. If a list has 3-8 similar items, the quantify function will quantify it to "several". Two items are quantified to a "couple".

from pattern.en import quantify
print(
    quantify([
        'apple', 'apple', 'apple', 'banana', 'banana', 'banana', 'mango',
        'mango'
    ]))
#In the list, we have three apples, three bananas, and two mangoes. The output of the quantify function for this list looks like this:

#demonstrates the other word count estimations.
from pattern.en import quantify
print(quantify({'strawberry': 200, 'peach': 15}))
print(quantify('orange', amount=1200))

from pattern.en import quantify

a = quantify([
    'Pencil', 'Pencil', 'Eraser', 'Sharpener', 'Sharpener', 'Sharpener',
    'Scale', 'Compass'
])
a
def assemble():
    # actually make the thing
    global credits
    credits = {
        'nouns':[],
        'houses':[],
        'characters':[],
        'concepts':[],
        'raw_txt':'',
        'chapter_count':7,
        'chapter_titles':[],
        'character_icons':[]
    }
    

    page_counter = 6
    # first, make the chapters
    animals = pycorpora.get_file("animals","common")['animals']
    
    
    for chapter_counter in range(credits['chapter_count']):
        result = 0
        while (result == 0):
            animal = random.randrange(0,len(animals))
            result = stack(animals[animal], 55)
            del animals[animal]
            
        
        a_chapter = prepare_chapter(result,int(page_counter) + 2,int(chapter_counter) + 1)
        page_counter = a_chapter
        
        # to make a chapter
        
        # pick an animal to stack
        
        # make sure it created a valid set
        
        # prepare_chapter should return a number, the next chapter should start two pages after, so 10 + 2 = 12, e.g.
        
        
    # prepare the frontmatter
    # 1r = frontcover
    # 2l = blank
    # 3r = title page
    # 4l = dedication
    # 5r = toc
    # 6l = blank
    # 7r = introduction
    # 8l = blank, chapter 1 page 0
    
    booktitle = book_title()
    print "This book is called " + booktitle
    
    tpl("templates/frontcover.html","pages/00001r.html",[("book_title",booktitle)])
    
    # blank page
    tpl("templates/template.html","pages/00002l.html",[("","")])
    
    # title page
    tpl("templates/titlepage.html","pages/00003r.html",[("book_title",booktitle)])
    
    # dedication page
    kid_icons = '<div id="kids">'
    for kid in ["Cecily","Daniel","Serena","Wendy"]:
        a_kid = re.sub(r"style=\".+?\"","",get_icon(kid,"333333"))
        kid_icons += a_kid
    kid_icons += "</div>"
    tpl("templates/dedication.html","pages/00004l.html",[("kids",kid_icons)])
    
    # toc -- this one has to be a bit more manual
    toc = ''
    for ch in range(len(credits['chapter_titles'])):
        toc_string = '<div class="toc-entry"><span>Chapter ' + str(ch + 1) + '</span><span>' + credits['chapter_titles'][ch][0] + '</span><span>' + str(credits['chapter_titles'][ch][1] - 8) + '</span></div>'                  
        toc += toc_string
        
    toc += '<div class="toc-entry"><span>&nbsp;</span><span>Credits</span><span>' + str(int(page_counter) - 3) + '</span></div>'
    tpl("templates/toc.html","pages/00005r.html",[("toc",toc)])
    
    # blank page
    tpl("templates/template.html","pages/00006l.html",[("","")])
     
    # introduction
    housecount = quantify("house",amount=len(credits['chapter_titles']))
    peoplecount = quantify("person",amount=len(credits['characters']))
    
    tpl("templates/preface.html","pages/00007r.html",[("house_count",housecount),("people_count",peoplecount),("character_names", ", ".join(list(credits['characters']))),("word_count",str(len(re.compile(r" +").split(credits['raw_txt']))))])
    
    # make a The End
    tpl("templates/last_page.html","pages/" + str(int(page_counter) + 2).zfill(5) + ".html",[("character_names", ", ".join(list(credits['characters'])))]) 
    
    tpl("templates/the_end.html","pages/" + str(int(page_counter) + 3).zfill(5) + ".html",[("character_icons"," ".join(list(credits['character_icons'])))])

    # make the credits
    make_credits(credits,str(int(page_counter) + 2))
    
    print "Generation complete"
def book_title ():
    global credits
    people = list(credits['characters'])
    people[-1] = "and " + people[-1]
    return "The " + quantify("house",amount=len(credits['chapter_titles'])) + " of " + ", ".join(people)
Пример #14
0
#singularity
print pluralize('child')
print singularize('wolves')

#
print
print lexeme('run')
print lemma('running')
print conjugate('purred', '3sg')
print PAST in tenses('purred')  # 'p' in tenses() also works.
print(PAST, 1, PL) in tenses('purred')

print 'Quantification'

print quantify(['goose', 'goose', 'duck', 'chicken', 'chicken', 'chicken'])
print quantify('carrot', amount=90)
print quantify({'carrot': 100, 'parrot': 20})

print 'ngrams'
print ngrams("I am eating a pizza.", n=2)

#parse
s = parse('I eat pizza with a fork.')
pprint(s)

#tag
for word, t in tag('The cat felt happy.'):
    print word + ' is ' + t

s = "The movie attempts to be surreal by incorporating various time paradoxes, but it's presented in such a ridiculous way it's seriously boring."