Docsity

Slides on Statistical NLP - Artificial Intelligence Programming | CS 662, Study notes of Computer Science

Partial preview of the text

Download Slides on Statistical NLP - Artificial Intelligence Programming | CS 662 and more Study notes Computer Science in PDF only on Docsity!

Artificial Intelligence

Programming^ Statistical NLP

Chris Brooks

Department of Computer ScienceUniversity of San Francisco

Outline n-grams^ Applications of n-grams review - Context-free grammars Probabilistic CFGs Information Extraction

Department of Computer Science — University of San Francisco – p.1/

Disadvantages of IR approaches No use of structural information.^ Not even co-occurrence of words Can’t deal with synonyms or dereferencing pronouns Very little semantic analysis.

Department of Computer Science — University of San Francisco – p.3/

Advantages of classical NLP Classical NLP approaches use a parser to generate aparse tree. This can then be used to transform knowledge into aform that can be reasoned with.^ Identifies sentence structure^ Easier to do semantic interpretation^ Can handle anaphora, synonyms, etc.

Department of Computer Science — University of San Francisco – p.4/

n-grams The simplest way to add structure to our IR approach isto count the occurrence not only of single tokens, but of sequences

of tokens.

So far, we’ve considered words as tokens. A token is sometimes called a

gram

an^ n-gram model considers the probability that asequence of

n^ tokens occurs in a row.

More precisely, it is the probability P^ (token

|tokeni

, tokeni− 1

, ..., tokeni− 2

)i−n Department of Computer Science — University of San Francisco – p.6/

n-grams Our approach in assignment 3 uses 1-grams, orunigrams. We could also choose to count

bigrams

, or 2-grams.

The sentence “Every good boy deserves fudge”contains the bigrams “every good”, “good boy”, “boydeserves”, “deserves fudge” We could continue this approach to 3-grams, or4-grams, or 5-grams. Longer n-grams give us more accurate informationabout content, since they include phrases rather thansingle words. What’s the downside here?

Department of Computer Science — University of San Francisco – p.7/

Smoothing So, when we are estimating

n-gram counts from a

corpus, there will be many

n-grams that we never see.

This might occur in your assignment - what if there’sa word in your similarity set that’s not in the corpus? The simplest thing to do is

add-one smoothing

. We start

each^

n-gram with a count of 1, rather than zero. Easy, but not very theoretically satisfying.

Department of Computer Science — University of San Francisco – p.9/

Linear Interpolation Smoothing We can also use estimates of shorter-length

n-grams to

help out.^ Assumption: the sequence

w, w^1

, wand the 23

sequence

w, w^1

are related. 2

More precisely, we want to know

P^ (w^3

|w, w^2

).^ We count 1

all 1-grams, 2-grams, and 3-grams. we estimate

P^ (w^3

|w, w^2

)^ as 1

cP^ (w^1

|w, w 32

) +^ c 1

P^ (w 23

|w) +^2

cP^ (w^3

So where do we get

c, c^12

, c?^3

They might be fixed, based on past experience. Or, we could learn them.

Department of Computer Science — University of San Francisco – p.10/

Viterbi segmentation input:^ a^ string

S,^ a^

1-gram^

distribution

P

n =^ length(S)words^ =

array[n+1] best^ = array[n+1]

=^ 0.

(n+1)*^

best[0]

=^ 1.

for^ i^ =

1 to^

n for^ j^ =

0 to^

i^ -^1 word^ =

S[j:i]

##get

the^ substring

from^

j^ to^ i

w =^ length(word)if^ (P[word]

x best[i

-^ w]^ >=

best[i])

best[i]

=^ P[word]

x^ best[i

-^ w]

words[i]

= word ###^ now

get^ best

words result^

=^ []

i =^ n while^ i

^0 push words[i]

onto^

result

i^ =^ i^

-^ len(words[i]) return^

result,

best[i]

Department of Computer Science — University of San Francisco – p.12/

Example Input ‘‘cattlefish’’

P(cat)

=^ 0.1,

P(cattle)

=^ 0.3,

P(fish)

=^ 0.1.

all^ other

1-grams

are^ 0.001. best[0]

=^ 1.

i:^ 1,^ j:

0 word:

’c’.^

w =^1

0.001^ *

1.0^ >=

best[1]

=^ 0.

words[1]

= ’c’ i =^ 2,^

j^ =^0

word^ =^

’ca’,^

w^ =^2

0.001^ *

1.0^ >=

best[2]

=^ 0.

words[2]

= ’ca’ i =^ 2,^

j^ =^1

word^ =^

’a’,^ w

=^1

0.001^ *

0.^

<^ 0.

Department of Computer Science — University of San Francisco – p.13/

Example i=4, j=0,^ word=’catt’,

w=

0.001^ *

1.0^ > 0.0 (^) best[4]^ =^

words[4]

= ’catt’ i=4,j=

word^

=^ ’att’,

w=

0.001^ *

0.^

<^ 0.

i=4,^ j=2,

word=’tt’,

w=

0.001^ *

0.^

<^ 0.

i=4,^ j=3,

word=’t’,

w=

0.001^ *

0.1^ < 0.

Department of Computer Science — University of San Francisco – p.15/

Example i=5, j=0,^ word=’cattl’,

w=

0.001^ *

1.0^ > 0.0 (^) best[5]^ =^ 0.001word[5]^ =^ ’cattl’ i=5,^ j=1,

word=’attl’,

w=

0.001^ *

0.^

<^ 0.

i=5,^ j=2,

word=’ttl’,

w=

0.001^ *

0.^

<^ 0.

i=5,^ j=3,

word=’tl’,

w=

0.001^ *

0.1^ < 0.

i=5,^ j=4,

word=’l’,

w=

0.001^ *

0.^

<^ 0.

Department of Computer Science — University of San Francisco – p.16/

Example best: [1.0^ 0.

0.^

0.1^ 0.

0.3^ 0.

0.2]

words:^

[’c’^ ’ca’

’cat’^

’catt’

’cattl’

’cattle’

’cattlef’

’cattlefi’

’cattlefis’

’fish’] i =^10 push^ ’fish’

onto^

result i =^ i-4push^ ’cattle’

onto^

result

i =^0

Department of Computer Science — University of San Francisco – p.18/

What’s going on here? The Viterbi algorithm is

searching

through the space of

all combinations of substrings.^ States with high probability mass are pursued. The ’best’ array is used to prevent the algorithm fromrepeatedly expanding portions of the search space. This is an example of dynamic programming (like chartparsing)

Department of Computer Science — University of San Francisco – p.19/