pleroma-ebooks/functions.py

100 lines
3.3 KiB
Python
Raw Normal View History

2018-10-08 19:11:51 -06:00
#!/usr/bin/env python3
2021-06-15 19:29:53 -06:00
# SPDX-License-Identifier: EUPL-1.2
2018-10-08 19:11:51 -06:00
import markovify
2019-01-11 05:47:42 -07:00
from bs4 import BeautifulSoup
from random import randint
import re, multiprocessing, sqlite3, shutil, os, html
2018-10-08 19:11:51 -06:00
def make_sentence(output, cfg):
class nlt_fixed(markovify.NewlineText): # modified version of NewlineText that never rejects sentences
2018-10-08 19:11:51 -06:00
def test_sentence_input(self, sentence):
return True # all sentences are valid <3
2018-10-08 19:11:51 -06: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 19:11:51 -06:00
db = sqlite3.connect("toots-copy.db")
db.text_factory = str
2018-10-08 19:11:51 -06:00
c = db.cursor()
if cfg['learn_from_cw']:
2021-06-11 15:29:51 -06:00
ignored_cws_query_params = "(" + ",".join("?" * len(cfg["ignored_cws"])) + ")"
2021-06-11 15:37:09 -06:00
toots = c.execute(f"SELECT content FROM `toots` WHERE cw IS NULL OR CW NOT IN {ignored_cws_query_params} ORDER BY RANDOM() LIMIT 10000", cfg["ignored_cws"]).fetchall()
else:
2021-06-11 15:29:51 -06:00
toots = c.execute("SELECT content FROM `toots` WHERE cw IS NULL ORDER BY RANDOM() LIMIT 10000").fetchall()
2018-10-08 19:11:51 -06:00
2020-03-08 03:46:07 -06:00
if len(toots) == 0:
2019-07-10 05:25:07 -06:00
output.send("Database is empty! Try running main.py.")
return
2020-05-27 06:31:16 -06:00
nlt = markovify.NewlineText if cfg['overlap_ratio_enabled'] else nlt_fixed
model = nlt(
2020-03-08 03:46:07 -06:00
"\n".join([toot[0] for toot in toots])
)
2020-05-27 06:31:16 -06:00
2020-03-08 03:46:07 -06:00
db.close()
os.remove("toots-copy.db")
2019-07-10 05:25:07 -06:00
if cfg['limit_length']:
sentence_len = randint(cfg['length_lower_limit'], cfg['length_upper_limit'])
2018-10-08 19:11:51 -06:00
sentence = None
tries = 0
while sentence is None and tries < 10:
sentence = model.make_short_sentence(
max_chars=500,
tries=10000,
max_overlap_ratio=cfg['overlap_ratio'] if cfg['overlap_ratio_enabled'] else 0.7,
max_words=sentence_len if cfg['limit_length'] else None
)
tries = tries + 1
2019-01-11 05:47:42 -07:00
# optionally remove mentions
if cfg['mention_handling'] == 1:
2019-04-28 22:38:44 -06:00
sentence = re.sub(r"^\S*@\u200B\S*\s?", "", sentence)
elif cfg['mention_handling'] == 0:
2019-04-28 22:38:44 -06:00
sentence = re.sub(r"\S*@\u200B\S*\s?", "", sentence)
2019-01-11 05:47:42 -07:00
2018-10-08 19:11:51 -06:00
output.send(sentence)
def make_toot(cfg):
2018-10-08 19:11:51 -06:00
toot = None
2019-05-19 07:06:31 -06:00
pin, pout = multiprocessing.Pipe(False)
p = multiprocessing.Process(target=make_sentence, args=[pout, cfg])
2019-05-19 07:06:31 -06: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
2019-05-19 07:06:31 -06:00
p.terminate()
p.join()
else:
toot = pin.recv()
if toot is None:
2019-01-11 05:16:04 -07:00
toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance."
2019-07-02 04:43:34 -06:00
return toot
def extract_toot(toot):
toot = html.unescape(toot) # convert HTML escape codes to text
soup = BeautifulSoup(toot, "html.parser")
for lb in soup.select("br"): # replace <br> with linebreak
lb.name = "\n"
for p in soup.select("p"): # ditto for <p>
p.name = "\n"
for ht in soup.select("a.hashtag"): # convert hashtags from links to text
ht.unwrap()
for link in soup.select("a"): # convert <a href='https://example.com>example.com</a> to just https://example.com
2020-05-27 06:31:16 -06:00
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"])
text = soup.get_text()
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)
return text