From 94a51fcaf8fe46a9fb5a346fee68e37bea0a436c Mon Sep 17 00:00:00 2001 From: tfa Date: Wed, 7 Sep 2022 12:58:31 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20recherche?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chef_mou.py | 89 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/chef_mou.py b/chef_mou.py index 2a6906d..8c14698 100644 --- a/chef_mou.py +++ b/chef_mou.py @@ -6,11 +6,35 @@ import aiohttp import asyncio from bs4 import BeautifulSoup import random +import re +from difflib import SequenceMatcher +from heapq import nlargest as _nlargest class ChefMou(Plugin): async def start(self) -> None: self.on_external_config_update() + def get_close_matches_indexes(self, word, possibilities, n=3, cutoff=0.6): + if not n > 0: + raise ValueError("n must be > 0: %r" % (n,)) + if not 0.0 <= cutoff <= 1.0: + raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,)) + result = [] + s = SequenceMatcher() + s.set_seq2(word) + for idx, x in enumerate(possibilities): + s.set_seq1(x) + if s.real_quick_ratio() >= cutoff and \ + s.quick_ratio() >= cutoff and \ + s.ratio() >= cutoff: + result.append((s.ratio(), idx)) + + # Move the best scorers to head of list + result = _nlargest(n, result) + + # Strip scores for the best n matches + return [x for score, x in result] + async def get_site_content(self): async with aiohttp.ClientSession() as session: @@ -49,30 +73,49 @@ class ChefMou(Plugin): loop.close() return soup = await self.get_site_content() - s = soup.find('th', string=chunks[2]) - if not s : - await evt.respond("Morceau non trouvé") - return - t = s.parent.findAll('a') - idx = 9 - if chunks[1].lower() == "souba" or chunks[1].lower() == "soubassophone" or chunks[1].lower() == "sb": - idx = 0 - elif chunks[1].lower() == "basse" or chunks[1].lower() == "bs" : - idx = 1 - elif chunks[1].lower() == "trombone" or chunks[1].lower() == "tb": - idx = 2 - elif chunks[1].lower() == "trompette" or chunks[1].lower() == "tp": - idx = 3 - elif chunks[1].lower() == "sax" or chunks[1].lower() == "saxophone": - idx = 4 - elif chunks[1].lower() == "clarinette" or chunks[1].lower() == "cl": - idx = 5 - else: - await evt.respond("Pas trouvé cet instru") - loop.close() - return + # Récuperer liste morceaux + s = soup.select('th', background='/fond3.gif') + # Object bts en str + parsedList=[] + originalList=[] + for ts in s: + fullTitre = ts.text + #Supression des () + parsedList.append(re.sub("[\(\[].*?[\)\]]", "", fullTitre)) + #On garde une copie pour la recherche + originalList.append(ts.text) + + # Fonction qui retourne l'index du string le plus proche + idxTitre = self.get_close_matches_indexes(chunks[2], parsedList, cutoff=0.4) + # Si trouvé le chercher sinon erreur + if len(idxTitre) > 0 : + #Et on recherche dans la liste originale avec le titre original + s = soup.find('th', string=originalList[idxTitre[0]]) + t = s.parent.findAll('a') + idx = 9 + if chunks[1].lower() == "souba" or chunks[1].lower() == "soubassophone" or chunks[1].lower() == "sb": + idx = 0 + elif chunks[1].lower() == "basse" or chunks[1].lower() == "bs" : + idx = 1 + elif chunks[1].lower() == "trombone" or chunks[1].lower() == "tb": + idx = 2 + elif chunks[1].lower() == "trompette" or chunks[1].lower() == "tp": + idx = 3 + elif chunks[1].lower() == "sax" or chunks[1].lower() == "saxophone": + idx = 4 + elif chunks[1].lower() == "clarinette" or chunks[1].lower() == "cl": + idx = 5 + else: + await evt.respond("Pas trouvé cet instru") + loop.close() + return + try : + await evt.respond("["+originalList[idxTitre[0]]+ " " + chunks[1]+"]"+"()") + except IndexError: + await evt.respond("Le morceau "+ strTitre[0] + " existe mais pas ta partoche") - await evt.respond("["+chunks[2]+ " " + chunks[1]+"]"+"()") + else: + await evt.respond("Morceau non trouvé") else: await evt.respond("Nope !") loop.close()