48
Dictionaries and Tolerant retrieval Slides adapted from Stanford CS297:Introduction to Information CS297:Introduction to Information Retrieval

Dictionaries and Tolerant retrieval

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dictionaries and Tolerant retrieval

Dictionaries and Tolerant retrieval

Slides adapted from Stanford CS297:Introduction to InformationCS297:Introduction to Information

Retrieval

Page 2: Dictionaries and Tolerant retrieval

A skipped lectureA skipped lecture

The type/token distinctionThe type/token distinctionTerms are normalized types put in the dictionary

Tokenization problems:Tokenization problems:Hyphens, apostrophes, compounds, Chinese

Term equivalence classing:Term equivalence classing:Numbers, case folding, stemming, lemmatization

Skip pointersp pEncoding a tree-like structure in a postings list

Biword indexes for phrasesPositional indexes for phrases/proximity queries

Page 3: Dictionaries and Tolerant retrieval

This lectureThis lecture

Di ti d t t tDictionary data structures“Tolerant” retrieval

Wild-card queriesSpelling correctionSoundex

Page 4: Dictionaries and Tolerant retrieval

Dictionary data structures for inverted indexesinverted indexes

The dictionary data structure stores the termThe dictionary data structure stores the term vocabulary, document frequency, pointers to each postings list … in what data structure?p g

Page 5: Dictionaries and Tolerant retrieval

A naïve dictionaryA naïve dictionary

An array of struct:An array of struct:

h [20] i t P ti *char[20] int Postings *20 bytes 4/8 bytes 4/8 bytes

How do we store a dictionary in memory efficiently?How do we store a dictionary in memory efficiently?How do we quickly look up elements at query time?

Page 6: Dictionaries and Tolerant retrieval

Dictionary data structuresDictionary data structures

Two main choices:Two main choices:Hash tableTreeTree

Some IR systems use hashes, some trees

Page 7: Dictionaries and Tolerant retrieval

HashesHashes

Each vocabulary term is hashed to an integerEach vocabulary term is hashed to an integer(We assume you’ve seen hashtables before)

Pros:Pros:Lookup is faster than for a tree: O(1)

Cons:Cons:No easy way to find minor variants:

judgment/judgement

No prefix search [tolerant retrieval]If vocabulary keeps going, need to occasionally do the expensive operation of rehashing everythingthe expensive operation of rehashing everything

Page 8: Dictionaries and Tolerant retrieval

Tree: binary treeRoot

a-m n-z

Tree: binary tree

a hu hy m n sh si za-hu hy-m n-sh si-z

Page 9: Dictionaries and Tolerant retrieval

Tree: B treeTree: B-tree

a-huhy-m

n-z

Definition: Every internal nodel has a number of children in the interval [a b] where a b arechildren in the interval [a,b] where a, b are appropriate natural numbers, e.g., [2,4].

Page 10: Dictionaries and Tolerant retrieval

TreesTrees

Simplest: binary treeSimplest: binary treeMore usual: B-treesTrees require a standard ordering of characters andTrees require a standard ordering of characters and hence strings … but we standardly have onePros:

Solves the prefix problem (terms starting with hyp)Cons:

Sl O(l M) [ d thi i b l d t ]Slower: O(log M) [and this requires balanced tree]Rebalancing binary trees is expensive

But B-trees mitigate the rebalancing problemBut B trees mitigate the rebalancing problem

Page 11: Dictionaries and Tolerant retrieval

Wild-card queriesq

Page 12: Dictionaries and Tolerant retrieval

Wild card queries: *Wild-card queries: *

mon*: find all docs containing any word beginningmon*: find all docs containing any word beginning “mon”.Easy with binary tree (or B-tree) lexicon: retrieveEasy with binary tree (or B tree) lexicon: retrieve all words in range: mon ≤ w < moo*mon: find words ending in “mon”: harderg

Maintain an additional B-tree for terms backwards.Can retrieve all words in range: nom ≤ w < non.

