akademie_2024_scitani_hlasu.../verze/sms_count_2.py
2024-12-12 17:11:39 +01:00

169 lines
4.6 KiB
Python

import xml.etree.ElementTree as ET
from unidecode import unidecode #### ! ! ! pip install Unidecode ! ! ! !
from datetime import datetime
import time
# this code processes sms messages from locally exported xml table from "sms backup & restore" app on android
##############################################################################################
# ! important settings ! #####################################################################
soubor = 'sms_export/sms-20241116201536.xml'
time_after = 1
# + spravne nastavit kandidati o par radku nize!!!!###########################################
##############################################################################################
ted = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
zaznam_file = f"zaznamy/bezec_{ted}.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 a podle posledni od telefonniho cisla
tree = ET.parse(soubor)
root = tree.getroot()
hlasy = [] # = relevant sms
volici = {} # = pocet sms na volice/telefonni cislo
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:
if author not in volici:
volici[author] = 1
relevant += 1
hlasy.append(Sms_hlas(author, content, [])) #obsah listu
else:
volici[author] += 1
else:
continue
return(hlasy, sms_count, relevant, volici)
zpracovane = zpracuj_sms()
sms_count = zpracovane[1]
relevant = zpracovane[2]
hlasy = zpracovane[0]
volici = zpracovane[3]
###
###
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):
global volici
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} ----- posledni z {volici[i.author]}\n")
def prite(zaznam, msg):
zaznam.write(f"{msg}\n")
print(msg)
###
###
file = open(zaznam_file, 'a')
zapis_do_zaznamu(file, "PLATNE HLASY", platne_hlasy) # jeden kandidat
zapis_do_zaznamu(file, "NEPLTANE HLASY", neplatne_hlasy) # vice jak jeden kandidat
zapis_do_zaznamu(file, "PRAZDNE HLASY", prazdne_hlasy) # zadny kandidat
zapis_do_zaznamu(file, "VSECHNY HLASY", zpracovane_hlasy) # vsechny hlasy po urcitem datu a posledni co telefonni cislo poslalo
file.write("\n\n\n\nCOMMAND PROMPT OUTPUT")
prite(file, "\n\n- - - - - - - - - - - - - - - - - - -")
prite(file, f"sms:{sms_count} relevant:{relevant}-{len(hlasy)}")
prite(file, f"platne:{len(platne_hlasy)} neplatne:{len(neplatne_hlasy)} prazdne:{len(prazdne_hlasy)}")
prite(file, f"ze souboru: {soubor} od casu: {time_after}\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, "- - - - - - - - - - - - - - - - - - - ")