import xml.etree.ElementTree as ET from unidecode import unidecode #### ! ! ! pip install Unidecode ! ! ! ! #processes sms messages from locally exported xml table from "sms backup & restore" app on android # important settings ! soubor = 'sms-20241116201536.xml' time_after = 1731312702226 zaznam_file = "relevant_sms_test1.txt" # kandidati = {"prima" : 0, "sekunda" : 0, "tercie" : 0, "kvarta" : 0, "kvinta" : 0, "sexta" : 0, "septima" : 0, "oktava" : 0, "1a" : 0, "2a" : 0, "3a" : 0, "4a" : 0} kandidati = {"bogus" : 0, "sigma" : 0, "ahoj" : 0} # important settings ! ### ### class Sms_hlas(): def __init__(self, author, content, kandidati): self.author = author self.content = content self.kandidati = kandidati ### ### def zpracuj_sms(): #podle data tree = ET.parse(soubor) root = tree.getroot() hlasy = [] # = relevant sms sms_count = 0 relevant = 0 for child in root: sms_count += 1 date = int(child.get('date')) content = unidecode(child.get('body')).lower() author = child.get('address') if date >= time_after: relevant += 1 hlasy.append(Sms_hlas(author, content, [])) #obsah listu else: continue return(hlasy, sms_count, relevant) zpracovane = zpracuj_sms() sms_count = zpracovane[1] relevant = zpracovane[2] hlasy = zpracovane[0] ### ### def najdi_kandidaty(hlasy, kandidati): for h in hlasy: for k in kandidati: if k in h.content: h.kandidati.append(k) else: continue return(hlasy) zpracovane_hlasy = najdi_kandidaty(hlasy, kandidati) ### ### def spocitej_kandidaty(zprac_hlasy, kandidati): neplatne_hlasy = [] platne_hlasy = [] prazdne_hlasy = [] for h in zprac_hlasy: if len(h.kandidati) > 1: neplatne_hlasy.append(h) elif len(h.kandidati) == 1: for k in kandidati: if k in h.kandidati: kandidati[k] += 1 platne_hlasy.append(h) else: continue else: prazdne_hlasy.append(h) return(kandidati, neplatne_hlasy, platne_hlasy, prazdne_hlasy) spocitane = spocitej_kandidaty(zpracovane_hlasy, kandidati) kandidati = spocitane[0] neplatne_hlasy = spocitane[1] platne_hlasy = spocitane[2] prazdne_hlasy = spocitane[3] ### ### def zapis_do_zaznamu(zaznam, nazev, list): zaznam.write(f"\n\n\n{nazev} - {len(list)}\n") for i in list: zaznam.write(f"Autor: {i.author} ----- Kandidati: {i.kandidati} ----- Content: {i.content}\n") def prite(zaznam, msg): zaznam.write(f"{msg}\n") print(msg) ### ### file = open(zaznam_file, 'a') zapis_do_zaznamu(file, "ZAZNAMENANE HLASY", zpracovane_hlasy) zapis_do_zaznamu(file, "PLATNE HLASY", platne_hlasy) zapis_do_zaznamu(file, "NEPLTANE HLASY", neplatne_hlasy) zapis_do_zaznamu(file, "PRAZDNE HLASY", prazdne_hlasy) prite(file, "\n\n- - - - - - - - - - - - - - - - - - -") prite(file, f"sms:{sms_count} relevant(in time):{relevant}-{len(hlasy)}") prite(file, f"platne:{len(platne_hlasy)} neplatne:{len(neplatne_hlasy)} prazdne:{len(prazdne_hlasy)}\n") prite(file, "VYSLEDKY") sorted_items = sorted(kandidati.items(), key=lambda kv: (kv[1], kv[0])) for k in sorted_items: prite(file, f"{k[0]} -> {k[1]}") prite(file, "- - - - - - - - - - - - - - - - - - - ")