nemubot/modules/conjugaison.py

87 lines
2.5 KiB
Python
Raw Normal View History

2014-07-11 13:38:30 +00:00
# coding=utf-8
2014-08-27 23:39:31 +00:00
"""Find french conjugaison"""
2014-07-11 13:38:30 +00:00
import re
import traceback
import sys
from urllib.parse import quote
from hooks import hook
2014-07-11 13:38:30 +00:00
from tools import web
from tools.web import striphtml
2014-07-15 09:28:38 +00:00
from collections import defaultdict
2014-07-11 13:38:30 +00:00
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
2014-07-11 13:38:30 +00:00
2014-07-15 09:28:38 +00:00
s = [('present', '0'), ('présent', '0'), ('pr', '0'),
('passé simple', '12'), ('passe simple', '12'), ('ps', '12'),
('passé antérieur', '112'), ('passe anterieur', '112'), ('pa', '112'),
('passé composé', '100'), ('passe compose', '100'), ('pc', '100'),
('futur', '18'), ('f', '18'),
('futur antérieur', '118'), ('futur anterieur', '118'), ('fa', '118'),
('subjonctif présent', '24'), ('subjonctif present', '24'), ('spr', '24'),
('subjonctif passé', '124'), ('subjonctif passe', '124'), ('spa', '124'),
('plus que parfait', '106'), ('pqp', '106'),
('imparfait', '6'), ('ii', '6')]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
def help_full():
2014-07-11 13:38:30 +00:00
return "!conjugaison <tens> <verb>: give the conjugaison for <verb> in <tens>."
@hook("cmd_hook", "conjugaison")
2014-07-11 13:38:30 +00:00
def cmd_conjug(msg):
if len(msg.cmds) < 3:
raise IRCException("donne moi un temps et un verbe, et je te donnerai sa conjugaison!")
tens = ' '.join(msg.cmds[1:-1])
print_debug(tens)
verb = msg.cmds[-1]
conjug = get_conjug(verb, tens)
if len(conjug) > 0:
return Response(conjug, channel=msg.channel,
title="Conjugaison de %s" % verb)
2014-07-11 13:38:30 +00:00
else:
raise IRCException("aucune conjugaison de '%s' n'a été trouvé" % verb)
2014-07-11 13:38:30 +00:00
def get_conjug(verb, stringTens):
url = ("http://leconjugueur.lefigaro.fr/conjugaison/verbe/%s.html" %
quote(verb.encode("ISO-8859-1")))
2014-07-11 13:38:30 +00:00
print_debug (url)
page = web.getURLContent(url)
2014-07-11 13:38:30 +00:00
if page is not None:
for line in page.split("\n"):
if re.search('<div class="modeBloc">', line) is not None:
return compute_line(line, stringTens)
return list()
2014-07-11 13:38:30 +00:00
def compute_line(line, stringTens):
2014-07-15 09:28:38 +00:00
try:
idTemps = d[stringTens]
2014-07-15 09:28:38 +00:00
except:
raise IRCException("le temps demandé n'existe pas")
2014-07-11 13:38:30 +00:00
2014-07-15 09:28:38 +00:00
if len(idTemps) == 0:
raise IRCException("le temps demandé n'existe pas")
2014-07-11 13:38:30 +00:00
2014-07-15 09:28:38 +00:00
index = line.index('<div id="temps' + idTemps[0] + '\"')
2014-07-11 13:38:30 +00:00
endIndex = line[index:].index('<div class=\"conjugBloc\"')
endIndex += index
newLine = line[index:endIndex]
res = list()
2014-07-11 13:38:30 +00:00
for elt in re.finditer("[p|/]>([^/]*/b>)", newLine):
res.append(striphtml(elt.group(1).replace("<b>", "\x02").replace("</b>", "\x0F")))
2014-07-11 13:38:30 +00:00
return res