How to make an omissis quiz with python with an html code output

This is the code to trasform a phrase into an omissis quiz

import re
import random
from random import shuffle



def get_words(phrase:str) -> [list, list]:
	''' return the omissed words in order and shuffled '''
	path = r"(\w*)\*"
	words = re.findall(path, phrase)
	shuffled = words.copy()
	# shuffle(shuffled)
	# print(f"{words=}")
	# print(f"{shuffled=}")
	return words, shuffled

def set_html(shuffled) -> [str, str]:
	html = "<p>"
	for word in shuffled:
		html += word + " "
	doc = html
	html +="</p>"

	# print(html)
	return html, doc

def show_omissis_init(phrase:str, shuffled: list) -> [str, str]:
	for w in shuffled:
		phrase = phrase.replace(w, f"<b style='color:blue'>{w[0]}{'|_'*(len(w)-2)}|{w[-1]}</b>")
	doc = phrase
	phrase = f"<p>{phrase}</p>"
	return phrase, doc

def show_omissis_init_middle(phrase:str, shuffled: list) -> [str, str]:
	for w in shuffled:
		a = random.randrange(1, (len(w) - 1)) # numero a caso di lettera da mostrare
		phrase = phrase.replace(w, f"<b style='color:blue'>{w[0]}{'|_'*len(w[2:a+1])}|{w[a]}{'|_'*len(w[a+1:-1])}|{w[-1]}</b>")
	doc = phrase
	phrase = f"<p>{phrase}</p>"
	return phrase, doc


def show_omissis(phrase:str, shuffled: list) -> [str, str]:
	for w in shuffled:
		phrase = phrase.replace(w, "|"*len(w))
	doc = phrase
	phrase = f"<p>{phrase}</p>"
	return phrase, doc

def output(ph):
	words, shuffled = get_words(ph)
	html, doc = set_html(shuffled)
	html, doc = show_omissis_init_middle(ph2, shuffled)
	print(html)

# Put here the phrase to show without the omissis putting and * after the words to omiss
ph = "Il marketing è la funzione organizzativa* che ha il compito di individuare i target* e trovare il modo migliore per soddisfare* i loro bisogni* e desideri in modo profittevole all’interno di un ambiente competitivo*."
# do not change this. This deletes the *
ph2 = ph.replace("*", "")

output(ph)

This is the output

<p>Il marketing è la funzione <b style='color:blue'>o|_|_|a|_|_|_|_|_|_|_|_|a</b> che ha il compito di individuare i <b style='color:blue'>t|_|_|_|e|t</b> e trovare il modo migliore per <b style='color:blue'>s|_|d|_|_|_|_|_|_|e</b> i loro <b style='color:blue'>b|_|_|o|_|_|i</b> e desideri in modo profittevole all’interno di un ambiente <b style='color:blue'>c|_|_|_|_|t|_|_|_|_|o</b>.</p>

That if saves as html gives this output

where you got the omissed words with the starting and ending letter and a ramdom letter shown in the middle too.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.