Statistica VS Regole · •Term Frequency per Inverse Document Frequency ... Remoto + 3ª p....
Transcript of Statistica VS Regole · •Term Frequency per Inverse Document Frequency ... Remoto + 3ª p....
Alcune nozioni di base
• Token e Type
‘Il cane che uccise il gatto, che uccise il topo’
• 10 tokens: {il, cane, che, uccise, il, gatto, che, uccise, il, topo}
• 6 Types: • Il
• Cane
• Che
• Uccise
• Gatto
• Topo
Alcune nozioni di base
• Part-of-Speech e Lemma
• ‘Il cane che uccise il gatto, che uccise il topo’
DET N PRON V DET N PRON V DET N
Il cane che uccidere il gatto che uccidere il topo
Nozioni di Base
• Pre-Processing
• Normalizzazione del Testo: sostituzione Maiuscole, eliminazione
punteggiatura…
‘il cane che uccise il gatto che uccise il topo’
• Tokenization: trasformazione del testo in una Lista di Tokens
• Rimozione delle ‘Stop Word’:
‘Cane uccise gatto uccise topo’
La Statistica dei Testi
‘In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort.
It had a perfectly round door like a porthole, painted green, with a shiny yellow brass knob in the exact middle. The door opened on to a tube-shaped hall like a tunnel: a very comfortable tunnel without smoke, with panelled walls, and floors tiled and carpeted, provided with polished chairs, and lots and lots of pegs for hats and coats – the hobbit was fond of visitors. The tunnel wound on and on, going fairly but not quite straight into the side of the hill – The Hill, as all the people for many miles round called it – and many little round doors opened out of it, first on one side and then on another. No going upstairs for the hobbit: bedrooms, bathrooms, cellars, pantries (lots of these), wardrobes (he had whole rooms devoted to clothes), kitchens, dining-rooms, all were on the same floor, and indeed on the same passage. The best rooms were all on the left-hand side (going in), for these were the only ones to have windows, deep-set round windows looking over his garden and meadows beyond, sloping down to the river.’
Lo Hobbit, Capitolo 1
Calcolo delle occorrenze
the 17
and 12
a 11
on 9
of 6
to 6
in 5
it 5
with 5
for 4
round 4
all 3
going 3
hobbit 3
hole 3
the 6040
and 4406
of 2481
to 2087
a 1919
he 1879
in 1456
was 1385
they 1342
it 1152
that 1007
had 949
his 905
you 779
on 771
for 718
not 714
were 695
as 687
i 686
at 658
all 654
with 653
but 592
said 583
them 539
their 525
bilbo 503
there 503
the 17
and 12
a 11
on 9
of 6
to 6
in 5
it 5
with 5
for 4
round 4
all 3
going 3
hobbit 3
hole 3
the 6040
and 4406
of 2481
to 2087
a 1919
he 1879
in 1456
was 1385
they 1342
it 1152
that 1007
had 949
his 905
you 779
on 771
for 718
not 714
were 695
as 687
i 686
at 658
all 654
with 653
but 592
said 583
them 539
their 525
bilbo 503
there 503
Comparazione tra testi di
Dimensioni diverse
said 51
frodo 42
night 34
aragorn 31
river 29
boats 28
out 26
grat 25
dark 25
sam 24
said 34
frodo 30
gollum 28
up 28
down 21
sam 20
now 19
must 18
out 18
under 17
‘Il Signore degli
anelli – La
compagnia
dell’anello’,
capitolo 9
Tokens: 8243
‘Il signore degli
anelli - Le due
torri’, capitolo 7
Tokens: 4946
Calcolo della Frequenza
Parole occorrenze frequenze
Said 34 0,006874
Frodo 30 0,006066
Night 11 0,002224
Parole Occorrenze frequenze
Said 51 0,006187
Frodo 42 0,005095
Night 28 0,004125
Libro 1 capitolo 9 Libro 2 capitolo 7
𝑇𝐹 =𝑛𝑖,𝑗
𝑑𝑗
the 0,061855
and 0,045121
of 0,025408
to 0,021373
a 0,019652
he 0,019243
in 0,014911
was 0,014184
they 0,013743
it 0,011797
that 0,010313
had 0,009719
his 0,009268
you 0,007978
on 0,007896
for 0,007353
not 0,007312
were 0,007117
as 0,007035
i 0,007025
at 0,006738
all 0,006698
with 0,006687
but 0,006063
said 0,00597
them 0,00552
their 0,005376
bilbo 0,005151
there 0,005151
the 6040
and 4406
of 2481
to 2087
a 1919
he 1879
in 1456
was 1385
they 1342
it 1152
that 1007
had 949
his 905
you 779
on 771
for 718
not 714
were 695
as 687
i 686
at 658
all 654
with 653
but 592
said 583
them 539
their 525
bilbo 503
there 503
Tf – idf
• Term Frequency per Inverse Document Frequency
• Misura il ‘PESO’ di ogni parola in un testo
𝑤𝑑 = 𝑓𝑤,𝑑 ∗ log𝐷
𝑓𝑤,𝐷
Es. Confronto tra i capitoli di un libri
Tf – idf Capitolo 1, lo Hobbit
said 0,008561
bilbo 0,004168
like 0,004168
thorin 0,00383
gandalf 0,003717
dwarves 0,003267
door 0,003042
baggins 0,002816
little 0,002704
hobbit 0,002704
the 0,056213
and 0,048327
a 0,02422
to 0,023882
of 0,02084
he 0,019038
in 0,013631
was 0,01273
i 0,012279
on 0,011152
plates 0,059822
gandalf 0,059115
map 0,053391
belladonna 0,047857
dungeons 0,04451
beautiful 0,043684
grandfather 0,040877
coffee 0,035893
glasses 0,035893
pantry 0,035893
Term Frequency
Con Stopwords
Term Frequency
Senza Stopwords
TF-IDF per capitolo
Riepilogo:
• Permette semplici e rapide analisi di testi
• Computazionalmente vantaggiosa
• Non Language-Dependent
• Presenza di margini di errore ‘statisticamente tollerabili’
borsellino 6.7214961766849655
suo 3.4154876523731406
diressero 3.3607480883424827
scolando 3.3607480883424827
manina 3.3607480883424827
borsa 3.3607480883424827
estraeva 3.3607480883424827
coraggio 3.3607480883424827
somma 3.3607480883424827
lavoro 3.3607480883424827
appassionandosi 3.3607480883424827
boccale 2.882922698399655
raccolse 2.882922698399655
cautela 2.882922698399655
inizio 2.882922698399655
barile 2.882922698399655
nuovo 2.6034127633906374
NLP basato su Regole
‘Berto e Maso si diressero verso il barile. Guglielmo stava
scolando un altro boccale. Allora Bilbo raccolse tutto il suo
coraggio e mise la manina nell’enorme tasca di Guglielmo. C’era
dentro un borsellino, grande quanto una borsa, per Bilbo. «
Eccoci qua! » pensò, appassionandosi al suo nuovo lavoro mentre
estraeva con somma cautela il borsellino. « Questo sì che è un
buon inizio! ».’
• Dove si diressero Berto e Maso?
• Che stava facendo Guglielmo?
• Che azioni compie Bilbo?
Parser sintattici
‘Berto e Maso si diressero verso il barile’
1. Dizionario Elettronico
1. Informazioni di tipo Sintattico e Semantico
Berto, Berto, Nome + Nome Proprio + Maschile + Animato +
Umano
si diressero, dirigersi, Verbo + riflessivo + Movimento + pass.
Remoto + 3ª p. plurale + Sogg. Animato + Destinazione
barile, Nome + singolare + Oggetto + Maschile + Contenitore
…
2. Una serie di istruzioni o REGOLE
Parser Sintattici
1. R1: Se F contiene ‘Dirigersi’, cerca $Soggetto$ Animato’ e
$Destinazione’$ introdotta da preposizione;
2. R2: Se V 3ª pers. Plurale cerca $Soggetto$ al plurale o più soggetti
coordinati;
3. R3: Scrivi: Agente = $Soggetto$;
4. R4: Scrivi: Azione = Movimento;
5. R5: Scrivi: Destinazione = $Destinazione$
Output:
Agente = Berto e Maso;
Azione = Movimento;
Destinazione = Barile.
Un Esempio
Le camere erano pulite quando ce le hanno consegnate, in alcune
c’è il bagno privato.
Lo sconsiglio nella maniera più assoluta
Il film non è stato bruttissimo, ma Johnny Depp è fantastico.
Hotel accettabile, ma cortesia zero!
Dizionario
1. Creazione del Dizionario Polarizzato
pulito: Aggettivo, Positivo (+2)
bruttissimo: Aggettivo, Negativo, Forte (-3)
fantastico: Aggettivo, Positivo, Forte (+3)
accettabile: Aggettivo, Positivo, Debole (+1)
2. Regola: ‘Se una parola del Dizionario è presente nella
frase, somma il valore relativo.
Un Esempio
Le camere erano pulite quando ce le hanno consegnate, in alcune
c’è il bagno privato.(+2)
Lo sconsiglio nella maniera più assoluta (0)
Il film non è stato bruttissimo, ma Johnny Depp è fantastico.(0)
Hotel accettabile, ma cortesia zero! (+1)
+2
-3 +3
+1
Dizionario
1. creazione del Dizionario Polarizzato
pulito: Aggettivo, Positivo (+2)
bruttissimo: Aggettivo, Negativo, Forte (-3)
fantastico: Aggettivo, Positivo, Forte (+3)
accettabile: Aggettivo, Positivo, Debole (+1)
sconsigliare: Verbo, Negativo, Forte (-2)
2. Regola: ‘Se una parola del Dizionario è presente nella
frase, somma il valore relativo.
3. Gestione della ‘Negazione’
4. Gestione delle avversative (ma…)
5. Frasi Idiomatiche (nella maniera più assoluta…)
Un Esempio
Le camere erano pulite quando ce le hanno consegnate, in alcune
c’è il bagno privato.(+2)
Lo sconsiglio nella maniera più assoluta (-6)
Il film non è stato bruttissimo, ma Johnny Depp è fantastico.(+4)
Hotel accettabile, ma cortesia zero! (-2)
+2
+1 +3
+1
-2 *3
-3
Riconoscimento delle Features
• Le [camere]F erano [pulite]O
• [Johnny Depp]F è [fantastico]O
• [Cortesia]F [zero]O
1. Individuazione feature:
Es: Nomi di cibi, bevande Tag FOODSERVICE
‘cibo’, ‘pranzo’, ‘cena’, ‘cucina’, ‘ristorazione’, ‘ristorante’ Tag FOODSERVICE
‘cortesia’, ecc… Tag ACCOGLIENZA
‘camere’, ‘stanze’, ‘suite’ Tag STRUTTURA
‘Johnny Depp’ Tag CAST
1. R: Se un Gruppo Nominale è seguito dal verbo ‘essere’ o da un Aggettivo allora è una Feature
Riepilogo
• Analisi mirate e altamente performanti (dal punto di vista
dei risultati)
• Maggior attenzione al Linguaggio e ai suoi fenomeni
• Comprensione del testo più profonda
• Dipendenti dalla lingua, dal topic, dal dominio e dal Task
• Mole di lavoro previo su Dizionari e Grammatiche
• Computazionalmente inefficiente