跳转到主要内容

Python艺术文本生成库

项目描述

Olipy

Olipy 是一个用于艺术文本生成的 Python 库。与大多数只有一个单一目的的软件包不同,Olipy 更像是一套艺术用品。每个模块都旨在帮助您实现不同的美学效果。

安装

Olipy 以 PyPI 上的 olipy 包的形式发行。以下是快速从命令行开始的步骤

# Create a virtual environment.
virtualenv env

# Activate the virtual environment.
source env/bin/activate

# Install Olipy within the virtual envirionment.
pip install olipy

# Run an example script.
olipy.apollo

Olipy 使用 TextBlob 库来解析文本。通过 pip 安装 Olipy 将安装 TextBlob 作为依赖项,但 TextBlob 有额外的依赖项(文本语料库),这些依赖项不会通过 pip 安装。安装额外依赖项的说明在 TextBlob 网站上,但它们归结为运行 此 Python 脚本

示例脚本

Olipy 包含一些脚本,可以对数据和算法进行有趣的操作。您可以从已安装 olipy 包的虚拟环境中运行这些脚本中的任何一个。

  • olipy.apollo:生成宇航员与任务控制之间的对话。演示了 Queneau 组装在对话中的应用。
  • olipy.board_games:生成棋盘游戏名称和描述。演示了复杂的 Queneau 组装。
  • olipy.corrupt "污染" 输入的任何文本,通过添加越来越多的变音符号。演示了 gibberish.Corruptor 类。
  • olipy.dinosaurs:生成恐龙名称。演示了 Queneau 组装在单词部分中的应用。
  • olipy.ebooks:使用 *_ebooks 算法从公共领域文本中选择一些行。演示了 olipy.gutenberg.ProjectGutenbergTextolipy.ebooks.EbooksQuotes 类。
  • olipy.gibberish:打印出 140 个字符的美学上令人愉悦的(?)乱码。演示了 gibberish.Gibberish 类。
  • olipy.mashteroids:生成小行星的名称和IAU引用。演示句子上的Queneau组装。
  • olipy.sonnet:使用Queneau组装生成莎士比亚式的十四行诗。
  • olipy.typewriter:将您输入的内容重新打字,并加入错误。
  • olipy.words:生成看起来普通和神秘的英语单词。

模块指南

alphabet.py

一组有趣的Unicode字符集合——字母表、形状等。

from olipy.alphabet import Alphabet
print(Alphabet.default().random_choice())
# 𝔄𝔅ℭ𝔇𝔈𝔉𝔊ℌℑ𝔍𝔎𝔏𝔐𝔑𝔒𝔓𝔔ℜ𝔖𝔗𝔘𝔙𝔚𝔛𝔜ℨ𝔞𝔟𝔠𝔡𝔢𝔣𝔤𝔥𝔦𝔧𝔨𝔩𝔪𝔫𝔬𝔭𝔮𝔯𝔰𝔱𝔲𝔳𝔴𝔵𝔶𝔷
print(Alphabet.default().random_choice())
# ┌┐└┘├┤┬┴┼═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╴╵╶╷

此模块被gibberish.py大量使用。

corpora.py

此模块可以轻松加载Darius Kazemi的Corpora项目中的数据集,以及Olipy特有的附加数据集——主要是Corpora项目认为超出范围的大型单词列表。(这些新数据集在本文档末尾讨论。)

Olipy附带Corpora项目的完整数据副本,因此您无需安装任何其他内容。但是,以其他方式安装Corpora项目数据可以为您提供自Olipy包更新以来创建的数据集。

corpora模块的接口与Allison Parrish的pycorpora项目使用的接口相同。数据集显示为包含Python数据结构的Python模块

from olipy import corpora
for city in corpora.geography.large_cities['cities']:
    print(city)
# Akron
# Albequerque
# Anchorage
# ...

您可以使用from corpora import ...来导入特定的Corpora项目类别

from olipy.corpora import governments
print(governments.nsa_projects["codenames"][0] # prints "ARTIFICE")

from olipy.pycorpora import humans
print(humans.occupations["occupations"][0] # prints "accountant")

此外,corpora支持与Corpora项目节点包提供的类似API

from olipy import corpora

# get a list of all categories
corpora.get_categories() # ["animals", "archetypes"...]

# get a list of subcategories for a particular category
corpora.get_categories("words") # ["literature", "word_clues"...]

# get a list of all files in a particular category
corpora.get_files("animals") # ["birds_antarctica", "birds_uk", ...]

# get data deserialized from the JSON data in a particular file
corpora.get_file("animals", "birds_antarctica") # returns dict w/data

# get file in a subcategory
corpora.get_file("words/literature", "shakespeare_words")

ebooks.py

一个模块,用于不协调地采样风格类似臭名昭著的https://twitter.com/horse_ebooks的文本。基于Allison Parrish的https://twitter.com/zzt_ebooks算法。

from olipy.ebooks import EbooksQuotes
from olipy import corpora
data = corpora.words.literature.fiction.pride_and_prejudice
for quote in EbooksQuotes().quotes_in(data['text']):
    print(quote)
# They attacked him  in various ways--with barefaced
# An invitation to dinner
# Mrs. Bennet
# ...

ebooks.py的示例脚本

  • example.ebooks.py:从Project Gutenberg的文本中选择一些行,倾向于您作为命令行参数提供的关键字。

gibberish.py

一个模块,对于那些对Unicode符号的外观感兴趣的人。其主要用途是使用所选的Unicode代码表组合生成美观的乱码。

from olipy.gibberish import Gibberish
print(Gibberish.random().tweet().encode("utf8"))
# ৠ𐒧𐒇দ𐒔𐒜ৗ𐒃𐒝𐒓আ৭৭উ𐒇৶০ধপ𐒤৯ৰ৪ড়ঐবননত৲ফঌ𐒓৴ৄু০েএঠৰ𐒔𐒥গনি৶ঘ𐒋উঙ𐒤ঙছতাৃীফ৮৬৸উকফ𐒘ইমঢ৭ূণঌঊ𐒇𐒋ীঁিৃ𐒌𐒒৺𐒤৺ভ𐒖৭𐒤ৡৰল𐒊ঢ়ৎ𐒅যথখৱঌ
# ঈঔ৫ঽ𐒔৩়দ𐒋ৠসুয়ঊশ𐒆𐒖𐒁ঔৰসঈ𐒆অ𐒋𐒑𐒨়দ৯ৄ৫ 😘

gutenberg.py

一个处理Project Gutenberg文本的模块。去除标题和页脚,并解析文本。

from olipy import corpora
from olipy.gutenberg import ProjectGutenbergText
text = corpora.words.literature.nonfiction.literary_shrines['text']
text = ProjectGutenbergText(text)
print(len(text.paragraphs))
# 1258

ia.py

一个处理Internet Archive文本的模块。

import random
from olipy.ia import Text

# Print a URL to the web reader for a specific title in the IA collection.
item = Text("yorkchronicle1946poqu")
print(item.reader_url(10))
# https://archive.org/details/yorkchronicle1946poqu/page/n10

# Pick a random page from a specific title, and print a URL to a
# reusable image of that page.
identifier = "TNM_Radio_equipment_catalog_fall__winter_1963_-_H_20180117_0150"
item = Text(identifier)
page = random.randint(0, item.pages-1)
print(item.image_url(page, scale=8))
# https://ia600106.us.archive.org/BookReader/BookReaderImages.php?zip=/30/items/TNM_Radio_equipment_catalog_fall__winter_1963_-_H_20180117_0150/TNM_Radio_equipment_catalog_fall__winter_1963_-_H_20180117_0150_jp2.zip&file=TNM_Radio_equipment_catalog_fall__winter_1963_-_H_20180117_0150_jp2/TNM_Radio_equipment_catalog_fall__winter_1963_-_H_20180117_0150_0007.jp2&scale=8

letterforms.py

一个了解Unicode符号形状的模块。

alternate_spelling将英文字母表中的字母翻译成相似的字符。

from olipy.letterforms import alternate_spelling
print(alternate_spelling("I love alternate letterforms."))
# ヱ 𝑳𝖮Ⓥ𝙀 𝚊𝓵┯⒠┌𝐍a⫪𝖊 𝐋𝖾ߙ𝓉ᥱ𝙧ߓ𝕠┍ጠ𝑆.

markov.py

一个模块,使用马尔可夫链从旧标记列表生成新标记列表。

Olipy的主要目的是推广替代马尔可夫链的方法(如Queneau组装和*_ebooks算法),但有时您确实需要马尔可夫链。Queneau组装通常比马尔可夫链在词级以上(从句子构建段落)和词级以下(从音素构建单词)更好,但当组装单词序列时,马尔可夫链通常更好。

markov.py最初由Allison "A. A." Parrish编写。

from olipy.markov import MarkovGenerator
from olipy import corpora
text = corpora.words.literature.nonfiction.literary_shrines['text']
g = MarkovGenerator(order=1, max=100)
g.add(text)
print(" ".join(g.assemble()))
# The Project Gutenberg-tm trademark.                    Canst thou, e'en thus, thy own savings, went as the gardens, the club. The quarrel occurred between
# him and his essay on the tea-table. In these that, in Lamb's day, for a stray
# relic or four years ago, taken with only Adam and _The
# Corsair_. Writing to his home on his new purple and the young man you might
# mean nothing on Christmas sports and art seriously instead of references to
# the heart'--allowed--yet I got out and more convenient.... Mr.

mosaic.py

将Unicode字符拼接在一起以创建对称的马赛克。gibberish.py使用此模块作为其技术之一。包括有关符号看起来像镜像的Unicode字符的信息。

from olipy.mosaic import MirroredMosaicGibberish
mosaic = MirroredMosaicGibberish()
print(mosaic.tweet())
# ▛▞ ▙▞▙▟▚▟ ▚▜
# ▛▞▞ ▞▛▜▚ ▚▚▜
#  ▞▙  ▞▚  ▟▚ 
# ▙▚▚ ▚▙▟▞ ▞▞▟
# ▙▚ ▛▚▛▜▞▜ ▞▟

print(gibberish.tweet())
# 🙌🙌😯📶🙌👍👍🙌📶😯🙌🙌
#  📶🙌😯🙌🕠🕠🙌😯🙌📶 
# 🚂💈🎈🔒🚲🕃🕃🚲🔒🎈💈🚂
#  📶🙌😯🙌🕠🕠🙌😯🙌📶 
# 🙌🙌😯📶🙌👍👍🙌📶😯🙌🙌

queneau.py

一个用于Queneau组装的模块,这是一种由Raymond Queneau在他的1961年书籍《Cent mille milliards de poèmes》(“一亿亿首诗”)中开创的技术。Queneau组装从具有相同结构的现有文本集合中随机创建新文本。

from olipy.queneau import WordAssembler
from olipy.corpus import Corpus
assembler = WordAssembler(Corpus.load("dinosaurs"))
print(assembler.assemble_word())
# Trilusmiasunaus

randomness.py

生成比random.choice更复杂的随机模式的技巧。

梯度

Gradient类生成一个随机选择的字符串,这些选择在开始时偏向一组选项,在结束时偏向另一组选项。

这是一个从小写字母到大写字母的渐变。

from olipy.randomness import Gradient
import string
print("".join(Gradient.gradient(string.lowercase, string.uppercase, 40)))
# rkwyobijqQOzKfdcSHIhYINGrQkBRddEWPHYtORB

WanderingMonsterTable

WanderingMonsterTable类允许您从四个桶中选择一个进行加权随机选择。从“普通”桶中随机选择的概率为65%,从“不常见”桶中为20%,“稀有”为11%,而“非常稀有”为4%。(它使用了《高级龙与地下城》第一版的相同概率。)

from olipy.randomness import WanderingMonsterTable

monsters = WanderingMonsterTable(
         common=["Giant rat", "Alligator"],
         uncommon=["Orc", "Hobgoblin"],
         rare=["Mind flayer", "Neo-otyugh"],
         very_rare=["Flumph", "Ygorl, Lord of Entropy"],
)
for i in range(5):
    print monsters.choice()
# Giant rat
# Alligator
# Alligator
# Orc
# Giant rat

tokenizer.py

一个比NLTK默认分词器在某些常见的英语类型上表现更好的单词分词器。

from nltk.tokenize.treebank import TreebankWordTokenizer
s = '''Good muffins cost $3.88\\nin New York. Email: muffins@example.com'''
TreebankWordTokenizer().tokenize(s)
# ['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York.', 'Email', ':', 'muffins', '@', 'example.com']
WordTokenizer().tokenize(s)
# ['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York.', 'Email:', 'muffins@example.com']

typewriter.py

模拟了《闪灵》中使用的Adler Universal 39打字机以及在那个打字机上可能会犯的错误。最初是为@a_dull_bot编写的。

from olipy.typewriter import Typewriter
typewriter = Typewriter()
typewriter.type("All work and no play makes Jack a dull boy.")
# 'All work and no play makes Jack a dull bo6.'

额外语料库

Olipy提供了几个不在语料库项目中的单词列表和数据集。这些数据集(以及标准语料库项目数据集)可以通过corpora模块访问。只需编写如下代码

from olipy import corpora
nouns = corpora.words.common_nouns['abstract_nouns']

corpora.geography.large_cities

大型美国和世界城市的名称。

corpora.geography.us_states

美国的五十个州。

corpora.language.languages

ISO-639-1中定义的语言名称

corpora.language.unicode_code_sheets

每个Unicode代码表的名称,每个代码表都包含该表上的字符。

corpora.science.minor_planet_details

截至2013年7月的命名小行星(例如小行星)的“名称”、“编号”和IAU“引用”。“发现”字段包含发现情况。当存在时,“建议”字段已从原始IAU引用的末尾分割出来,使用简单的启发式方法。然后使用NLTK的Punkt分词器和一组自定义缩写将“引用”字段分词为句子。

数据来源:http://www.minorplanetcenter.net/iau/lists/NumberedMPs.html http://ssd.jpl.nasa.gov/sbdb.cgi

这比语料库项目的minor_planets更完整,后者只列出了前1000颗小行星的名称。

corpora.words.adjectives

大约5000个英语形容词,按出现频率大致排序。

corpora.words.common_nouns

英语名词列表,按出现频率大致排序。

包括

  • 抽象名词如“工作”和“爱情”。
  • 具体名词如“脸”和“房子”。
  • 形容词性名词——可以作为形容词使用的名词,如“机会”和“光”。

corpora.words.common_verbs

英语动词列表,按出现频率大致排序。

  • 现在时态动词,如“得到”和“想要”。
  • 过去时态动词,如“说”和“找到”。
  • 动名词形式,如“拿着”和“离开”。

corpora.words.english_words

来自FRELI项目的约73,000个英语单词的汇总列表。(http://www.nkuitse.com/freli/

corpora.words.scribblenauts

2009年游戏《Scribblenauts》中可以召唤的“足够具体”的前4000个名词。像往常一样,这个列表按照更常见的单词排列在前面。

corpora.words.literature.board_games

关于棋盘游戏的信息,于2013年7月从BoardGameGeek收集而来。每行一个JSON对象。

数据来源:http://boardgamegeek.com/wiki/page/BGG_XML_API2

corpora.words.literature.fiction.pride_and_prejudice

一部公共领域小说的完整文本(《简·奥斯汀的《傲慢与偏见》》)。

corpora.words.literature.nonfiction.apollo_11

阿波罗11号任务的记录,以对话形式呈现,使用NLTK的Punkt分词器分词成句子。每行一个JSON对象。

数据来源:阿波罗11号飞行日志:http://history.nasa.gov/ap11fj/ 阿波罗11号表面日志:http://history.nasa.gov/alsj/ “旨在成为所有对阿波罗计划感兴趣的人的资源,无论其兴趣是短暂的还是学术性的。”

corpora.words.literature.nonfiction.literary_shrines

一部公有领域非虚构书籍的完整文本(《伦敦著名房屋和文学圣地》由A. St. John Adcock著)。

corpora.words.literature.gutenberg_id_mapping

将旧式(2007年之前)的Project Gutenberg文件名映射到新式电子书ID。例如,"/etext95/3boat10.zip"映射到数字308(见http://www.gutenberg.org/ebooks/308)。基本上没有人需要这个。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

olipy-1.0.4.tar.gz (4.6 MB 查看哈希)

上传时间

构建分布

olipy-1.0.4-py3-none-any.whl (4.7 MB 查看哈希)

上传时间 Python 3

olipy-1.0.4-py2-none-any.whl (4.7 MB 查看哈希)

上传时间 Python 2

由以下机构支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF 赞助商 PingdomPingdom 监控 SentrySentry 错误记录 StatusPageStatusPage 状态页面