コード例 #1
0
def generate_content(nitems=1):
    """
    This method generates some text about
    dinosaurs and asteroids.

    - Number of lines/items = 1
    """

    body = ""

    # =================================
    # Asteroids

    body += "Asteroid of the Day: "

    corpus = Assembler.load(open("olipy/data/minor_planets.min.json"), tokens_in='citation')

    sentences = []
    names = []
    for sentence, source in corpus.assemble("f.l", min_length=4):
        sentences.append(sentence)
        names.append(source['name'])

    # Make a new assembler from the names of the asteroids that were chosen.
    name_assembler = WordAssembler(names)
    name = name_assembler.assemble_word()

    asteroid = name
    asteroid += "\n"*2

    for s in " ".join(sentences):
        asteroid += s

    body += asteroid 
    body += "\n"*2

    # =================================
    # Dinosaurs

    body += "In an alternate universe, this asteroid would have annihilated the following dinosaur species:"
    body += "\n"*2

    assembler = WordAssembler(load_json("dinosaurs.json"))
    
    dinos = []
    for z in range(20):

        dino = assembler.assemble_word()
        if dino[0] in 'AEIO':
            dino = "* " + dino
        else:
            dino = "* " + dino
        dinos.append(dino)

    body += "\n".join(dinos)

    # ===============================
    
    return body
コード例 #2
0
def words():
    common = corpora.words.english_words['words']
    less_common = corpora.words.english_words['words']
    common_corpus = WordAssembler(common)
    full_corpus = WordAssembler(less_common)

    print('You know "%s", "%s", and "%s".' %
          tuple(common_corpus.assemble_word() for i in range(3)))
    print('But have you heard of "%s", "%s", or "%s"?' %
          tuple(full_corpus.assemble_word() for i in range(3)))
コード例 #3
0
def board_games(how_many=10):
    corpus = Assembler.loadlist(corpora.games.bgg_board_games['board_games'],
                                tokens_in='description')

    no_punctuation_at_end = re.compile("[a-zA-Z0-9]$")
    whitespace = re.compile("\s+")

    for i in range(how_many):

        sentences = []
        names = []
        genres = []
        mechanics = []
        for line, source in corpus.assemble("0.l"):
            if no_punctuation_at_end.search(line):
                line += "."
            sentences.append(line)
            names.append(source['name'])
            genres.append(
                [genre for id, genre in source.get('boardgamecategory', [])])
            mechanics.append([
                mechanic
                for id, mechanic in source.get('boardgamemechanic', [])
            ])

        # Make assemblers for single- and multi-word names.
        single_word_assembler = WordAssembler()
        multi_word_assembler = Assembler()

        # Create a composite assembler that will choose single- and
        # multi-word names in appropriate proportion.
        name_assembler = CompositeAssembler(
            [single_word_assembler, multi_word_assembler])
        for name in names:
            words = whitespace.split(name)
            if len(words) == 1:
                single_word_assembler.add(name)
            else:
                multi_word_assembler.add(words)
        assembler, choice = name_assembler.assemble()
        if assembler == single_word_assembler:
            separator = ''
        else:
            separator = ' '
        print(separator.join([x for x, source in choice]))

        # Make assemblers for the game's genres and mechanics
        for name, l in (('Genres', genres), ('Mechanics', mechanics)):
            assembler = Assembler()
            for list in l:
                assembler.add(list)
            choices = [choice for choice, source in assembler.assemble()]
            print("%s: %s" % (name, ", ".join(choices)))
        print("")

        for s in textwrap.wrap(" ".join(sentences)):
            print(s)
        if i < how_many - 1:
            print("-" * 80)
コード例 #4
0
def dinosaurs():
    dinosaurs = corpora.animals.dinosaurs['dinosaurs']
    assembler = WordAssembler(dinosaurs)
    dinos = []
    for i in range(2):
        dino = assembler.assemble_word()
        if dino[0] in 'AEIO':
            dino = "an " + dino
        else:
            dino = "a " + dino
        dinos.append(dino)
    print("Look! Behind that ridge! It's %s fighting %s!" % tuple(dinos))
コード例 #5
0
def mashteroids(how_many=10):
    import textwrap
    asteroids = corpora.science.minor_planet_details["minor_planets"]

    # Make an assembler to generate asteroid citations.
    corpus = Assembler.loadlist(asteroids, tokens_in='citation')

    for i in range(how_many):
        sentences = []
        names = []
        for sentence, source in corpus.assemble("f.l", min_length=3):
            sentences.append(sentence)
            names.append(source['name'])

        # Make a new assembler from the names of the asteroids that
        # were chosen, and use that to generate a new name
        name_assembler = WordAssembler(names)
        name = name_assembler.assemble_word()
        print(name)
        for s in textwrap.wrap(" ".join(sentences)):
            print(s)
        print("")
コード例 #6
0
import json
from olipy.queneau import WordAssembler
from olipy.data import load_json
import textwrap

assembler = WordAssembler(load_json("dinosaurs.json"))

dinos = []
for i in range(2):
    dino = assembler.assemble_word()
    if dino[0] in 'AEIO':
        dino = "an " + dino
    else:
        dino = "a " + dino
    dinos.append(dino)

print "Look! Behind that ridge! It's %s fighting %s!" % tuple(dinos)
コード例 #7
0
import json
from olipy.queneau import WordAssembler
from olipy.data import load_json
import textwrap

my_json_file = "/Volumes/noospace/Users/charles/codes/riverrun/data/greeknames.json"

assembler = WordAssembler(load_json( my_json_file ))

for i in range(18):

    greek = assembler.assemble_word()
    
    print("Chapter %d: %s"%(i+1,greek)) 

コード例 #8
0
from olipy.queneau import Assembler, WordAssembler
import textwrap

corpus = Assembler.load(open("minor_planets.min.json"), tokens_in='citation')

how_many = 100
for i in range(how_many):

    sentences = []
    names = []
    for sentence, source in corpus.assemble("f.l", min_length=3):
        sentences.append(sentence)
        names.append(source['name'])

    # Make a new assembler from the names of the asteroids that were chosen.
    name_assembler = WordAssembler(names)
    name = name_assembler.assemble_word()
    print name
    for s in textwrap.wrap(" ".join(sentences)):
        print s

    if i < how_many - 1:
        print
コード例 #9
0
ファイル: Riverrun.py プロジェクト: estheroids/riverrun
 def make_chapter_name(self):
     assembler = WordAssembler(load_json( rr.json_greek_names ))
     if( random.random() < 0.10 ):
         return assembler.assemble_word() + " and " + assembler.assemble_word()
     else:
         return assembler.assemble_word()