| 1 |
|
| 2 |
from .repetition import repetition_experiences |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
def parse_text(text): |
| 8 |
"""Describe drawing. |
| 9 |
|
| 10 |
Coordinates are expressed as percentages of picture dimensions. |
| 11 |
Return a dict containing 2 lists: |
| 12 |
* one for points: [(text, x, y), ...] |
| 13 |
* one for lines: [(text, xy1, xy2), ...] |
| 14 |
""" |
| 15 |
lines = [l for l in text.split("\n") if l.strip()] |
| 16 |
|
| 17 |
if not lines: |
| 18 |
return {'points': [], 'lines': []} |
| 19 |
|
| 20 |
if lines[0].startswith("|"): |
| 21 |
legende = lines.pop(0) |
| 22 |
ce |
| 23 |
ymax = 90 |
| 24 |
else: |
| 25 |
legende = None |
| 26 |
ymax = 100 |
| 27 |
|
| 28 |
if not lines: |
| 29 |
return {'points': [], 'lines': []} |
| 30 |
|
| 31 |
antes |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
à: |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
if lines[0].startswith('>>'): |
| 47 |
profondeur = len(lines[0]) - len(lines[0].lstrip('>')) |
| 48 |
evenements = {} |
| 49 |
for line in lines: |
| 50 |
assert line.startswith(profondeur*'>') |
| 51 |
nom, proba = line[profondeur:].split(':') |
| 52 |
evenements[nom.strip()] = proba.strip() |
| 53 |
lines = repetition_experiences(_profondeur=profondeur, _numeroter=True, **evenements).split('\n') |
| 54 |
|
| 55 |
al). |
| 56 |
). |
| 57 |
if lines[0].startswith(">"): |
| 58 |
lines = [""] + lines |
| 59 |
|
| 60 |
depths = [len(line) - len(line.lstrip(">")) for line in lines] |
| 61 |
|
| 62 |
nbr_colonnes = 1 + max(depths, default=0) |
| 63 |
|
| 64 |
|
| 65 |
nbr_lignes = [] |
| 66 |
|
| 67 |
for i in xrange(nbr_colonnes): |
| 68 |
nbr_lignes.append(depths.count(i)) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
s |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
arbre = [] |
| 86 |
ligne_precedente = [-1 for i in xrange(nbr_colonnes)] e |
| 87 |
for line in lines: |
| 88 |
colonne = len(line) - len(line.lstrip(">")) |
| 89 |
branche = arbre |
| 90 |
for i in xrange(colonne): ) |
| 91 |
branche = branche[ligne_precedente[i]]["liste"] |
| 92 |
branche.append({"texte": line.lstrip(">"), "liste": []}) |
| 93 |
ligne_precedente[colonne] += 1 |
| 94 |
for i in xrange(colonne + 1, nbr_colonnes): |
| 95 |
ligne_precedente[i] = -1 |
| 96 |
print (arbre) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
def compter_ramifications(branche): |
| 101 |
if len(branche["liste"]) > 0: |
| 102 |
return sum(compter_ramifications(tige) for tige in branche["liste"]) |
| 103 |
else: |
| 104 |
return 1 |
| 105 |
|
| 106 |
ramifications = sum(compter_ramifications(branche) for branche in arbre) |
| 107 |
if param.debug: |
| 108 |
print("Nombre de ramifications: " + str(ramifications)) |
| 109 |
|
| 110 |
def formater_texte(texte): |
| 111 |
if texte: |
| 112 |
if texte.startswith("&"): |
| 113 |
texte = r"\overline{" + texte[1:] + "}" |
| 114 |
texte = texte.replace("&", r"\overline ") |
| 115 |
|
| 116 |
texte = re.sub("(\\\\overline)[ ]+(%s)" % VAR, lambda m:'%s{%s}' % (m.group(1), m.group(2)), texte) |
| 117 |
texte = "$" + texte + "$" if texte[0] != '$' else texte |
| 118 |
if param.latex: |
| 119 |
texte = "$" + texte + "$" |
| 120 |
texte = texte.replace(" inter ", r"\ \cap \ ").replace(" union ", r"\ \cup \ ").replace("Omega", r"\Omega").replace("omega", r"\Omega") |
| 121 |
if param.latex: |
| 122 |
texte = re.sub("[0-9]+/[0-9]+",lambda s:"\\frac{" + s.group().replace("/", "}{") + "}", texte) |
| 123 |
return texte |
| 124 |
|
| 125 |
|
| 126 |
def creer_point(x, y, texte): |
| 127 |
texte = formater_texte(texte) |
| 128 |
M = Point(x, y, style = "o", couleur = "w", taille = 0) |
| 129 |
M.label(texte) |
| 130 |
M.etiquette.style(_rayon_=0, niveau=15, alignement_vertical="center", |
| 131 |
alignement_horizontal="center", fond=True, |
| 132 |
couleur_fond="w") |
| 133 |
return M |
| 134 |
|
| 135 |
|
| 136 |
def creer_segment(point1, point2, texte): |
| 137 |
texte = formater_texte(texte) |
| 138 |
s = Segment(point1, point2) |
| 139 |
s.label(texte) |
| 140 |
style = {'_rayon_': 0, 'niveau': 15} |
| 141 |
placement = self.param('placement_probabilites') |
| 142 |
if placement == 'dessus': |
| 143 |
style.update(alignement_vertical='center', couleur_fond='w', |
| 144 |
fond=True, alignement_horizontal='center', |
| 145 |
angle=0) |
| 146 |
elif placement == 'longe': |
| 147 |
style.update(alignement_vertical='auto', fond=False, |
| 148 |
alignement_horizontal='center', angle='auto') |
| 149 |
elif placement == 'decale': |
| 150 |
style.update(alignement_vertical='auto', fond=False, |
| 151 |
alignement_horizontal='right', angle=0) |
| 152 |
else: |
| 153 |
print(u"Placement: mode '%s' non reconnu." % placement) |
| 154 |
s.etiquette.style(**style) |
| 155 |
return s |
| 156 |
|
| 157 |
ramification = [0] |
| 158 |
def parcourir_branche(branche, n, ramification = ramification): |
| 159 |
txt = branche["texte"].rsplit(":", 1) |
| 160 |
if len(txt) == 2: |
| 161 |
txt_pt, txt_segm = txt |
| 162 |
else: |
| 163 |
txt_pt = txt[0] |
| 164 |
txt_segm = "" |
| 165 |
if len(branche["liste"]) > 0: |
| 166 |
l = [] |
| 167 |
for tige in branche["liste"]: |
| 168 |
l.append(parcourir_branche(tige, n + 1)) |
| 169 |
|
| 170 |
M = creer_point(n/(nbr_colonnes - 1), .5*(l[0][0].ordonnee+l[-1][0].ordonnee), txt_pt) |
| 171 |
self.feuille_actuelle.objets.add(M) |
| 172 |
for point, txt_s in l: |
| 173 |
s = creer_segment(M, point, txt_s) |
| 174 |
self.feuille_actuelle.objets.add(s) |
| 175 |
return M, txt_segm |
| 176 |
else: |
| 177 |
if ramifications > 1: |
| 178 |
y = 1 - ramification[0]/(ramifications - 1) |
| 179 |
else: |
| 180 |
y = .5 |
| 181 |
M = creer_point(n/(nbr_colonnes - 1), y, txt_pt) |
| 182 |
ramification[0] += 1 |
| 183 |
self.feuille_actuelle.objets.add(M) |
| 184 |
return M, txt_segm |
| 185 |
|
| 186 |
for branche in arbre: |
| 187 |
parcourir_branche(branche, 0) |
| 188 |
|
| 189 |
if legende is not None: |
| 190 |
decalage = -0.5 |
| 191 |
for i in legende: |
| 192 |
if i != "|": |
| 193 |
break |
| 194 |
decalage += .5 |
| 195 |
legende = legende.strip("|").split("|") |
| 196 |
for n in xrange(len(legende)): |
| 197 |
t = Texte(legende[n], (n + decalage)/(nbr_colonnes - 1), 1.1) |
| 198 |
self.feuille_actuelle.objets.add(t) |
| 199 |
|
| 200 |
self.feuille_actuelle.interprete.commande_executee() |