feat: Initial commit
This commit is contained in:
67
simulazioni/murphy/murphy.py
Normal file
67
simulazioni/murphy/murphy.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from string import punctuation
|
||||
|
||||
|
||||
def carica_leggi(filename: str):
|
||||
try:
|
||||
fio = open(filename, "r")
|
||||
|
||||
leggi = {}
|
||||
|
||||
titolo = fio.readline()
|
||||
|
||||
while titolo != "":
|
||||
linea = fio.readline()
|
||||
legge = ""
|
||||
|
||||
while linea != "\n" and linea != "":
|
||||
legge += linea.strip()
|
||||
linea = fio.readline()
|
||||
|
||||
leggi[titolo.strip()] = legge
|
||||
|
||||
titolo = fio.readline()
|
||||
|
||||
fio.close()
|
||||
except FileNotFoundError:
|
||||
exit(f"Errore: Il file {filename} non è stato trovato!")
|
||||
except IOError:
|
||||
exit("Errore: Si è verificato un problema durante la lettura del file!")
|
||||
|
||||
return leggi
|
||||
|
||||
|
||||
def carica_argomenti(filename: str):
|
||||
try:
|
||||
fio = open(filename, "r")
|
||||
argomenti = [arg.strip() for arg in fio]
|
||||
fio.close()
|
||||
except FileNotFoundError:
|
||||
exit(f"Errore: Il file {filename} non è stato trovato!")
|
||||
return argomenti
|
||||
|
||||
|
||||
def rimuovi_punteggiatura(stringa: str):
|
||||
for p in punctuation:
|
||||
stringa = stringa.replace(p, " ")
|
||||
|
||||
return stringa
|
||||
|
||||
|
||||
def main():
|
||||
leggi = carica_leggi("leggi_di_Murphy.txt")
|
||||
argomenti = carica_argomenti("argomenti.txt")
|
||||
|
||||
for legge in leggi:
|
||||
testo_legge = leggi[legge]
|
||||
parole = rimuovi_punteggiatura(testo_legge.lower()).split()
|
||||
|
||||
for argomento in argomenti:
|
||||
if argomento in parole:
|
||||
if len(testo_legge) > 50:
|
||||
testo_legge = testo_legge[:50] + "..."
|
||||
print(legge, "-", testo_legge)
|
||||
break
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user