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
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
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)
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2014-07-11 13:38:30 +00:00
|
|
|
return "!conjugaison <tens> <verb>: give the conjugaison for <verb> in <tens>."
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "conjugaison", help="!conjugaison <tens> <verb>: give the conjugaison for <verb> in <tens>.")
|
2014-07-11 13:38:30 +00:00
|
|
|
def cmd_conjug(msg):
|
|
|
|
if len(msg.cmds) < 3:
|
2014-07-25 10:53:07 +00:00
|
|
|
raise IRCException("donne moi un temps et un verbe, et je te donnerai sa conjugaison!")
|
2014-07-23 13:00:01 +00:00
|
|
|
|
2014-07-25 10:53:07 +00:00
|
|
|
tens = ' '.join(msg.cmds[1:-1])
|
|
|
|
print_debug(tens)
|
2014-07-23 13:00:01 +00:00
|
|
|
|
2014-07-25 10:53:07 +00:00
|
|
|
verb = msg.cmds[-1]
|
2014-07-23 13:00:01 +00:00
|
|
|
|
2014-07-25 10:53:07 +00:00
|
|
|
conjug = get_conjug(verb, tens)
|
2014-07-23 13:00:01 +00:00
|
|
|
|
2014-07-25 10:53:07 +00:00
|
|
|
if len(conjug) > 0:
|
2014-08-12 18:10:19 +00:00
|
|
|
return Response(msg.sender, conjug, channel=msg.channel,
|
|
|
|
title="Conjugaison de %s" % verb)
|
2014-07-11 13:38:30 +00:00
|
|
|
else:
|
2014-07-25 10:53:07 +00:00
|
|
|
raise IRCException("aucune conjugaison de '%s' n'a été trouvé" % verb)
|
2014-07-11 13:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_conjug(verb, stringTens):
|
2014-07-25 10:53:07 +00:00
|
|
|
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-25 10:53:07 +00:00
|
|
|
|
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)
|
2014-07-25 10:53:07 +00:00
|
|
|
return list()
|
2014-07-11 13:38:30 +00:00
|
|
|
|
|
|
|
def compute_line(line, stringTens):
|
2014-07-15 09:28:38 +00:00
|
|
|
try:
|
2014-07-25 10:53:07 +00:00
|
|
|
idTemps = d[stringTens]
|
2014-07-15 09:28:38 +00:00
|
|
|
except:
|
2014-07-25 10:53:07 +00:00
|
|
|
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:
|
2014-07-25 10:53:07 +00:00
|
|
|
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]
|
|
|
|
|
2014-07-25 10:53:07 +00:00
|
|
|
res = list()
|
2014-07-11 13:38:30 +00:00
|
|
|
for elt in re.finditer("[p|/]>([^/]*/b>)", newLine):
|
2014-07-25 10:53:07 +00:00
|
|
|
res.append(striphtml(elt.group(1).replace("<b>", "\x02").replace("</b>", "\x0F")))
|
2014-07-11 13:38:30 +00:00
|
|
|
return res
|