Exercise: from this, how can we enumerate all termsi h ild d * ?meeting the wild-card query pro*cent ?

Page 13: Dictionaries and Tolerant retrieval

Query processingQuery processing

At this point we have an enumeration of all termsAt this point, we have an enumeration of all terms in the dictionary that match the wild-card query.We still have to look up the postings for eachWe still have to look up the postings for each enumerated term.E.g., consider the query:g q yse*ate AND fil*erThis may result in the execution of many Boolean AND queries.

Page 14: Dictionaries and Tolerant retrieval

B-trees handle *’s at the end of a query termquery term

How can we handle *’s in the middle of queryHow can we handle * s in the middle of query term?

co*tionco tionWe could look up co* AND *tion in a B-tree and intersect the two term sets

ExpensiveThe solution: transform wild-card queries so that the *’s occur at the endThis gives rise to the Permuterm Index.

Page 15: Dictionaries and Tolerant retrieval

Permuterm indexPermuterm index

For term hello index under:For term hello, index under:hello$, ello$h, llo$he, lo$hel, o$hell

where $ is a special symbolwhere $ is a special symbol.Queries:

X lookup on X$ X* lookup on X*$X lookup on X$ X lookup on X $*X lookup on X$* *X* lookup on X*X*Y lookup on Y$X* X*Y*Z ??? Exercise!

Query = hel*oX=hel Y=oX=hel, Y=o

Lookup o$hel*

Page 16: Dictionaries and Tolerant retrieval

Permuterm query processingPermuterm query processing

Rotate query wild card to the rightRotate query wild-card to the rightNow use B-tree lookup as before.Permuterm problem: ≈ quadruples lexicon sizePermuterm problem: ≈ quadruples lexicon size

Empirical observation for English.

Page 17: Dictionaries and Tolerant retrieval

Bigram (k gram) indexesBigram (k-gram) indexes

Enumerate all k grams (sequence of k chars)Enumerate all k-grams (sequence of k chars) occurring in any terme g from text “April is the cruelest month” wee.g., from text April is the cruelest month we get the 2-grams (bigrams)

$a ap pr ri il l$ $i is s$ $t th he e$ $c cr ru

$ is a special word boundary symbol

$a,ap,pr,ri,il,l$,$i,is,s$,$t,th,he,e$,$c,cr,ru,ue,el,le,es,st,t$, $m,mo,on,nt,h$

$ is a special word boundary symbolMaintain a second inverted index from bigrams todictionary terms that match each bigram.dictionary terms that match each bigram.

Page 18: Dictionaries and Tolerant retrieval

Bigram index exampleBigram index example

The k gram index finds terms based on a queryThe k-gram index finds terms based on a query consisting of k-grams

mo among

$m mace

amortize

madden

mo

on

among

among

amortize

around

Page 19: Dictionaries and Tolerant retrieval

Processing n gram wild cardsProcessing n-gram wild-cards

Query mon* can now be run asQuery mon* can now be run as$m AND mo AND on

Gets terms that match AND version of ourGets terms that match AND version of our wildcard query.But we’d enumerate moon.Must post-filter these terms against query.Surviving enumerated terms are then looked up g pin the term-document inverted index.Fast, space efficient (compared to permuterm).

Page 20: Dictionaries and Tolerant retrieval

Processing wild card queriesProcessing wild-card queries

As before we must execute a Boolean query forAs before, we must execute a Boolean query for each enumerated, filtered term.Wild-cards can result in expensive queryWild cards can result in expensive query execution (very large disjunctions…)

pyth* AND prog*If you encourage “laziness” people will respond!

S hSearchType your search terms, use ‘*’ if you need to.E.g., Alex* will match Alexander.

Does Google allow wildcard queries?

Page 21: Dictionaries and Tolerant retrieval

Spelling correctionp g

Page 22: Dictionaries and Tolerant retrieval

Spell correctionSpell correction

