-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplonutils.py
56 lines (48 loc) · 2.09 KB
/
simplonutils.py
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
import regex as re
def almost(mot: str, s: str) -> list:
words = ["".join([mot[j] if j != i else mot[j]+"?" for j in range(len(mot))]) for i in range(len(mot))]
searchRegex = re.compile(r'\b('+ r"|".join(words) + r')\b', flags=re.IGNORECASE)
return searchRegex.findall(s, overlapped=True)
# print(almost("Jaune", "Ma voiture jaune est pas toute jeune mais je l'ai acheté en June et je l'appelle jarune iaune"))
def pluslarge(mot: str, s: str) -> list:
words = ["".join([mot[j] if j != i else mot[j]+"?.{0,1}" for j in range(len(mot))]) for i in range(len(mot))]
searchRegex = re.compile(r'\b('+ r"|".join(words) + r'|\w' + mot + r')\b', flags=re.IGNORECASE)
print(searchRegex)
return searchRegex.findall(s, overlapped=True)
# print(pluslarge("le", "ce me el e superbe"))
def score(p: str, s: str) -> int:
words = p.split(' ')
score: int = 0
for word in words:
if word.isalnum():
matches = pluslarge(word, s)
print(matches)
for m in matches:
if m == word:
score += 5
else:
score += 1
return score
# print(score("Le petit bonhomme en mousse", "Ce superbe matelas en mousse naturelle"))
def score2(p: str, s: str) -> int:
words = p.split(' ')
score: int = 0
lastword = ''
i = 0
for word in words:
if word.isalnum():
matches = pluslarge(word, s)
for m in matches:
if m.lower() == word.lower():
score += 5
if lastword != '':
occurences = len(re.findall(lastword + ' ' + m, s, flags=re.IGNORECASE))
if occurences > 0:
score += 20 * occurences
lastword = word
else:
score += 1
i += 1
return score
# print(score2("Ma voiture préféré est en jaune !", "Ma voiture jaune est pas toute jeune mais je l'ai acheté en June et je l'appelle jarune iaune"))
# print(pluslarge('trois', 'Les etrois tris, lys trois gros, les troisx roi.'))