本文整理汇总了Python中markovify.Text方法的典型用法代码示例。如果您正苦于以下问题:Python markovify.Text方法的具体用法?Python markovify.Text怎么用?Python markovify.Text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类markovify
的用法示例。
在下文中一共展示了markovify.Text方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_padding
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def make_padding(self):
if self.dynamic:
f = open(self.corpus, 'r')
text = markovify.Text(f)
self.logger.info('generating dynamic padding from corpus')
pad = '<p style="font-size: 0px">'
for i in range(1, 50):
temp = text.make_sentence()
if temp is not None:
pad += ' ' + temp
if i % 5 == 0:
pad +=' </br>'
else:
pad += ' </br>'
pad += ' </p>'
self.logger.info('dynamic padding generated successfully')
f.close()
else:
self.logger.warning('message created using static padding')
pad = STATIC_PADDING
return pad
示例2: gen_markov
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def gen_markov(self, msg, matches):
chat_id = msg.dest.id
if matches.group(4) is not None:
text = self.markov_by_id(chat_id, matches.group(2))
elif matches.group(3) is not None:
text = self.markov_by_username(abs(chat_id), matches.group(3))
else:
text = self.markov_by_fullname(abs(chat_id), matches.group(4))
try:
text_model = markovify.Text(text[0]["fulltext"].replace('?.', '?').replace('!.', '!'))
text = ""
for i in range(3):
text += text_model.make_short_sentence(140, tries=100, ) + " "
return "{}".format(text)
except:
return "Error generating chain."
示例3: markov
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def markov():
with open('parsed.csv', 'rb') as csvfile:
reader = csv.reader(csvfile)
text = ''
for row in reader:
escapes = ''.join([chr(char) for char in range(1, 32)])
t = str(row).translate(None, escapes)
t = t.decode('string_escape')
text += t
text_model = markovify.Text(text)
return decorate(h.unescape(text_model.make_short_sentence(140)))
示例4: test_text_too_small
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_text_too_small(self):
text = u"Example phrase. This is another example sentence."
text_model = markovify.Text(text)
assert(text_model.make_sentence() == None)
示例5: test_json
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_json(self):
text_model = self.sherlock_model
json_model = text_model.to_json()
new_text_model = markovify.Text.from_json(json_model)
sent = new_text_model.make_sentence()
assert(len(sent) != 0)
示例6: test_chain
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_chain(self):
text_model = self.sherlock_model
chain_json = text_model.chain.to_json()
stored_chain = markovify.Chain.from_json(chain_json)
assert(get_sorted(stored_chain.to_json()) == get_sorted(chain_json))
new_text_model = markovify.Text.from_chain(chain_json)
assert(get_sorted(new_text_model.chain.to_json()) == get_sorted(chain_json))
sent = new_text_model.make_sentence()
assert(len(sent) != 0)
示例7: test_mismatched_state_sizes
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_mismatched_state_sizes(self):
with self.assertRaises(Exception) as context:
text_model_a = markovify.Text(sherlock, state_size=2)
text_model_b = markovify.Text(sherlock, state_size=3)
combo = markovify.combine([ text_model_a, text_model_b ])
示例8: test_simple
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_simple(self):
with open(os.path.join(os.path.dirname(__file__), "texts/sherlock.txt")) as f:
sherlock_model = markovify.Text(f)
sent = sherlock_model.make_sentence()
assert sent is not None
assert len(sent) != 0
示例9: test_from_json_without_retaining
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_from_json_without_retaining(self):
with open(os.path.join(os.path.dirname(__file__), "texts/senate-bills.txt")) as f:
original_model = markovify.Text(f, retain_original=False)
d = original_model.to_json()
new_model = markovify.Text.from_json(d)
sent = new_model.make_sentence()
assert sent is not None
assert len(sent) != 0
示例10: test_from_mult_files_without_retaining
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def test_from_mult_files_without_retaining(self):
models = []
for (dirpath, _, filenames) in os.walk(os.path.join(os.path.dirname(__file__), "texts")):
for filename in filenames:
with open(os.path.join(dirpath, filename)) as f:
models.append(markovify.Text(f, retain_original=False))
combined_model = markovify.combine(models)
sent = combined_model.make_sentence()
assert sent is not None
assert len(sent) != 0
示例11: cycler
# 需要导入模块: import markovify [as 别名]
# 或者: from markovify import Text [as 别名]
def cycler(ev):
"""
:param ev: The event object referenced in the event.
:type ev: sigma.core.mechanics.event.SigmaEvent
"""
global current_user_collecting
while True:
if ev.bot.is_ready():
cltr_items = await ev.db[ev.db.db_nam].CollectorQueue.find({}).to_list(None)
for cltr_item in cltr_items:
cl_usr = await ev.bot.get_user(cltr_item.get('user_id'))
cl_chn = await ev.bot.get_channel(cltr_item.get('channel_id'))
cl_ath = await ev.bot.get_user(cltr_item.get('author_id'))
if cl_usr and cl_chn:
await ev.db[ev.db.db_nam].CollectorQueue.delete_one(cltr_item)
current_user_collecting = cl_usr.id
collection = await ev.db[ev.db.db_nam].MarkovChains.find_one({'user_id': cl_usr.id})
collection = collection.get('chain') if collection else None
chain = markovify.Text.from_dict(deserialize(collection)) if collection is not None else None
messages = []
pfx = await ev.db.get_guild_settings(cl_chn.guild.id, 'prefix') or ev.bot.cfg.pref.prefix
# noinspection PyBroadException
try:
async for log in cl_chn.history(limit=100000):
cnt = log.content
if log.author.id == cl_usr.id and len(log.content) > 8:
if not check_for_bot_prefixes(pfx, cnt) and not check_for_bad_content(cnt):
cnt = cleanse_content(log, cnt)
if cnt not in messages and cnt and len(cnt) > 1:
messages.append(cnt)
if len(messages) >= 5000:
break
except Exception as e:
print(e)
pass
try:
new_chain = markovify.Text(f'{". ".join(messages)}.')
combined = markovify.combine([chain, new_chain]) if chain else new_chain
insert_data = {'user_id': cl_usr.id, 'chain': serialize(combined.to_dict())}
await ev.db[ev.db.db_nam].MarkovChains.delete_one({'user_id': cl_usr.id})
await ev.db[ev.db.db_nam].MarkovChains.insert_one(insert_data)
await notify_target(cl_ath, cl_usr, cl_chn, len(messages), combined.parsed_sentences)
current_user_collecting = None
ev.log.info(f'Collected a chain for {cl_usr.name}#{cl_usr.discriminator} [{cl_usr.id}]')
except Exception as e:
await notify_failure(cl_ath, cl_usr, cl_chn)
ev.log.error(f"Markov generation failure for {cl_usr.id}: {e}.")
await asyncio.sleep(1)