]> gitweb.pimeys.fr Git - scripts-20-100.git/blob - cambridge.py
typo
[scripts-20-100.git] / cambridge.py
1 #!/usr/bin/env python
2 # -*- encoding: utf-8 -*-
3
4 """ Pour mélanger les lettres dans l'ordre des mots.
5 http://www.sauv.net/cmabrigde.php
6 """
7
8 import re
9 import sys
10 import random
11
12 reg = re.compile(r"\b(?P<word>\w+)\b", flags=re.UNICODE)
13
14 def shuffle_word(word):
15 """Mélange un mot. Avec une bonne définition de "mélange"."""
16 if len(word) in [0, 1, 2]:
17 return word
18 inside_word = list(word[1:-1])
19 random.shuffle(inside_word)
20 return word[0] + "".join(inside_word) + word[-1]
21
22 def shuffle(sentence):
23 """Mélange les mots. Avec une bonne définition de "mots" et de "mélange"."""
24 result, pos = u"", 0
25 for match in reg.finditer(sentence):
26 start, end = match.span()
27 result += sentence[pos:start]
28 result += shuffle_word(match.groupdict()["word"])
29 pos = end
30 result += sentence[pos:]
31 return result
32
33 if __name__ == "__main__":
34 sentence = " ".join(sys.argv[1:])
35 sentence = sentence.decode("utf-8")
36 sentence = shuffle(sentence)
37 print sentence.encode("utf-8")