Two principal usesTwo principal usesCorrecting document(s) being indexedCorrecting user queries to retrieve “right” answersCorrecting user queries to retrieve right answers

Two main flavors:Isolated wordIsolated word

Check each word on its own for misspellingWill not catch typos resulting in correctly spelled wordse g from forme.g., from → form

Context-sensitiveLook at surrounding words, ge.g., I flew form Heathrow to Narita.

Page 23: Dictionaries and Tolerant retrieval

Document correctionDocument correction

Especially needed for OCR’ed documentsEspecially needed for OCR ed documentsCorrection algorithms are tuned for this: rn/mCan use domain-specific knowledgeCan use domain-specific knowledge

E.g., OCR can confuse O and D more often than it would confuse O and I (adjacent on the QWERTY keyboard, so more likely interchanged in typing)more likely interchanged in typing).

But also: web pages and even printed material has typosypGoal: the dictionary contains fewer misspellingsBut often we don’t change the documents but aim to fix the query-document mapping

Page 24: Dictionaries and Tolerant retrieval

Query mis spellingsQuery mis-spellings

Our principal focus hereOur principal focus hereE.g., the query Alanis Morisett

We can eitherWe can eitherRetrieve documents indexed by the correct spelling, ORReturn several suggested alternative queries with the correct spelling

Did you mean ?Did you mean … ?

Page 25: Dictionaries and Tolerant retrieval

Isolated word correctionIsolated word correction

Fundamental premise there is a lexicon fromFundamental premise – there is a lexicon from which the correct spellings comeTwo basic choices for thisTwo basic choices for this

A standard lexicon such asWebster’s English DictionaryAn “industry-specific” lexicon – hand-maintained

The lexicon of the indexed corpusE g all words on the webE.g., all words on the webAll names, acronyms etc.(Including the mis-spellings)

Page 26: Dictionaries and Tolerant retrieval

Isolated word correctionIsolated word correction

Given a lexicon and a character sequence QGiven a lexicon and a character sequence Q, return the words in the lexicon closest to QWhat’s “closest”?What s closest ?We’ll study several alternatives

Edit distance (Levenshtein distance)( )Weighted edit distancen-gram overlap

Page 27: Dictionaries and Tolerant retrieval

Edit distanceEdit distance

Given two strings S and S the minimumGiven two strings S1 and S2, the minimum number of operations to convert one to the otherOperations are typically character-levelOperations are typically character level

Insert, Delete, Replace, (Transposition)E.g., the edit distance from dof to dog is 1g , g

From cat to act is 2 (Just 1 with transpose.)from cat to dog is 3.

Generally found by dynamic programming.See http://www.merriampark.com/ld.htm for a nice example plus an applet.

Page 28: Dictionaries and Tolerant retrieval

Weighted edit distanceWeighted edit distance

As above but the weight of an operationAs above, but the weight of an operation depends on the character(s) involved

Meant to capture OCR or keyboard errors, e.g. mMeant to capture OCR or keyboard errors, e.g. mmore likely to be mis-typed as n than as qTherefore, replacing m by n is a smaller edit di h bdistance than by qThis may be formulated as a probability model

Requires weight matrix as inputRequires weight matrix as inputModify dynamic programming to handle weights

Page 29: Dictionaries and Tolerant retrieval

Using edit distancesUsing edit distances

Given query first enumerate all characterGiven query, first enumerate all character sequences within a preset (weighted) edit distance (e.g., 2)( g , )Intersect this set with list of “correct” wordsShow terms you found to user as suggestionsy ggAlternatively,

We can look up all possible corrections in our inverted index and return all docs … slowWe can run with a single most likely correction

Th lt ti di th b tThe alternatives disempower the user, but save a round of interaction with the user

Page 30: Dictionaries and Tolerant retrieval

Edit distance to all dictionar terms?Edit distance to all dictionary terms?

Given a (mis spelled) query do we compute itsGiven a (mis-spelled) query – do we compute its edit distance to every dictionary term?

