Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
| Prossima revisione | Revisione precedente | ||
|
distribuzione_delle_frequenze_senza_nltk [2021/05/20 07:21] 127.0.0.1 external edit |
distribuzione_delle_frequenze_senza_nltk [2021/06/02 18:15] (versione attuale) admin |
||
|---|---|---|---|
| Linea 1: | Linea 1: | ||
| - | ====== Distribuzione delle frequenze | + | ====== Distribuzione delle frequenze ====== |
| Esemplificazione su una singola frase. Si noti, innanzitutto, | Esemplificazione su una singola frase. Si noti, innanzitutto, | ||
| <code python> | <code python> | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | frase=u' | + | frase=' |
| - | print frase | + | print(frase) |
| + | #Se si è in ambiente shell | ||
| + | # | ||
| + | #anziché utilizzare il comando “print” è possibile digitare solamente | ||
| + | #il nome della variabile di cui si vuole ispezionare il valore. | ||
| + | #Ad es., anziché digitare “print(frase)”INVIO basta digitare “frase”INVIO. | ||
| frase=frase.split() | frase=frase.split() | ||
| - | print frase | + | print(frase) |
| - | print len(frase) | + | print(len(frase)) |
| - | print set(frase) | + | print(set(frase)) |
| - | print len(set(frase)) | + | print(len(set(frase))) |
| - | print frase.count(' | + | print(frase.count(' |
| fd=[frase.count(i) for i in set(frase)] | fd=[frase.count(i) for i in set(frase)] | ||
| - | print fd | + | print(fd) |
| fd.sort(reverse=True) | fd.sort(reverse=True) | ||
| - | print fd | + | print(fd) |
| - | print range(10) | + | sequenza=range(1,len(fd)+1) |
| - | print range(1,11) | + | for i in sequenza: |
| - | plt.plot(range(1, | + | plt.plot(sequenza,fd,'.') |
| plt.show() | plt.show() | ||
| </ | </ | ||
| - | |||
| ---- | ---- | ||
| Applicazione al testo ' | Applicazione al testo ' | ||
| <code python> | <code python> | ||
| - | import codecs | ||
| import re | import re | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | file=codecs.open('Testi/ | + | with open('testi/ |
| - | testo=file.read() | + | testo=testo[1704:] |
| - | + | ||
| - | testo=testo[1758:] | + | |
| testo=re.sub("'","' | testo=re.sub("'","' | ||
| testo=re.sub(" | testo=re.sub(" | ||
| testo=testo.lower() | testo=testo.lower() | ||
| testo=testo.split() | testo=testo.split() | ||
| - | |||
| fd=[testo.count(i) for i in set(testo)] | fd=[testo.count(i) for i in set(testo)] | ||
| fd.sort(reverse=True) | fd.sort(reverse=True) | ||
| Linea 48: | Linea 48: | ||
| frase=frase.split() | frase=frase.split() | ||
| occ_forma=[(frase.count(i), | occ_forma=[(frase.count(i), | ||
| - | print occ_forma | + | print(occ_forma) |
| occ_forma.sort(reverse=True) | occ_forma.sort(reverse=True) | ||
| - | print occ forma | + | print (occ_forma) |
| fd=[i[0] for i in occ_forma] | fd=[i[0] for i in occ_forma] | ||
| forme=[i[1] for i in occ_forma] | forme=[i[1] for i in occ_forma] | ||