From bc2612eb18e5ae3400fe8f96ff25019e6ff3e0b0 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sun, 9 Aug 2015 17:39:48 +0200 Subject: [PATCH] =?utf8?q?Un=20script=20qui=20m=C3=A9lange=20les=20mots=20?= =?utf8?q?sleon=20l'Uvinertis=C3=A9=20de=20Cmabrigde?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- cambridge.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 cambridge.py diff --git a/cambridge.py b/cambridge.py new file mode 100755 index 0000000..3d85a39 --- /dev/null +++ b/cambridge.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +""" Pour mélanger les lettres dans l'ordre des mots. +http://www.sauv.net/cmabrigde.php +""" + +import re +import sys +import random + +reg = re.compile(r"\b(?P\w+)\b", flags=re.UNICODE) + +def shuffle_word(word): + """Mélange un mot. Avec une bonne définition de "mélange".""" + if len(word) in [0, 1, 2]: + return word + inside_word = list(word[1:-1]) + random.shuffle(inside_word) + return word[0] + "".join(inside_word) + word[-1] + +def shuffle(sentence): + """Mélange les mots. Avec une bonne définition de "mots" et de "mélange".""" + result, pos = u"", 0 + for match in reg.finditer(sentence): + start, end = match.span() + result += sentence[pos:start] + result += shuffle_word(match.groupdict()["word"]) + pos = end + result += sentence[pos:] + return result + +if __name__ == "__main__": + sentence = " ".join(sys.argv[1:]) + sentence = sentence.decode("utf-8") + sentence = shuffle(sentence) + print sentence.encode("utf-8") -- 2.39.2