Expensive and slowExpensive and slowAlternative?

How do we cut the set of candidate dictionary yterms?One possibility is to use n-gram overlap for thisThis can also be used by itself for spelling correction.

Page 31: Dictionaries and Tolerant retrieval

n gram overlapn-gram overlap

Enumerate all the n grams in the query string asEnumerate all the n-grams in the query string as well as in the lexiconUse the n-gram index (recall wild-card search) toUse the n gram index (recall wild card search) to retrieve all lexicon terms matching any of the query n-gramsThreshold by number of matching n-grams

Variants – weight by keyboard layout, etc.

Page 32: Dictionaries and Tolerant retrieval

Example with trigramsExample with trigrams

Suppose the text is novemberSuppose the text is novemberTrigrams are nov, ove, vem, emb, mbe, ber.

The query is decemberThe query is decemberTrigrams are dec, ece, cem, emb, mbe, ber.

So 3 trigrams overlap (of 6 in each term)So 3 trigrams overlap (of 6 in each term)How can we turn this into a normalized measure of overlap?

Page 33: Dictionaries and Tolerant retrieval

One option Jaccard coefficientOne option – Jaccard coefficient

A commonly used measure of overlapA commonly-used measure of overlapLet X and Y be two sets; then the J.C. is

Equals 1 when X and Y have the same elements

YXYX ∪∩ /Equals 1 when X and Y have the same elements and zero when they are disjointX and Y don’t have to be of the same sizea d do a e o be o e sa e s eAlways assigns a number between 0 and 1

Now threshold to decide if you have a matchyE.g., if J.C. > 0.8, declare a match

Page 34: Dictionaries and Tolerant retrieval

Matching trigramsMatching trigrams

Consider the query lord we wish to identifyConsider the query lord – we wish to identify words matching 2 of its 3 bigrams (lo, or, rd)

lo alone lord sloth

or

rd

lord morbid

border card

border

ardent

Standard postings “merge” will enumerate … p g g

Adapt this to using Jaccard (or another) measure.

Page 35: Dictionaries and Tolerant retrieval

Context sensitive spell correctionContext-sensitive spell correction

Text: I flew from Heathrow to NaritaText: I flew from Heathrow to Narita.Consider the phrase query “flew formHeathrow”HeathrowWe’d like to respond

Did you mean “flew from Heathrow”?Did you mean flew from Heathrow ?because no docs matched the query phrase.

Page 36: Dictionaries and Tolerant retrieval

Context sensitive correctionContext-sensitive correction

Need surrounding context to catch thisNeed surrounding context to catch this.First idea: retrieve dictionary terms close (in weighted edit distance) to each query termg ) q yNow try all possible resulting phrases with one word “fixed” at a time

flew from heathrowflew from heathrow fled form heathrowflea form heathrow

Hit-based spelling correction: Suggest the alternative that has lots of hits.

Page 37: Dictionaries and Tolerant retrieval

ExerciseExercise

Suppose that for “flew form Heathrow” weSuppose that for “flew form Heathrow” we have 7 alternatives for flew, 19 for form and 3 for heathrow.

How many “corrected” phrases will we enumerate in this scheme?

Page 38: Dictionaries and Tolerant retrieval

Another approachAnother approach

Break phrase query into a conjunction of biwordsBreak phrase query into a conjunction of biwords (Lecture 2).Look for biwords that need only one termLook for biwords that need only one term corrected.Enumerate phrase matches and … rank them!p

Page 39: Dictionaries and Tolerant retrieval

General issues in spell correctionGeneral issues in spell correction

We enumerate multiple alternatives for “Did youWe enumerate multiple alternatives for Did you mean?”Need to figure out which to present to the userNeed to figure out which to present to the userUse heuristics

The alternative hitting most docsgQuery log analysis + tweaking

For especially popular, topical queries

Spell-correction is computationally expensiveAvoid running routinely on every query?R l i th t t h d f dRun only on queries that matched few docs

