1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
# Test si on trouve une partie pat "proche" de la partie du pattern patbase
def estProche(patbase, pat):
if isIntElem1: elem1 = pat[0] in patbase[0]
else: elem1 = ord(pat[0]) in patbase[0]
if isIntElem2: elem2 = pat[1] in patbase[1]
else: elem2 = ord(pat[1]) in patbase[1]
if isIntElem3: elem3 = pat[2] in patbase[2]
else: elem3 = ord(pat[2]) in patbase[2]
return elem1 and elem2 and elem3
# Retourne l'indice du pattern trouver
def cherchePremier(liste, debut, patbase):
for i in range(debut + 1, len(liste)):
if estProche(patbase, liste[i]):
return i
return None
def normaliseTriplet(triplet):
if len(triplet) != 3:
return None
if isIntElem1: elem1 = int(triplet[0])
else: elem1 = triplet[0].strip()
if isIntElem2: elem2 = int(triplet[1])
else: elem2 = triplet[1].strip()
if isIntElem3: elem3 = int(triplet[2])
else: elem3 = triplet[2].strip()
return [elem1, elem2, elem3]
def rangefy(triplet, delta):
if isIntElem1: elem1 = range(max(triplet[0] - delta[0], 0), triplet[0] + delta[0] + 1)
else:
bornes = ([ord('a'), ord('z') + 1], [ord('A'), ord('Z') + 1])[triplet[0].isupper()]
elem1 = range(max(ord(triplet[0]) - delta[0], bornes[0]), min(ord(triplet[0]) + delta[0] + 1, bornes[1]))
if isIntElem2: elem2 = range(max(triplet[1] - delta[1], 0), triplet[1] + delta[1] + 1)
else:
bornes = ([ord('a'), ord('z') + 1], [ord('A'), ord('Z') + 1])[triplet[1].isupper()]
elem2 = range(max(ord(triplet[1]) - delta[1], bornes[0]), min(ord(triplet[1]) + delta[1] + 1, bornes[1]))
if isIntElem3:
elem3 = range(max(triplet[2] - delta[2], 0), triplet[2] + delta[2] + 1)
else:
bornes = ([ord('a'), ord('z') + 1], [ord('A'), ord('Z') + 1])[triplet[2].isupper()]
elem3 = range(max(ord(triplet[2]) - delta[2], bornes[0]), min(ord(triplet[2]) + delta[2] + 1, bornes[1]))
return [elem1, elem2, elem3]
def normalisePatterns(patterns, prox1, prox2):
pat = [normaliseTriplet(x.split(', ')) for x in patterns]
P1 = rangefy(pat[0], prox1)
listP2 = [rangefy(pat[i], prox2) for i in range(1, len(patterns))]
return P1, listP2
description = "(a, S, 25), (e, c, 4), (z, z, 3), (b, S, 24), (z, d, 3), (c, d, 4), (a, S, 26) "
patterns = ["a, S, 22", "d, d, 4", "a, S, 25"]
description = "(1, T, 1), (1, T, 1), (1, T, 1), (1, T, 1) "
patterns = ["1, T, 1", "1, T, 1"]
tripletsStrings = re.findall("\([a-zA-Z0-9 ,]*\)", description)
#print "tripletsStrings=" + str(tripletsStrings)
listTriplets = [x[1:-1].split(', ') for x in tripletsStrings]
#print "listTriplets="+ str(listTriplets)
# Test the shape of the pattern (int|letter, int|letter, int|letter)
if listTriplets[0][0][0] in digits: isIntElem1 = True
else: isIntElem1 = False
if listTriplets[0][1][0] in digits: isIntElem2 = True
else: isIntElem2 = False
if listTriplets[0][2][0] in digits: isIntElem3 = True
else: isIntElem3 = False
print str(isIntElem1)+ str(isIntElem2)+ str(isIntElem3)
desc = [normaliseTriplet(x[1:-1].split(', ')) for x in tripletsStrings]
print "desc="+ str(desc)
prox1 = [2, 0, 3]
prox2 = [1, 1, 1]
P1, listP2 = normalisePatterns(patterns, prox1, prox2)
#print "P1="+str(P1)
#print "listP2="+str(listP2)
listAllPattFound = []
for i in [i for i in range(0, len(desc)) if estProche(P1, desc[i])]: # si on a trouvé la 1ere partie du pattern
listPattFound = []
listy = []
indicePrecedent = i
# print "indicePrecedent="+str(indicePrecedent)
# print "tripletsStrings[i]="+tripletsStrings[i]
listPattFound.append([tripletsStrings[i], i])
for j in listP2: # pour chaque partie du pattern
indiceCourant=cherchePremier(desc, indicePrecedent, j)
listy.append(indiceCourant)
indicePrecedent = indiceCourant
# print 'listy='+str(listy)
if None not in listy :
for y in listy:
#print "tripletsStrings[y]="+tripletsStrings[y]
listPattFound.append([tripletsStrings[y], y])
listAllPattFound.append(listPattFound)
print listAllPattFound
# Pour éliminer ce cas où 2 patterns étaient trouvées au lieu d'un seul:
# description = "(1, T, 1), (1, T, 1), (1, T, 1) "
# patterns = ["1, T, 1", "1, T, 1"]
#
pattPrec = listAllPattFound[0]
listAllPattFoundFinal = []
for pattFound in listAllPattFound:
# si la derniere partie du pattern précédent est au même indice que la première partie du pattern courant
if pattPrec[-1][1] != pattFound[0][1]:
listAllPattFoundFinal.append(pattFound)
for pattFound in listAllPattFoundFinal:
string = ""
for patt in pattFound:
string = string + patt[0]
string = string + ' -- ('
for patt in pattFound:
string = string + str(patt[1]) + ', '
string = string[:-2] + ') --'
print string |
Partager