2018-10-09 01:11:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import markovify
|
|
|
|
import json
|
|
|
|
import re, random, multiprocessing, time, sqlite3, shutil, os
|
|
|
|
|
|
|
|
def make_sentence(output):
|
|
|
|
class nlt_fixed(markovify.NewlineText):
|
|
|
|
def test_sentence_input(self, sentence):
|
|
|
|
return True #all sentences are valid <3
|
|
|
|
|
|
|
|
shutil.copyfile("toots.db", "toots-copy.db")
|
|
|
|
db = sqlite3.connect("toots-copy.db")
|
|
|
|
db.text_factory=str
|
|
|
|
c = db.cursor()
|
|
|
|
toots = c.execute("SELECT content FROM `toots`").fetchall()
|
|
|
|
toots_str = ""
|
|
|
|
for toot in toots:
|
|
|
|
toots_str += "\n{}".format(toot[0])
|
|
|
|
model = nlt_fixed(toots_str)
|
|
|
|
toots_str = None
|
|
|
|
db.close()
|
|
|
|
os.remove("toots-copy.db")
|
|
|
|
|
|
|
|
sentence = None
|
2018-10-29 01:23:01 +00:00
|
|
|
tries = 0
|
|
|
|
while sentence is None and tries < 10:
|
2018-10-26 10:16:40 +00:00
|
|
|
sentence = model.make_short_sentence(500, tries=10000)
|
2018-10-29 01:23:01 +00:00
|
|
|
tries = tries + 1
|
2018-10-28 20:28:56 +00:00
|
|
|
sentence = re.sub("^@\u202B[^ ]* ", "", sentence)
|
2018-10-09 01:11:51 +00:00
|
|
|
output.send(sentence)
|
|
|
|
|
|
|
|
def make_toot(force_markov = False, args = None):
|
|
|
|
return make_toot_markov()
|
|
|
|
|
2018-10-26 10:16:40 +00:00
|
|
|
def make_toot_markov(query = None):
|
2018-10-09 01:11:51 +00:00
|
|
|
tries = 0
|
|
|
|
toot = None
|
2018-10-26 10:16:40 +00:00
|
|
|
while toot == None and tries < 25:
|
2018-10-09 01:11:51 +00:00
|
|
|
pin, pout = multiprocessing.Pipe(False)
|
2018-10-27 06:43:45 +00:00
|
|
|
p = multiprocessing.Process(target = make_sentence, args = [pout])
|
2018-10-09 01:11:51 +00:00
|
|
|
p.start()
|
|
|
|
p.join(10)
|
|
|
|
if p.is_alive():
|
|
|
|
p.terminate()
|
|
|
|
p.join()
|
|
|
|
toot = None
|
|
|
|
tries = tries + 1
|
|
|
|
else:
|
|
|
|
toot = pin.recv()
|
2018-10-26 10:16:40 +00:00
|
|
|
if toot == None:
|
2019-01-11 12:16:04 +00:00
|
|
|
toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance."
|
2018-10-09 01:11:51 +00:00
|
|
|
return {
|
|
|
|
"toot":toot,
|
|
|
|
"media":None
|
|
|
|
}
|