klafytg/klafytg/bots/klafyvel.py
Hugo Levy-Falk 3de7da9916 Fix inline
2020-06-13 18:48:30 +02:00

46 lines
1.4 KiB
Python

import requests
from bs4 import BeautifulSoup
import random
URL_BASE = "https://crisco2.unicaen.fr/des/synonymes/"
def synonyms(word):
page = requests.get(URL_BASE+word)
soup = BeautifulSoup(page.text, features="html.parser")
syn = soup.find_all(id='synonymes')[0]
buff = syn.div
while buff != " Fin titre (vedette + nb de synonymes)":
buff = buff.next_sibling
while buff != "Fin liste des synonymes":
if buff.name and buff.name == "a":
yield buff.string
buff = buff.next_sibling
def make_corpus(start, max_depth=2, exclude=set()):
if max_depth <= 0:
return start
res = set(start)
for word in start:
syn = set(synonyms(word))
res = res.union(make_corpus((syn - res) - exclude, max_depth-1, res.union(syn)))
return res
adjectives_seed = {"idiot"}
nuns_seed = {"patriarcat", "capitaliste", "banquier"}
adjectives = list(make_corpus(adjectives_seed).union(adjectives_seed))
nuns = list(make_corpus(nuns_seed).union(nuns_seed))
def attack():
adj = str.capitalize(random.choice(adjectives))
nun = random.choice(nuns)
if nun[0] in 'aeiouéàè':
link = "d'"
else:
link = "de "
return ' '.join([adj, link+nun, '!'])
def on_attack(update, context):
# you can choose to reply here or to simply return a str wich will be returned (second option is required for inline).
return attack()