Page 40: Dictionaries and Tolerant retrieval

Soundex

Page 41: Dictionaries and Tolerant retrieval

SoundexSoundex

Class of heuristics to expand a query intoClass of heuristics to expand a query into phonetic equivalents

Language specific – mainly for namesLanguage specific mainly for namesE.g., chebyshev → tchebycheff

Invented for the U.S. census … in 1918

Page 42: Dictionaries and Tolerant retrieval

Soundex typical algorithmSoundex – typical algorithm

Turn every token to be indexed into a 4 characterTurn every token to be indexed into a 4-character reduced formDo the same with query termsDo the same with query termsBuild and search an index on the reduced forms

(when the query calls for a soundex match)( q y )

http://www.creativyst.com/Doc/Articles/SoundEx1/SoundEx1.htm#Top

Page 43: Dictionaries and Tolerant retrieval

Soundex typical algorithmSoundex – typical algorithm

1 Retain the first letter of the word1. Retain the first letter of the word. 2. Change all occurrences of the following letters

to '0' (zero):( )'A', E', 'I', 'O', 'U', 'H', 'W', 'Y'.

3. Change letters to digits as follows: B F P V 1B, F, P, V → 1C, G, J, K, Q, S, X, Z → 2D T → 3D,T → 3L → 4M, N → 5,R → 6

Page 44: Dictionaries and Tolerant retrieval

Soundex continuedSoundex continued

4 Remove all pairs of consecutive digits4. Remove all pairs of consecutive digits.5. Remove all zeros from the resulting string.6 Pad the resulting string with trailing zeros and6. Pad the resulting string with trailing zeros and

return the first four positions, which will be of the form <uppercase letter> <digit> <digit> <digit>. pp g g g

E.g., Herman becomes H655.

Will hermann generate the same code?

Page 45: Dictionaries and Tolerant retrieval

SoundexSoundex

Soundex is the classic algorithm provided bySoundex is the classic algorithm, provided by most databases (Oracle, Microsoft, …)How useful is soundex?How useful is soundex?Not very – for information retrievalOkay for “high recall” tasks (e.g., Interpol),Okay for high recall tasks (e.g., Interpol), though biased to names of certain nationalitiesZobel and Dart (1996) show that other algorithms for phonetic matching perform much better in the context of IR

Page 46: Dictionaries and Tolerant retrieval

What queries can we process?What queries can we process?

We haveWe havePositional inverted index with skip pointersWild-card indexWild-card indexSpell-correctionSoundex

Queries such as(SPELL(moriset) /3 toron*to) OR

SOUNDEX(chaikofski)

Page 47: Dictionaries and Tolerant retrieval

ExerciseExercise

Draw yourself a diagram showing the variousDraw yourself a diagram showing the various indexes in a search engine incorporating all the functionality we have talked aboutyIdentify some of the key design choices in the index pipeline:

Does stemming happen before the Soundex index?What about n grams?What about n-grams?

Given a query, how would you parse and dispatch sub-queries to the various indexes?dispatch sub queries to the various indexes?

Page 48: Dictionaries and Tolerant retrieval

ResourcesResources

IIR 3 MG 4 2IIR 3, MG 4.2Efficient spell retrieval:

K. Kukich. Techniques for automatically correcting words in text. ACM Computing Surveys 24(4), Dec 1992.J. Zobel and P. Dart. Finding approximate matches in large lexicons. Software - practice and experience 25(3), March 1995. http://citeseer.ist.psu.edu/zobel95finding.htmlMikael Tillenius: Efficient Generation and Ranking of Spelling Error Corrections. Master’s thesis at Sweden’s Royal Institute of Technology htt // it i t d /179155 ht lTechnology. http://citeseer.ist.psu.edu/179155.html

Nice, easy reading on spell correction:Peter Norvig: How to write a spelling corrector g p ghttp://norvig.com/spell-correct.html