2022-01-24 16:47:29 +01:00
|
|
|
#!/usr/bin/env python3
|
2022-01-24 12:20:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import json
|
|
|
|
import argparse
|
2022-01-24 23:14:17 +01:00
|
|
|
from random import shuffle
|
2022-01-24 12:20:21 +01:00
|
|
|
|
2022-01-24 16:13:22 +01:00
|
|
|
|
2022-01-24 18:50:29 +01:00
|
|
|
def load_dict(dict_file):
|
2022-01-24 22:22:40 +01:00
|
|
|
try:
|
2022-01-24 18:50:29 +01:00
|
|
|
with open(dict_file, "r") as dico:
|
2022-01-24 16:52:59 +01:00
|
|
|
liste = json.load(dico)
|
2022-01-24 19:44:49 +01:00
|
|
|
return liste
|
2022-01-24 22:22:40 +01:00
|
|
|
except Exception as error:
|
|
|
|
print(f"Impossible d'ouvrir le fichier {dict_file} : {error}")
|
2022-01-24 19:44:49 +01:00
|
|
|
exit(2)
|
2022-01-24 16:13:22 +01:00
|
|
|
|
2022-01-24 12:20:21 +01:00
|
|
|
|
|
|
|
def noDouble(mots):
|
2022-01-24 19:30:24 +01:00
|
|
|
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
2022-01-24 12:20:21 +01:00
|
|
|
ndList = []
|
|
|
|
for mot in mots:
|
2022-01-25 11:41:57 +01:00
|
|
|
if len(list(mot)) == len(set(list(mot))):
|
2022-01-24 12:20:21 +01:00
|
|
|
ndList.append(mot)
|
2022-01-25 11:41:57 +01:00
|
|
|
return ndList
|
2022-01-24 12:20:21 +01:00
|
|
|
|
2022-01-24 22:22:40 +01:00
|
|
|
|
2022-01-25 15:00:08 +01:00
|
|
|
def firstTryFilter(mots, max_mots, pattern):
|
2022-01-24 20:16:31 +01:00
|
|
|
'''
|
|
|
|
First Try : sélectionne des mots ayant des chances de valider ou invalider
|
2022-01-25 15:00:08 +01:00
|
|
|
l'une des lettres les plus utilisée en français.
|
2022-01-24 20:16:31 +01:00
|
|
|
'''
|
2022-01-25 15:00:08 +01:00
|
|
|
if pattern:
|
|
|
|
return mots # Inhibe le filtre si on utilise aussi un pattern
|
|
|
|
else:
|
|
|
|
bestLetters = ['E', 'A', 'I', 'R', 'S', 'N', 'T', 'O', 'L', 'U']
|
|
|
|
bestWords = []
|
|
|
|
shuffle(mots)
|
|
|
|
mots = mots[:100]
|
|
|
|
for mot in mots:
|
|
|
|
for bestLetter in bestLetters:
|
|
|
|
if bestLetter in mot:
|
|
|
|
bestWords.append(mot)
|
|
|
|
bestWords = list(set(bestWords))
|
|
|
|
bestWords_noDouble = noDouble(bestWords) # Autant maximiser les chances
|
|
|
|
if len(bestWords_noDouble) > 0:
|
|
|
|
bestWords = bestWords_noDouble
|
|
|
|
bestWords = bestWords[:max_mots]
|
|
|
|
return bestWords
|
2022-01-24 12:20:21 +01:00
|
|
|
|
|
|
|
|
2022-01-24 18:39:40 +01:00
|
|
|
def resolv_fl(liste, firstletter):
|
2022-01-24 18:33:52 +01:00
|
|
|
# On ne garde que les mots qui commence par FirstLetter
|
2022-01-25 13:03:50 +01:00
|
|
|
firstletter = firstletter.upper()
|
2022-01-24 18:39:40 +01:00
|
|
|
return [mot for mot in liste if mot[0] == firstletter]
|
|
|
|
|
|
|
|
|
|
|
|
def resolv_len(liste, lenght):
|
|
|
|
# On ne garde que les mots qui ont la bonne longueur
|
|
|
|
return [mot for mot in liste if len(mot) == lenght]
|
2022-01-24 12:20:21 +01:00
|
|
|
|
|
|
|
|
2022-01-24 22:15:17 +01:00
|
|
|
def resolv_pattern(liste, pattern):
|
|
|
|
# Pattern : élimination des mots ne satisfaisant pas la pattern
|
2022-01-25 13:13:38 +01:00
|
|
|
pattern_dict, not_pattern_dict = {}, {}
|
2022-01-24 22:15:17 +01:00
|
|
|
for lettre in pattern:
|
2022-01-25 13:13:38 +01:00
|
|
|
if lettre != "." and lettre.isupper():
|
2022-01-24 22:15:17 +01:00
|
|
|
pattern_dict.update({pattern.index(lettre): lettre})
|
2022-01-25 13:13:38 +01:00
|
|
|
if lettre != "." and lettre.islower():
|
|
|
|
not_pattern_dict.update({pattern.index(lettre): lettre.upper()})
|
2022-01-24 22:15:17 +01:00
|
|
|
MotsOK, MotsKO = [], []
|
|
|
|
for mot in liste:
|
|
|
|
for key in pattern_dict:
|
|
|
|
if mot[key] == pattern_dict[key]:
|
|
|
|
MotsOK.append(mot)
|
|
|
|
else:
|
|
|
|
MotsKO.append(mot)
|
2022-01-25 13:13:38 +01:00
|
|
|
for key in not_pattern_dict:
|
|
|
|
if mot[key] == not_pattern_dict[key]:
|
|
|
|
MotsKO.append(mot)
|
|
|
|
MotsKO = set(MotsKO)
|
2022-01-24 22:15:17 +01:00
|
|
|
liste = [mot for mot in MotsOK if mot not in MotsKO]
|
|
|
|
liste = list(set(liste))
|
|
|
|
return liste
|
|
|
|
|
2022-01-24 21:05:06 +01:00
|
|
|
|
|
|
|
def resolv_kl(liste, lettres):
|
2022-01-24 22:22:40 +01:00
|
|
|
'''
|
|
|
|
Known Letters : élimination des mots ne comprenant pas
|
|
|
|
les lettres validées (hors pattern)
|
|
|
|
'''
|
2022-01-25 13:03:50 +01:00
|
|
|
lettres = list(set(list(lettres.upper())))
|
2022-01-24 21:05:06 +01:00
|
|
|
MotsRestants = []
|
|
|
|
for mot in liste:
|
2022-01-24 21:46:06 +01:00
|
|
|
if len(set(lettres).intersection(mot)) == len(lettres):
|
|
|
|
MotsRestants.append(mot)
|
2022-01-24 21:05:06 +01:00
|
|
|
return MotsRestants
|
|
|
|
|
2022-01-24 12:20:21 +01:00
|
|
|
|
2022-01-24 21:46:06 +01:00
|
|
|
def resolv_bl(liste, lettres):
|
2022-01-24 22:22:40 +01:00
|
|
|
'''
|
|
|
|
Bad Letters : élimination des mots contenant
|
|
|
|
des lettres invalidées (hors pattern)
|
|
|
|
'''
|
2022-01-25 13:03:50 +01:00
|
|
|
lettres = list(set(list(lettres.upper())))
|
2022-01-24 21:46:06 +01:00
|
|
|
MotsSuppr = []
|
|
|
|
for mot in liste:
|
|
|
|
for lettre in lettres:
|
|
|
|
if lettre in mot:
|
|
|
|
MotsSuppr.append(mot)
|
2022-01-25 12:59:08 +01:00
|
|
|
return [mot for mot in liste if mot not in MotsSuppr]
|
2022-01-24 16:13:22 +01:00
|
|
|
|
2022-01-24 18:50:29 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-01-24 19:44:49 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Cheating with SUTOM'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-d',
|
|
|
|
'--dictionary',
|
|
|
|
help='Dictionnaire des mots du jeu',
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-fl',
|
|
|
|
'--firstLetter',
|
|
|
|
help='Première lettre',
|
2022-01-24 23:14:17 +01:00
|
|
|
default=False
|
2022-01-24 19:44:49 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-nb',
|
|
|
|
'--nbLetters',
|
|
|
|
type=int,
|
|
|
|
help='Nombre de lettres, un entier',
|
|
|
|
default=False,
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-kl',
|
|
|
|
'--knownLetters',
|
|
|
|
default=False,
|
|
|
|
help='Lettres connues, hors première, sans espaces : -kl BE'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-bl',
|
|
|
|
'--badLetters',
|
|
|
|
default=False,
|
|
|
|
help='Lettres non valides, sans espaces : -bl AKL'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-p',
|
|
|
|
'--pattern',
|
|
|
|
default=False,
|
2022-01-25 13:16:38 +01:00
|
|
|
help='Placement : bien placée en majuscule, mal placée en minuscule et non existante par un \'.\'. E.g. : Ra..e.T'
|
2022-01-24 19:44:49 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-nd',
|
|
|
|
'--noDoubleLetters',
|
|
|
|
help="Option pour ne chercher que des mots ne contenant qu'une occurence de chaque lettre",
|
|
|
|
default=False,
|
|
|
|
action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-ft',
|
|
|
|
'--firstTry',
|
|
|
|
default=False,
|
|
|
|
type=int,
|
|
|
|
help="Sélectionne des mots commençants par firstLetter et composés de ft voyelles différentes: -ft 4",
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2022-01-24 22:22:40 +01:00
|
|
|
|
2022-01-24 22:15:17 +01:00
|
|
|
if args.pattern and args.nbLetters and len(args.pattern) != args.nbLetters:
|
2022-01-24 19:44:49 +01:00
|
|
|
print("Tu as merdé ta pattern petit scarabé !")
|
|
|
|
exit(2)
|
2022-01-24 22:22:40 +01:00
|
|
|
|
2022-01-24 18:50:29 +01:00
|
|
|
liste = load_dict(args.dictionary)
|
2022-01-24 19:44:49 +01:00
|
|
|
liste = resolv_len(liste, args.nbLetters)
|
2022-01-24 23:14:17 +01:00
|
|
|
if args.firstLetter:
|
|
|
|
liste = resolv_fl(liste, args.firstLetter)
|
|
|
|
if args.pattern:
|
|
|
|
liste = resolv_pattern(liste, args.pattern)
|
2022-01-24 19:30:24 +01:00
|
|
|
if args.noDoubleLetters:
|
|
|
|
liste = noDouble(liste)
|
2022-01-24 23:14:17 +01:00
|
|
|
if args.knownLetters:
|
|
|
|
liste = resolv_kl(liste, args.knownLetters)
|
|
|
|
if args.badLetters:
|
|
|
|
liste = resolv_bl(liste, args.badLetters)
|
2022-01-24 20:16:31 +01:00
|
|
|
if args.firstTry:
|
2022-01-25 15:00:08 +01:00
|
|
|
liste = firstTryFilter(liste, args.firstTry, args.pattern)
|
2022-01-24 20:16:31 +01:00
|
|
|
if len(liste) == 1:
|
|
|
|
print(f"Essaie : \"{liste[0]}\"")
|
|
|
|
elif len(liste) > 1:
|
|
|
|
print(f"Mots à tester : {liste}")
|
|
|
|
else:
|
|
|
|
print("Hmmm... Je crois qu'on s'est perdu...")
|
|
|
|
exit(0)
|
2022-01-24 19:05:38 +01:00
|
|
|
if len(liste) > 1:
|
|
|
|
print(liste)
|
|
|
|
print(f"Il y a {len(liste)} mots dans la liste")
|
|
|
|
elif len(liste) == 1:
|
|
|
|
print(f"La solution est \"{liste[0]}\".")
|
|
|
|
else:
|
|
|
|
print("Hmmm... Je crois qu'on s'est perdu...")
|