Commit 718fb2df authored by Michael Nauge's avatar Michael Nauge
Browse files

Update amalgameSolveur.ipynb

amélioration de fonctions en réutilisant la fonction isSolveByConcat
parent 375d623d
%% Cell type:markdown id: tags:
### Operateurs
* clip
* overlap
* concat (c'est un overlap(0)
* clip
* split
%% Cell type:markdown id: tags:
# isSolveByConcat
%% Cell type:code id: tags:
``` python
def isSolveByConcat(targetAmalgame, mot1, mot2):
generatedAmalgame = mot1+mot2
#print("generatedAmalgame",generatedAmalgame)
if generatedAmalgame == targetAmalgame:
return True
return False
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abcde"
mot1 = "abc"
mot2 = "de"
isSolveByConcat(targetAmalgame, mot1, mot2)
```
%% Output
True
%% Cell type:markdown id: tags:
# isSolveByClip1Concat
%% Cell type:code id: tags:
``` python
def isSolveByClip1Concat(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 1 en coupant par la queue
for lenClipCur in range(len(mot1), 0,-1):
C_mot1 = mot1[0:lenClipCur]
#print("C_mot1 : ",C_mot1)
generatedAmalgame = C_mot1+mot2
#print(generatedAmalgame)
if generatedAmalgame == targetAmalgame:
# on test si ça marche avec une concat
solved = isSolveByConcat(targetAmalgame, C_mot1, mot2)
if solved:
deletedSegmentMot1 = mot1[lenClipCur:]
return (True,deletedSegmentMot1)
return (False,"")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abascon"
mot1 = "abasourdi"
mot2 = "con"
isSolveByClip1Concat(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'ourdi')
%% Cell type:markdown id: tags:
A : acquihire [ˌækwɪˈhaɪ‿ə]
1 : acquisition [ˌækwɪˈzɪʃən]
2 : hire [ˈhaɪ‿ə]
%% Cell type:code id: tags:
``` python
targetAmalgame = "ækwɪhaɪ‿ə"
mot1 = "ækwɪzɪʃən"
mot2 = "haɪ‿ə"
isSolveByClip1Concat(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'zɪʃən')
%% Cell type:markdown id: tags:
# isSolveByClip2Concat
%% Cell type:code id: tags:
``` python
def isSolveByClip2Concat(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 2 en coupant par la tete
for lenClipCur in range(0,len(mot2)):
C_mot2 = mot2[lenClipCur:]
#print("C_mot2 : ",C_mot2)
generatedAmalgame = mot1+C_mot2
#print(generatedAmalgame)
if generatedAmalgame == targetAmalgame:
# on test si ça marche avec une concat
solved = isSolveByConcat(targetAmalgame, C_mot1, mot2)
if solved:
deletedSegmentMot2 = mot2[0:lenClipCur]
return (True,deletedSegmentMot2)
return (False,"")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abcde"
mot1 = "abc"
mot2 = "fgde"
isSolveByClip2Concat(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'fg')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-2ec9f724c3ab> in <module>
3 mot2 = "fgde"
4
----> 5 isSolveByClip2Concat(targetAmalgame, mot1, mot2)
<ipython-input-7-21100710eb4f> in isSolveByClip2Concat(targetAmalgame, mot1, mot2)
7
8 # on test si ça marche avec une concat
----> 9 solved = isSolveByConcat(targetAmalgame, C_mot1, mot2)
10 if solved:
11 deletedSegmentMot2 = mot2[0:lenClipCur]
NameError: name 'C_mot1' is not defined
%% Cell type:markdown id: tags:
# isSolveByOverlap
%% Cell type:code id: tags:
``` python
def isSolveByOverlap(targetAmalgame, mot1, mot2):
incrementMot1 = 0
# swo : sizeWindowsOverlap
for swoCur in range(len(mot1), 0, -1):
segmentMot1 = mot1[incrementMot1:]
segmentMot2 = mot2[0:swoCur]
#print("segmentMot1",segmentMot1)
#print("segmentMot2",segmentMot2)
if segmentMot1==segmentMot2:
segmentOverlap = segmentMot1
generatedAmalgame = mot1+mot2[swoCur:]
#print("generatedAmalgame", generatedAmalgame)
if generatedAmalgame == targetAmalgame:
return (True,segmentOverlap)
incrementMot1+=1
return (False, "")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abascon"
mot1 = "abas"
mot2 = "ascon"
isSolveByOverlap(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'as')
%% Cell type:markdown id: tags:
# isSolveByClip1Overlap(targetAmalgame, mot1, mot2)
%% Cell type:code id: tags:
``` python
def isSolveByClip1Overlap(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 1 en coupant par la queue
for lenClipCur in range(len(mot1), 0,-1):
C_mot1 = mot1[0:lenClipCur]
#print("C_mot1 : ",C_mot1)
# gestion overlap
# --------------------
(solved, findedOverlap) = isSolveByOverlap(targetAmalgame, C_mot1, mot2)
if solved:
deletedSegmentMot1 = mot1[lenClipCur:]
return (solved, findedOverlap, deletedSegmentMot1)
return (False, "", "")
```
%% Cell type:markdown id: tags:
A : cybrarian [saɪˈbreəri‿ən]
1 : cyber [ˈsaɪbə]
2 : librarian [laɪˈbreəri‿ən]
%% Cell type:code id: tags:
``` python
targetAmalgame = "saɪbreəri‿ən"
mot1 = "saɪbə"
mot2 = "aɪbreəri‿ən"
isSolveByClip1Overlap(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'aɪb', 'ə')
%% Cell type:markdown id: tags:
# isSolveByClip2Overlap(targetAmalgame, mot1, mot2)
%% Cell type:code id: tags:
``` python
def isSolveByClip2Overlap(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 2 en coupant par la tete
for lenClipCur in range(0,len(mot2)):
C_mot2 = mot2[lenClipCur:]
#print("C_mot2 : ",C_mot2)
# gestion overlap
# --------------------
(solved, findedOverlap) = isSolveByOverlap(targetAmalgame, mot1, C_mot2)
if solved:
deletedSegmentMot2 = mot2[0:lenClipCur]
return (solved, findedOverlap, deletedSegmentMot2)
return (False, "", "")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abcdemnop"
mot1 = "abcde"
mot2 = "fgdemnop"
isSolveByClip2Overlap(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'de', 'fg')
%% Cell type:markdown id: tags:
# isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2)
%% Cell type:code id: tags:
``` python
def isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 1 en coupant par la queue
for lenClip1Cur in range(len(mot1), 0,-1):
C_mot1 = mot1[0:lenClip1Cur]
#print("C_mot1 : ",C_mot1)
# on test tous les clip de mot 2 en coupant par la tete
for lenClip2Cur in range(0,len(mot2)):
C_mot2 = mot2[lenClip2Cur:]
#print("C_mot2 : ",C_mot2)
# on test l'overlap sur toutes les tailles de fenetre
# --------------------
(solved, findedOverlap) = isSolveByOverlap(targetAmalgame, C_mot1, C_mot2)
if solved:
deletedSegmentMot1 = mot1[lenClip1Cur:]
deletedSegmentMot2 = mot2[0:lenClip2Cur]
return (solved, findedOverlap, deletedSegmentMot1, deletedSegmentMot2)
return (False,"","","")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "abcdemnop"
mot1 = "abcderz"
mot2 = "fgdemnop"
isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'de', 'rz', 'fg')
%% Cell type:markdown id: tags:
A : cybrarian [saɪˈbreəri‿ən]
1 : cyber [ˈsaɪbə]
2 : librarian [laɪˈbreəri‿ən]
%% Cell type:code id: tags:
``` python
targetAmalgame = "saɪbreəri‿ən"
mot1 = "saɪbə"
mot2 = "laɪbreəri‿ən"
isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'aɪb', 'ə', 'l')
%% Cell type:markdown id: tags:
# isSolveByClip1Clip2Concat(targetAmalgame, mot1, mot2)
%% Cell type:code id: tags:
``` python
def isSolveByClip1Clip2Concat(targetAmalgame, mot1, mot2):
# on test tous les clip de mot 1 en coupant par la queue
for lenClip1Cur in range(len(mot1), 0,-1):
C_mot1 = mot1[0:lenClip1Cur]
#print("C_mot1 : ",C_mot1)
# on test tous les clip de mot 2 en coupant par la tete
for lenClip2Cur in range(0,len(mot2)):
C_mot2 = mot2[lenClip2Cur:]
#print("C_mot2 : ",C_mot2)
# on test la concat
# --------------------
solved = isSolveByConcat(targetAmalgame, C_mot1, C_mot2)
if solved:
deletedSegmentMot1 = mot1[lenClip1Cur:]
deletedSegmentMot2 = mot2[0:lenClip2Cur]
return (solved, deletedSegmentMot1, deletedSegmentMot2)
return (False,"","")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "flɒg"
mot1 = "feɪk"
mot2 = "blɒg"
isSolveByClip1Clip2Concat(targetAmalgame, mot1, mot2)
```
%% Output
(True, 'eɪk', 'b')
%% Cell type:markdown id: tags:
# solveAmalgame(targetAmalgame, mot1, mot2):
%% Cell type:code id: tags:
``` python
def solveAmalgameV0(targetAmalgame, mot1, mot2):
# on va essayer tous les solveurs les uns après les autres
#isSolveByClip1Clip2Concat
(solved, deletedSegmentMot1, deletedSegmentMot2) = isSolveByClip1Clip2Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Clip2Concat !", "deletedSegmentMot1",deletedSegmentMot1,"deletedSegmentMot2", deletedSegmentMot2)
# isSolveByClip1Clip2Overlap
(solved, findedOverlap, deletedSegmentMot1, deletedSegmentMot2) = isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Clip2Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot1",deletedSegmentMot1,"deletedSegmentMot2", deletedSegmentMot2)
#isSolveByClip2Overlap
(solved, findedOverlap, deletedSegmentMot2)=isSolveByClip2Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip2Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot2", deletedSegmentMot2)
# isSolveByClip1Overlap
(solved, findedOverlap, deletedSegmentMot1) = isSolveByClip1Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot1", deletedSegmentMot1)
#isSolveByOverlap
(solved,segmentOverlap)= isSolveByOverlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByOverlap !", "findedOverlap", findedOverlap)
#isSolveByClip2Concat(targetAmalgame, mot1, mot2)
(solved,deletedSegmentMot2) = isSolveByClip2Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip2Concat !", "deletedSegmentMot2", deletedSegmentMot2)
#isSolveByClip1Concat(targetAmalgame, mot1, mot2)
(solved,deletedSegmentMot1) = isSolveByClip1Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Concat !", "deletedSegmentMot1", deletedSegmentMot1)
# isSolveByConcat
solved = isSolveByConcat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByConcat !")
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "fɪtspəˈreɪʃən"
mot1 = "fɪt"
mot2 = "ɪnspəˈreɪʃən"
solveAmalgameV0(targetAmalgame, mot1, mot2)
```
%% Output
>> isSolveByClip1Clip2Concat ! deletedSegmentMot1 deletedSegmentMot2 ɪn
>> isSolveByClip2Concat ! deletedSegmentMot2 ɪn
%% Cell type:code id: tags:
``` python
#targetAmalgame = "ˌækwɪˈhaɪ‿ə"
#mot1 = "ˌækwɪˈzɪʃən"
#mot2 = "ˈhaɪ‿ə"
#overlap trop court
targetAmalgame = "ækwɪhaɪ‿ə"
mot1 = "ækwɪzɪʃən"
mot2 = "wɪhaɪ‿ə"
solveAmalgameV0(targetAmalgame, mot1, mot2)
```
%% Output
>> isSolveByClip1Clip2Concat ! deletedSegmentMot1 zɪʃən deletedSegmentMot2 wɪ
>> isSolveByClip1Clip2Overlap ! findedOverlap wɪ deletedSegmentMot1 zɪʃən deletedSegmentMot2
>> isSolveByClip1Overlap ! findedOverlap wɪ deletedSegmentMot1 zɪʃən
>> isSolveByClip1Concat ! deletedSegmentMot1 wɪzɪʃən
%% Cell type:markdown id: tags:
Attention l'ordre de test est important pour trouver la meilleure solution, la solution préféré étant l'overlap avec un minimun de clip
* isSolveByOverlap
* isSolveByClip1Overlap
* isSolveByClip2Overlap
* isSolveByClip1Clip2Overlap
* isSolveByConcat
* isSolveByClip1Concat
* isSolveByClip2Concat
* isSolveByClip1Clip2Concat
%% Cell type:code id: tags:
``` python
def solveAmalgame(targetAmalgame, mot1, mot2):
# on va essayer tous les solveurs les uns après les autres mais dans un ordre bien choisi
#isSolveByOverlap
(solved,segmentOverlap)= isSolveByOverlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByOverlap !", "findedOverlap", findedOverlap)
# isSolveByClip1Overlap
(solved, findedOverlap, deletedSegmentMot1) = isSolveByClip1Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot1", deletedSegmentMot1)
#isSolveByClip2Overlap
(solved, findedOverlap, deletedSegmentMot2)=isSolveByClip2Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip2Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot2", deletedSegmentMot2)
# isSolveByClip1Clip2Overlap
(solved, findedOverlap, deletedSegmentMot1, deletedSegmentMot2) = isSolveByClip1Clip2Overlap(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Clip2Overlap !", "findedOverlap", findedOverlap, "deletedSegmentMot1",deletedSegmentMot1,"deletedSegmentMot2", deletedSegmentMot2)
# isSolveByConcat
solved = isSolveByConcat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByConcat !")
#isSolveByClip1Concat(targetAmalgame, mot1, mot2)
(solved,deletedSegmentMot1) = isSolveByClip1Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Concat !", "deletedSegmentMot1", deletedSegmentMot1)
#isSolveByClip2Concat(targetAmalgame, mot1, mot2)
(solved,deletedSegmentMot2) = isSolveByClip2Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip2Concat !", "deletedSegmentMot2", deletedSegmentMot2)
#isSolveByClip1Clip2Concat
(solved, deletedSegmentMot1, deletedSegmentMot2) = isSolveByClip1Clip2Concat(targetAmalgame, mot1, mot2)
if solved:
print(">> isSolveByClip1Clip2Concat !", "deletedSegmentMot1",deletedSegmentMot1,"deletedSegmentMot2", deletedSegmentMot2)
```
%% Cell type:code id: tags:
``` python
targetAmalgame = "ækwɪhaɪ‿ə"
mot1 = "ækwɪzɪʃən"
mot2 = "wɪhaɪ‿ə"
solveAmalgame(targetAmalgame, mot1, mot2)
```
%% Output
>> isSolveByClip1Overlap ! findedOverlap wɪ deletedSegmentMot1 zɪʃən
>> isSolveByClip1Clip2Overlap ! findedOverlap wɪ deletedSegmentMot1 zɪʃən deletedSegmentMot2
>> isSolveByClip1Concat ! deletedSegmentMot1 wɪzɪʃən
>> isSolveByClip1Clip2Concat ! deletedSegmentMot1 zɪʃən deletedSegmentMot2 wɪ
%% Cell type:markdown id: tags:
Attention il faut souvent supprimer les ˌ et ˈ qui bloquent le matching
%% Cell type:code id: tags:
``` python
targetAmalgame = "fɪtspəreɪʃən"
mot1 = "fɪt"
mot2 = "ɪnspəreɪʃən"
solveAmalgame(targetAmalgame, mot1, mot2)
```
%% Output
>> isSolveByClip2Concat ! deletedSegmentMot2 ɪn
>> isSolveByClip1Clip2Concat ! deletedSegmentMot1 deletedSegmentMot2 ɪn
%% Cell type:code id: tags:
``` python
targetAmalgame = "saɪbreəriən"
mot1 = "saɪbə"
mot2 = "laɪbreəriən"
solveAmalgame(targetAmalgame, mot1, mot2)
```
%% Output
>> isSolveByClip1Clip2Overlap ! findedOverlap aɪb deletedSegmentMot1 ə deletedSegmentMot2 l
>> isSolveByClip1Clip2Concat ! deletedSegmentMot1 ə deletedSegmentMot2 laɪb
%% Cell type:markdown id: tags:
# todo : cas de l'inclusion
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment