2018-10-08 21:11:51 -04: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
|
2019-01-11 07:47:42 -05:00
|
|
|
from bs4 import BeautifulSoup
|
2019-08-06 23:46:57 -04:00
|
|
|
import re, multiprocessing, sqlite3, shutil, os, html
|
2018-10-08 21:11:51 -04:00
|
|
|
|
2019-08-06 23:46:57 -04:00
|
|
|
def make_sentence(output, cfg):
|
2019-01-11 07:47:42 -05:00
|
|
|
class nlt_fixed(markovify.NewlineText): #modified version of NewlineText that never rejects sentences
|
2018-10-08 21:11:51 -04:00
|
|
|
def test_sentence_input(self, sentence):
|
|
|
|
return True #all sentences are valid <3
|
|
|
|
|
2019-01-11 07:47:42 -05:00
|
|
|
shutil.copyfile("toots.db", "toots-copy.db") #create a copy of the database because reply.py will be using the main one
|
2018-10-08 21:11:51 -04:00
|
|
|
db = sqlite3.connect("toots-copy.db")
|
2019-02-25 13:30:40 -05:00
|
|
|
db.text_factory = str
|
2018-10-08 21:11:51 -04:00
|
|
|
c = db.cursor()
|
2019-02-24 20:17:06 -05:00
|
|
|
if cfg['learn_from_cw']:
|
|
|
|
toots = c.execute("SELECT content FROM `toots` ORDER BY RANDOM() LIMIT 10000").fetchall()
|
2019-02-25 13:30:40 -05:00
|
|
|
else:
|
2019-02-24 20:17:06 -05:00
|
|
|
toots = c.execute("SELECT content FROM `toots` WHERE cw = 0 ORDER BY RANDOM() LIMIT 10000").fetchall()
|
2018-10-08 21:11:51 -04:00
|
|
|
|
2020-03-08 05:46:07 -04:00
|
|
|
if len(toots) == 0:
|
2019-07-10 07:25:07 -04:00
|
|
|
output.send("Database is empty! Try running main.py.")
|
|
|
|
return
|
2020-05-27 08:31:16 -04:00
|
|
|
|
2020-03-08 05:46:07 -04:00
|
|
|
model = nlt_fixed(
|
|
|
|
"\n".join([toot[0] for toot in toots])
|
|
|
|
)
|
2020-05-27 08:31:16 -04:00
|
|
|
|
2020-03-08 05:46:07 -04:00
|
|
|
db.close()
|
|
|
|
os.remove("toots-copy.db")
|
2019-07-10 07:25:07 -04:00
|
|
|
|
|
|
|
toots_str = None
|
|
|
|
|
2018-10-08 21:11:51 -04:00
|
|
|
sentence = None
|
2018-10-28 21:23:01 -04:00
|
|
|
tries = 0
|
|
|
|
while sentence is None and tries < 10:
|
2018-10-26 06:16:40 -04:00
|
|
|
sentence = model.make_short_sentence(500, tries=10000)
|
2018-10-28 21:23:01 -04:00
|
|
|
tries = tries + 1
|
2019-01-11 07:47:42 -05:00
|
|
|
|
2019-04-29 00:21:46 -04:00
|
|
|
# optionally remove mentions
|
|
|
|
if cfg['mention_handling'] == 1:
|
2019-04-29 00:38:44 -04:00
|
|
|
sentence = re.sub(r"^\S*@\u200B\S*\s?", "", sentence)
|
2019-04-29 00:21:46 -04:00
|
|
|
elif cfg['mention_handling'] == 0:
|
2019-04-29 00:38:44 -04:00
|
|
|
sentence = re.sub(r"\S*@\u200B\S*\s?", "", sentence)
|
2019-01-11 07:47:42 -05:00
|
|
|
|
2018-10-08 21:11:51 -04:00
|
|
|
output.send(sentence)
|
|
|
|
|
2019-08-06 23:46:57 -04:00
|
|
|
def make_toot(cfg):
|
2018-10-08 21:11:51 -04:00
|
|
|
toot = None
|
2019-05-19 09:06:31 -04:00
|
|
|
pin, pout = multiprocessing.Pipe(False)
|
2019-08-06 23:46:57 -04:00
|
|
|
p = multiprocessing.Process(target = make_sentence, args = [pout, cfg])
|
2019-05-19 09:06:31 -04:00
|
|
|
p.start()
|
|
|
|
p.join(5) #wait 5 seconds to get something
|
|
|
|
if p.is_alive(): #if it's still trying to make a toot after 5 seconds
|
|
|
|
p.terminate()
|
|
|
|
p.join()
|
|
|
|
else:
|
|
|
|
toot = pin.recv()
|
|
|
|
|
|
|
|
if toot == None:
|
2019-01-11 07:16:04 -05:00
|
|
|
toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance."
|
2019-07-02 06:43:34 -04:00
|
|
|
return toot
|
2019-01-11 07:55:31 -05:00
|
|
|
|
|
|
|
def extract_toot(toot):
|
2020-05-27 08:31:16 -04:00
|
|
|
toot = html.unescape(toot) # convert HTML escape codes to text
|
2019-01-11 07:55:31 -05:00
|
|
|
soup = BeautifulSoup(toot, "html.parser")
|
2020-05-27 08:31:16 -04:00
|
|
|
for lb in soup.select("br"): # replace <br> with linebreak
|
2021-02-18 11:01:43 -05:00
|
|
|
lb.name = "\n"
|
2019-01-11 07:55:31 -05:00
|
|
|
|
2020-05-27 08:31:16 -04:00
|
|
|
for p in soup.select("p"): # ditto for <p>
|
2021-02-18 11:01:43 -05:00
|
|
|
p.name = "\n"
|
2019-01-11 07:55:31 -05:00
|
|
|
|
2020-05-27 08:31:16 -04:00
|
|
|
for ht in soup.select("a.hashtag"): # convert hashtags from links to text
|
2019-01-11 07:55:31 -05:00
|
|
|
ht.unwrap()
|
|
|
|
|
2020-05-27 08:31:16 -04:00
|
|
|
for link in soup.select("a"): #ocnvert <a href='https://example.com>example.com</a> to just https://example.com
|
|
|
|
if 'href' in link:
|
|
|
|
# apparently not all a tags have a href, which is understandable if you're doing normal web stuff, but on a social media platform??
|
|
|
|
link.replace_with(link["href"])
|
2019-01-11 07:55:31 -05:00
|
|
|
|
2019-01-11 07:56:35 -05:00
|
|
|
text = soup.get_text()
|
2020-05-27 08:31:16 -04:00
|
|
|
text = re.sub(r"https://([^/]+)/(@[^\s]+)", r"\2@\1", text) # put mastodon-style mentions back in
|
|
|
|
text = re.sub(r"https://([^/]+)/users/([^\s/]+)", r"@\2@\1", text) # put pleroma-style mentions back in
|
|
|
|
text = text.rstrip("\n") # remove trailing newline(s)
|
2019-02-25 13:30:40 -05:00
|
|
|
return text
|