SlideShare a Scribd company logo
1 of 74
Basics of Information Retrieval
Adapted from Lectures by
Christopher Manning, Prabhakar Raghavan, and
Pandu Nayak, Stanford University
Web Info. Systems Spring 2015 1
Web Information Systems - 2015
Information Retrieval
• Information Retrieval (IR) is finding material
(usually documents) of an unstructured nature
(usually text) that satisfies an information need
from within large collections (usually stored on
computers).
– These days we frequently think first of web search,
but there are many other cases:
• E-mail search
• Searching your laptop
• Corporate knowledge bases
• Legal information retrieval
Pramod Web Info. Systems Spring 2015 2
Unstructured (text) vs. structured
(database) data in the mid-nineties
Pramod Web Info. Systems Spring 2015 3
Unstructured (text) vs. structured
(database) data today
Pramod Web Info. Systems Spring 2015 4
IR vs. databases:
Structured vs. unstructured data
• Structured data tends to refer to information
in “tables”
Employee Manager Salary
Smith Jones 50000
Chang Smith 60000
50000Ivy Smith
Typically allows numerical range and exact match
(for text) queries, e.g.,
Salary < 60000 AND Manager = Smith.
Pramod Web Info. Systems Spring 2015 5
Unstructured data
• Typically refers to free text
• Allows
– Keyword queries including operators
– More sophisticated “concept” queries e.g.,
• find all web pages dealing with drug abuse
• Classic model for searching text documents
Pramod Web Info. Systems Spring 2015 6
Semi-structured data
• In fact almost no data is “unstructured”
• E.g., this slide has distinctly identified zones such
as the Title and Bullets
• … to say nothing of linguistic structure
• Facilitates “semi-structured” search such as
– Title contains data AND Bullets contain search
• Or even
– Title is about Object Oriented Programming AND
Author something like stro*rup
– where * is the wild-card operator
Pramod Web Info. Systems Spring 2015 7
Basic assumptions of Information
Retrieval
• Collection: A set of documents
– Assume it is a static collection for the moment
• Goal: Retrieve documents with information
that is relevant to the user’s information need
and helps the user complete a task
Sec. 1.1
Pramod Web Info. Systems Spring 2015 8
how trap mice alive
The classic search model
Collection
User task
Info need
Query
Results
Search
engine
Query
refinement
Get rid of mice in a
politically correct way
Info about removing mice
without killing them
Misconception?
Misformulation?
Search
Pramod
Web Info. Systems Spring 2015
9
How good are the retrieved docs?
 Precision : Fraction of retrieved docs that are
relevant to the user’s information need
 Recall : Fraction of relevant docs in collection
that are retrieved
Sec. 1.1
Pramod Web Info. Systems Spring 2015 10
Term-document incident matrices
Adapted from Lectures by
Christopher Manning, Prabhakar Raghavan, and
Pandu Nayak, Stanford University
Pramod Web Info. Systems Spring 2015 11
Unstructured data in 1620
• Which plays of Shakespeare contain the words
Brutus AND Caesar but NOT Calpurnia?
• One could grep all of Shakespeare’s plays for
Brutus and Caesar, then strip out lines containing
Calpurnia?
• Why is that not the answer?
– Slow (for large corpora)
– NOT Calpurnia is non-trivial
– Other operations (e.g., find the word Romans near
countrymen) not feasible
– Ranked retrieval (best documents to return)
• Later in the lecture
Sec. 1.1
Pramod Web Info. Systems Spring 2015 12
Term-document incidence matrices
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0
1 if play contains
word, 0 otherwise
Brutus AND Caesar BUT NOT
Calpurnia
Sec. 1.1
Pramod Web Info. Systems Spring 2015 13
Incidence vectors
• So we have a 0/1 vector for each term.
• To answer query: take the vectors for Brutus,
Caesar and Calpurnia (complemented) 
bitwise AND.
– 110100 AND
110111 AND
101111 =
– 100100
Sec. 1.1
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0
Pramod Web Info. Systems Spring 2015 14
Answers to query
• Antony and Cleopatra, Act III, Scene ii
Agrippa [Aside to DOMITIUS ENOBARBUS]: Why, Enobarbus,
When Antony found Julius Caesar dead,
He cried almost to roaring; and he wept
When at Philippi he found Brutus slain.
• Hamlet, Act III, Scene ii
Lord Polonius: I did enact Julius Caesar I was killed i’ the
Capitol; Brutus killed me.
Sec. 1.1
Pramod Web Info. Systems Spring 2015 15
Bigger collections
• Consider N = 1 million documents, each with
about 1000 words.
• Avg 6 bytes/word including
spaces/punctuation
– 6GB of data in the documents.
• Say there are M = 500K distinct terms among
these.
Sec. 1.1
Pramod Web Info. Systems Spring 2015 16
Can’t build the matrix
• 500K x 1M matrix has half-a-trillion 0’s and
1’s.
• But it has no more than one billion 1’s.
– matrix is extremely sparse.
• What’s a better representation?
– We only record the 1 positions.
Sec. 1.1
Pramod Web Info. Systems Spring 2015 17
The Inverted Index: key data structure
underlying modern IR
Adapted from Lectures by
Christopher Manning and Pandu Nayak,
Stanford University
Pramod Web Info. Systems Spring 2015 18
Inverted index
• For each term t, we must store a list of all
documents that contain t.
– Identify each doc by a docID, a document serial
number
• Can we used fixed-size arrays for this?
What happens if the word Caesar
is added to document 14?
Sec. 1.2
Brutus
Calpurnia
Caesar 1 2 4 5 6 16 57 132
1 2 4 11 31 45 173
2 31
174
54 101
Pramod Web Info. Systems Spring 2015 19
Inverted index
• We need variable-size postings lists
– On disk, a continuous run of postings is normal
and best
– In memory, can use linked lists or variable length
arrays
• Some tradeoffs in size/ease of insertion
Dictionary Postings
Posting
Sec. 1.2
Brutus
Calpurnia
Caesar 1 2 4 5 6 16 57 132
1 2 4 11 31 45 173
2 31
174
54 101
Pramod Web Info. Systems Spring 2015 20
Tokenizer
Token stream Friends Romans Countrymen
Inverted index construction
Linguistic modules
Modified tokens friend roman countryman
Indexer
Inverted index
friend
roman
countryman
2 4
2
13 16
1
Documents to
be indexed
Friends, Romans, countrymen.
Sec. 1.2
Pramod Web Info. Systems Spring 2015 21
Initial stages of text processing
• Tokenization
– Cut character sequence into word tokens
• Deal with “John’s”, a state-of-the-art solution
• Normalization
– Map text and query term to same form
• You want U.S.A. and USA to match
• Stemming
– We may wish different forms of a root to match
• authorize, authorization
• Stop words
– We may omit very common words (or not)
• the, a, to, of
Pramod Web Info. Systems Spring 2015 22
Indexer steps: Token sequence
• Sequence of (Modified token, Document ID) pairs.
I did enact Julius
Caesar I was killed
i’ the Capitol;
Brutus killed me.
Doc 1
So let it be with
Caesar. The noble
Brutus hath told you
Caesar was ambitious
Doc 2
Sec. 1.2
Pramod Web Info. Systems Spring 2015 23
Indexer steps: Sort
• Sort by terms
– And then docID
Core indexing step
Sec. 1.2
Pramod Web Info. Systems Spring 2015 24
Indexer steps: Dictionary & Postings
• Multiple term entries
in a single document
are merged.
• Split into Dictionary
and Postings
• Doc. frequency
information is added.
Why frequency?
Will discuss later.
Sec. 1.2
Pramod Web Info. Systems Spring 2015 25
Where do we pay in storage?
Pointers
Terms
and
counts
IR system
implementation
• How do we
index efficiently?
• How much
storage do we
need?
Sec. 1.2
Lists of
docIDs
Pramod Web Info. Systems Spring 2015 26
Query processing with an inverted index
Adapted from Lectures by
Christopher Manning, Prabhakar Raghavan, and
Pandu Nayak, Stanford University
Pramod Web Info. Systems Spring 2015 27
The index we just built
• How do we process a query?
– Later - what kinds of queries can we process?
Our focus
Sec. 1.3
Pramod Web Info. Systems Spring 2015 28
Query processing: AND
• Consider processing the query:
Brutus AND Caesar
– Locate Brutus in the Dictionary;
• Retrieve its postings.
– Locate Caesar in the Dictionary;
• Retrieve its postings.
– “Merge” the two postings (intersect the
document sets):
128
34
2 4 8 16 32 64
1 2 3 5 8 13 21
Brutus
Caesar
Sec. 1.3
Pramod Web Info. Systems Spring 2015 29
The merge
• Walk through the two postings
simultaneously, in time linear in the total
number of postings entries
34
1282 4 8 16 32 64
1 2 3 5 8 13 21
Brutus
Caesar
If the list lengths are x and y, the merge takes O(x+y)
operations.
Crucial: postings sorted by docID.
Sec. 1.3
Pramod Web Info. Systems Spring 2015 30
Intersecting two postings lists
(a “merge” algorithm)
Pramod Web Info. Systems Spring 2015 31
The Boolean Retrieval Model
Adapted from Lectures by
Christopher Manning, Prabhakar Raghavan, and
Pandu Nayak, Stanford University
Pramod Web Info. Systems Spring 2015 32
Boolean queries: Exact match
• The Boolean retrieval model is being able to ask a
query that is a Boolean expression:
– Boolean Queries are queries using AND, OR and NOT
to join query terms
• Views each document as a set of words
• Is precise: document matches condition or not.
– Perhaps the simplest model to build an IR system on
• Primary commercial retrieval tool for 3 decades.
• Many search systems you still use are Boolean:
– Email, library catalog, Mac OS X Spotlight
Sec. 1.3
Pramod Web Info. Systems Spring 2015 33
Query optimization
• What is the best order for query
processing?
• Consider a query that is an AND of n terms.
• For each of the n terms, get its postings,
then AND them together.
Brutus
Caesar
Calpurnia
1 2 3 5 8 16 21 34
2 4 8 16 32 64 128
13 16
Query: Brutus AND Calpurnia AND Caesar 34
Sec. 1.3
Pramod Web Info. Systems Spring 2015 34
Query optimization example
• Process in order of increasing freq:
– start with smallest set, then keep cutting further.
This is why we kept
document freq. in dictionary
Execute the query as (Calpurnia AND Brutus) AND Caesar.
Sec. 1.3
Brutus
Caesar
Calpurnia
1 2 3 5 8 16 21 34
2 4 8 16 32 64 128
13 16
Pramod Web Info. Systems Spring 2015 35
More general optimization
• e.g., (madding OR crowd) AND (ignoble OR
strife)
• Get doc. freq.’s for all terms.
• Estimate the size of each OR by the sum of its
doc. freq.’s (conservative).
• Process in increasing order of OR sizes.
Sec. 1.3
Pramod Web Info. Systems Spring 2015 36
Exercise
• Recommend a query
processing order for
• Which two terms should we
process first?
Term Freq
eyes 213312
kaleidoscope 87009
marmalade 107913
skies 271658
tangerine 46653
trees 316812
(tangerine OR trees) AND
(marmalade OR skies) AND
(kaleidoscope OR eyes)
Pramod Web Info. Systems Spring 2015 37
Indexing and Search Continued…
• In Class
– Vector Space Model
– Lucene Java API for indexing a set of documents
and searching the index for a user query
• Assignment (More details in class)
– Set up Nutch to crawl web pages
– Index the crawled web pages
– Retrieve the results to a search query
Pramod Web Info. Systems Spring 2015 38
Scoring, Term Weighing, and
the Vector Space Model
Adapted from Lectures by
Christopher Manning, Prabhakar Raghavan, and
Pandu Nayak, Stanford University
Pramod Web Info. Systems Spring 2015 39
Ranked retrieval
• Thus far, our queries have all been Boolean.
– Documents either match or don’t.
• Good for expert users with precise understanding of
their needs and the collection.
– Also good for applications: Applications can easily
consume 1000s of results.
• Not good for the majority of users.
– Most users incapable of writing Boolean queries (or they
are, but they think it’s too much work).
– Most users don’t want to wade through 1000s of results.
• This is particularly true of web search.
Ch. 6
Pramod Web Info. Systems Spring 2015 40
Problem with Boolean search:
feast or famine
• Boolean queries often result in either too few
(=0) or too many (1000s) results.
• Query 1: “standard user dlink 650” → 200,000
hits
• Query 2: “standard user dlink 650 no card
found”: 0 hits
• It takes a lot of skill to come up with a query
that produces a manageable number of hits.
– AND gives too few; OR gives too many
Ch. 6
Pramod Web Info. Systems Spring 2015 41
Ranked retrieval models
• Rather than a set of documents satisfying a query
expression, in ranked retrieval, the system returns an
ordering over the (top) documents in the collection for
a query
• Free text queries: Rather than a query language of
operators and expressions, the user’s query is just one
or more words in a human language
• In principle, there are two separate choices here, but in
practice, ranked retrieval has normally been associated
with free text queries and vice versa
Pramod Web Info. Systems Spring 2015 42
Feast or famine: not a problem in
ranked retrieval
• When a system produces a ranked result set,
large result sets are not an issue
– Indeed, the size of the result set is not an issue
– We just show the top k ( ≈ 10) results
– We don’t overwhelm the user
– Premise: the ranking algorithm works
Ch. 6
Pramod Web Info. Systems Spring 2015 43
Scoring as the basis of ranked
retrieval
• We wish to return in order the documents
most likely to be useful to the searcher
• How can we rank-order the documents in the
collection with respect to a query?
• Assign a score – say in [0, 1] – to each
document
• This score measures how well document and
query “match”.
Ch. 6
Pramod Web Info. Systems Spring 2015 44
Query-document matching scores
• We need a way of assigning a score to a
query/document pair
• Let’s start with a one-term query
• If the query term does not occur in the
document: score should be 0
• The more frequent the query term in the
document, the higher the score (should be)
• We will look at a number of alternatives for
this.
Ch. 6
Pramod Web Info. Systems Spring 2015 45
Take 1: Jaccard coefficient
• A commonly used measure of overlap of two
sets A and B
• jaccard(A,B) = |A ∩ B| / |A ∪ B|
• jaccard(A,A) = 1
• jaccard(A,B) = 0 if A ∩ B = 0
• A and B don’t have to be the same size.
• Always assigns a number between 0 and 1.
Ch. 6
Pramod Web Info. Systems Spring 2015 46
Jaccard coefficient: Scoring example
• What is the query-document match score that
the Jaccard coefficient computes for each of
the two documents below?
• Query: ides of march
• Document 1: caesar died in march
• Document 2: the long march
Ch. 6
Pramod Web Info. Systems Spring 2015 47
Issues with Jaccard for scoring
• It doesn’t consider term frequency (how many
times a term occurs in a document)
• Rare terms in a collection are more
informative than frequent terms. Jaccard
doesn’t consider this information
• We need a more sophisticated way of
normalizing for length
Ch. 6
Pramod Web Info. Systems Spring 2015 48
Recall (Lecture 1): Binary term-
document incidence matrix
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0
Each document is represented by a binary vector ∈ {0,1}|V|
Sec. 6.2
Pramod Web Info. Systems Spring 2015 49
Term-document count matrices
• Consider the number of occurrences of a term
in a document:
– Each document is a count vector in ℕv: a column
below
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 157 73 0 0 0 0
Brutus 4 157 0 1 0 0
Caesar 232 227 0 2 1 1
Calpurnia 0 10 0 0 0 0
Cleopatra 57 0 0 0 0 0
mercy 2 0 3 5 5 1
worser 2 0 1 1 1 0
Sec. 6.2
Pramod Web Info. Systems Spring 2015 50
Bag of words model
• Vector representation doesn’t consider the
ordering of words in a document
• John is quicker than Mary and Mary is quicker
than John have the same vectors
• This is called the bag of words model.
• Positional index will be able to distinguish
these two documents.
• For now: bag of words model
Pramod Web Info. Systems Spring 2015 51
Term frequency tf
• The term frequency tft,d of term t in document d is
defined as the number of times that t occurs in d.
• We want to use tf when computing query-document
match scores. But how?
• Raw term frequency is not what we want:
– A document with 10 occurrences of the term is more
relevant than a document with 1 occurrence of the term.
– But not 10 times more relevant.
• Relevance does not increase proportionally with term
frequency.
Pramod Web Info. Systems Spring 2015 52
Log-frequency weighting
• The log frequency weight of term t in d is
• 0 → 0, 1 → 1, 2 → 1.3, 10 → 2, 1000 → 4, etc.
• Score for a document-query pair: sum over terms
t in both q and d:
• score
• The score is 0 if none of the query terms is
present in the document.


 

otherwise0,
0tfif,tflog1 10 t,dt,d
t,dw
 
 dqt dt )tflog(1 ,
Sec. 6.2
Pramod Web Info. Systems Spring 2015 53
Document frequency
• Rare terms are more informative than frequent
terms
– Recall stop words
• Consider a term in the query that is rare in the
collection (e.g., arachnocentric)
• A document containing this term is very likely to
be relevant to the query arachnocentric
→ We want a high weight for rare terms like
arachnocentric.
Sec. 6.2.1
Pramod Web Info. Systems Spring 2015 54
Document frequency, continued
• Frequent terms are less informative than rare terms
• Consider a query term that is frequent in the collection
(e.g., high, increase, line)
• A document containing such a term is more likely to be
relevant than a document that doesn’t
• But it’s not a sure indicator of relevance.
• → For frequent terms, we want high positive weights
for words like high, increase, and line
• But lower weights than for rare terms.
• We will use document frequency (df) to capture this.
Sec. 6.2.1
Pramod Web Info. Systems Spring 2015 55
idf weight
• dft is the document frequency of t: the number of
documents that contain t
– dft is an inverse measure of the informativeness of t
– dft  N
• We define the idf (inverse document frequency)
of t by
– We use log (N/dft) instead of N/dft to “dampen” the
effect of idf.
)/df(logidf 10 tt N
Will turn out the base of the log is immaterial.
Sec. 6.2.1
Pramod Web Info. Systems Spring 2015 56
idf example, suppose N = 1 million
term dft idft
calpurnia 1
animal 100
sunday 1,000
fly 10,000
under 100,000
the 1,000,000
There is one idf value for each term t in a collection.
Sec. 6.2.1
)/df(logidf 10 tt N
Pramod Web Info. Systems Spring 2015 57
Effect of idf on ranking
• Does idf have an effect on ranking for one-
term queries, like
– iPhone
• idf has no effect on ranking one term queries
– idf affects the ranking of documents for queries
with at least two terms
– For the query capricious person, idf weighting
makes occurrences of capricious count for much
more in the final document ranking than
occurrences of person.
Pramod Web Info. Systems Spring 2015 58
Collection vs. Document frequency
• The collection frequency of t is the number of
occurrences of t in the collection, counting
multiple occurrences.
• Example:
• Which word is a better search term (and
should get a higher weight)?
Word Collection frequency Document frequency
insurance 10440 3997
try 10422 8760
Sec. 6.2.1
Pramod Web Info. Systems Spring 2015 59
tf-idf weighting
• The tf-idf weight of a term is the product of its tf weight
and its idf weight.
• Best known weighting scheme in information retrieval
– Note: the “-” in tf-idf is a hyphen, not a minus sign!
– Alternative names: tf.idf, tf x idf
• Increases with the number of occurrences within a
document
• Increases with the rarity of the term in the collection
)df/(log)tf1log(w 10,, tdt Ndt

Sec. 6.2.2
Pramod Web Info. Systems Spring 2015 60
Score for a document given a query
• There are many variants
– How “tf” is computed (with/without logs)
– Whether the terms in the query are also weighted
– …
Score(q,d) = tf.idft,dtÎqÇd
å
Sec. 6.2.2
Pramod Web Info. Systems Spring 2015 61
Binary → count → weight matrix
Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth
Antony 5.25 3.18 0 0 0 0.35
Brutus 1.21 6.1 0 1 0 0
Caesar 8.59 2.54 0 1.51 0.25 0
Calpurnia 0 1.54 0 0 0 0
Cleopatra 2.85 0 0 0 0 0
mercy 1.51 0 1.9 0.12 5.25 0.88
worser 1.37 0 0.11 4.15 0.25 1.95
Each document is now represented by a real-valued
vector of tf-idf weights ∈ R|V|
Sec. 6.3
Pramod Web Info. Systems Spring 2015 62
Documents as vectors
• So we have a |V|-dimensional vector space
• Terms are axes of the space
• Documents are points or vectors in this space
• Very high-dimensional: tens of millions of
dimensions when you apply this to a web
search engine
• These are very sparse vectors - most entries
are zero.
Sec. 6.3
Pramod Web Info. Systems Spring 2015 63
Queries as vectors
• Key idea 1: Do the same for queries: represent
them as vectors in the space
• Key idea 2: Rank documents according to their
proximity to the query in this space
• proximity = similarity of vectors
• proximity ≈ inverse of distance
• Recall: We do this because we want to get away
from the you’re-either-in-or-out Boolean model.
• Instead: rank more relevant documents higher
than less relevant documents
Sec. 6.3
Pramod Web Info. Systems Spring 2015 64
Formalizing vector space proximity
• First cut: distance between two points
– ( = distance between the end points of the two
vectors)
• Euclidean distance?
• Euclidean distance is a bad idea . . .
• . . . because Euclidean distance is large for
vectors of different lengths.
Sec. 6.3
Pramod Web Info. Systems Spring 2015 65
Why distance is a bad idea
The Euclidean
distance between q
and d2 is large even
though the
distribution of terms
in the query q and the
distribution of
terms in the
document d2 are
very similar.
Sec. 6.3
Pramod Web Info. Systems Spring 2015 66
Use angle instead of distance
• Thought experiment: take a document d and
append it to itself. Call this document d′.
• “Semantically” d and d′ have the same content
• The Euclidean distance between the two
documents can be quite large
• The angle between the two documents is 0,
corresponding to maximal similarity.
• Key idea: Rank documents according to angle
with query.
Sec. 6.3
Pramod Web Info. Systems Spring 2015 67
Length normalization
• A vector can be (length-) normalized by dividing
each of its components by its length – for this we
use the L2 norm:
• Dividing a vector by its L2 norm makes it a unit
(length) vector (on surface of unit hypersphere)
• Effect on the two documents d and d′ (d
appended to itself) from earlier slide: they have
identical vectors after length-normalization.
– Long and short documents now have comparable
weights
 i ixx 2
2

Sec. 6.3
Pramod Web Info. Systems Spring 2015 68
cosine(query,document)







V
i i
V
i i
V
i ii
dq
dq
d
d
q
q
dq
dq
dq
1
2
1
2
1
),cos( 





Dot product Unit vectors
qi is the tf-idf weight of term i in the query
di is the tf-idf weight of term i in the document
cos(q,d) is the cosine similarity of q and d … or,
equivalently, the cosine of the angle between q and d.
Sec. 6.3
Pramod Web Info. Systems Spring 2015 69
Cosine for length-normalized
vectors
• For length-normalized vectors, cosine
similarity is simply the dot product (or scalar
product):
for q, d length-normalized.
cos(q,d) = q·d = qidii=1
V
å
Pramod Web Info. Systems Spring 2015 70
Cosine similarity illustrated
Pramod Web Info. Systems Spring 2015 71
Cosine similarity amongst 3 documents
term SaS PaP WH
affection 115 58 20
jealous 10 7 11
gossip 2 0 6
wuthering 0 0 38
How similar are
the novels
SaS: Sense and
Sensibility
PaP: Pride and
Prejudice, and
WH: Wuthering
Heights?
Term frequencies (counts)
Sec. 6.3
Note: To simplify this example, we don’t do idf weighting.
Pramod Web Info. Systems Spring 2015 72
3 documents example contd.
Log frequency weighting
term SaS PaP WH
affection 3.06 2.76 2.30
jealous 2.00 1.85 2.04
gossip 1.30 0 1.78
wuthering 0 0 2.58
After length normalization
term SaS PaP WH
affection 0.789 0.832 0.524
jealous 0.515 0.555 0.465
gossip 0.335 0 0.405
wuthering 0 0 0.588
cos(SaS,PaP) ≈
0.789 × 0.832 + 0.515 × 0.555 + 0.335 × 0.0 + 0.0 × 0.0
≈ 0.94
cos(SaS,WH) ≈ 0.79
cos(PaP,WH) ≈ 0.69
Why do we have cos(SaS,PaP) > cos(SaS,WH)?
Sec. 6.3
Pramod Web Info. Systems Spring 2015 73
Summary – vector space ranking
• Represent the query as a weighted tf-idf vector
• Represent each document as a weighted tf-idf
vector
• Compute the cosine similarity score for the
query vector and each document vector
• Rank documents with respect to the query by
score
• Return the top K (e.g., K = 10) to the user
Pramod Web Info. Systems Spring 2015 74

More Related Content

What's hot

Python and BIG Data analytics | Python Fundamentals | Python Architecture
Python and BIG Data analytics | Python Fundamentals | Python ArchitecturePython and BIG Data analytics | Python Fundamentals | Python Architecture
Python and BIG Data analytics | Python Fundamentals | Python Architecture
Skillspeed
 
LiveLinkedData - TransWebData - Nantes 2013
LiveLinkedData - TransWebData - Nantes 2013LiveLinkedData - TransWebData - Nantes 2013
LiveLinkedData - TransWebData - Nantes 2013
Luis Daniel Ibáñez
 

What's hot (16)

BDACA1516s2 - Lecture8
BDACA1516s2 - Lecture8BDACA1516s2 - Lecture8
BDACA1516s2 - Lecture8
 
BDACA1617s2 - Lecture3
BDACA1617s2 - Lecture3BDACA1617s2 - Lecture3
BDACA1617s2 - Lecture3
 
BDACA1617s2 - Lecture4
BDACA1617s2 - Lecture4BDACA1617s2 - Lecture4
BDACA1617s2 - Lecture4
 
BDACA1617s2 - Tutorial 1
BDACA1617s2 - Tutorial 1BDACA1617s2 - Tutorial 1
BDACA1617s2 - Tutorial 1
 
Storage for next-generation sequencing
Storage for next-generation sequencingStorage for next-generation sequencing
Storage for next-generation sequencing
 
BDACA1617s2 - Lecture 2
BDACA1617s2 - Lecture 2BDACA1617s2 - Lecture 2
BDACA1617s2 - Lecture 2
 
Future Architectures for genomics
Future Architectures for genomicsFuture Architectures for genomics
Future Architectures for genomics
 
Python and BIG Data analytics | Python Fundamentals | Python Architecture
Python and BIG Data analytics | Python Fundamentals | Python ArchitecturePython and BIG Data analytics | Python Fundamentals | Python Architecture
Python and BIG Data analytics | Python Fundamentals | Python Architecture
 
A mathematical appraisal
A mathematical appraisalA mathematical appraisal
A mathematical appraisal
 
Life sciences big data use cases
Life sciences big data use casesLife sciences big data use cases
Life sciences big data use cases
 
R, Data Wrangling & Predicting NFL with Elo like Nate SIlver & 538
R, Data Wrangling & Predicting NFL with Elo like Nate SIlver & 538R, Data Wrangling & Predicting NFL with Elo like Nate SIlver & 538
R, Data Wrangling & Predicting NFL with Elo like Nate SIlver & 538
 
LiveLinkedData - TransWebData - Nantes 2013
LiveLinkedData - TransWebData - Nantes 2013LiveLinkedData - TransWebData - Nantes 2013
LiveLinkedData - TransWebData - Nantes 2013
 
Exploring Direct Concept Search - Steve Rowe, Lucidworks
Exploring Direct Concept Search - Steve Rowe, LucidworksExploring Direct Concept Search - Steve Rowe, Lucidworks
Exploring Direct Concept Search - Steve Rowe, Lucidworks
 
Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)
 
Analyzing Stack Overflow - Problem
Analyzing Stack Overflow - ProblemAnalyzing Stack Overflow - Problem
Analyzing Stack Overflow - Problem
 
Hadoop with Python
Hadoop with PythonHadoop with Python
Hadoop with Python
 

Similar to Basics of IR: Web Information Systems class

Big Data Analytics Strategy and Roadmap
Big Data Analytics Strategy and RoadmapBig Data Analytics Strategy and Roadmap
Big Data Analytics Strategy and Roadmap
Srinath Perera
 
Uc14 chap14
Uc14 chap14Uc14 chap14
Uc14 chap14
ayahye
 

Similar to Basics of IR: Web Information Systems class (20)

introduction into IR
introduction into IRintroduction into IR
introduction into IR
 
lecture1-intro.ppt
lecture1-intro.pptlecture1-intro.ppt
lecture1-intro.ppt
 
lecture1-intro.ppt
lecture1-intro.pptlecture1-intro.ppt
lecture1-intro.ppt
 
lectura
lecturalectura
lectura
 
Building highly reliable data pipeline @datadog par Quentin François
Building highly reliable data pipeline @datadog par Quentin FrançoisBuilding highly reliable data pipeline @datadog par Quentin François
Building highly reliable data pipeline @datadog par Quentin François
 
eScience: A Transformed Scientific Method
eScience: A Transformed Scientific MethodeScience: A Transformed Scientific Method
eScience: A Transformed Scientific Method
 
Data analytics introduction
Data analytics introductionData analytics introduction
Data analytics introduction
 
Data Discovery and Metadata
Data Discovery and MetadataData Discovery and Metadata
Data Discovery and Metadata
 
Big Data Analytics Strategy and Roadmap
Big Data Analytics Strategy and RoadmapBig Data Analytics Strategy and Roadmap
Big Data Analytics Strategy and Roadmap
 
lec01.ppt
lec01.pptlec01.ppt
lec01.ppt
 
Data council sf amundsen presentation
Data council sf    amundsen presentationData council sf    amundsen presentation
Data council sf amundsen presentation
 
Kostas Tzoumas - Stream Processing with Apache Flink®
Kostas Tzoumas - Stream Processing with Apache Flink®Kostas Tzoumas - Stream Processing with Apache Flink®
Kostas Tzoumas - Stream Processing with Apache Flink®
 
Debunking Common Myths in Stream Processing
Debunking Common Myths in Stream ProcessingDebunking Common Myths in Stream Processing
Debunking Common Myths in Stream Processing
 
Big Data and Data Science: The Technologies Shaping Our Lives
Big Data and Data Science: The Technologies Shaping Our LivesBig Data and Data Science: The Technologies Shaping Our Lives
Big Data and Data Science: The Technologies Shaping Our Lives
 
Debunking Six Common Myths in Stream Processing
Debunking Six Common Myths in Stream ProcessingDebunking Six Common Myths in Stream Processing
Debunking Six Common Myths in Stream Processing
 
Lecture_1_Intro.pdf
Lecture_1_Intro.pdfLecture_1_Intro.pdf
Lecture_1_Intro.pdf
 
Is this normal?
Is this normal?Is this normal?
Is this normal?
 
Uc14 chap14
Uc14 chap14Uc14 chap14
Uc14 chap14
 
Uc14 chap14
Uc14 chap14Uc14 chap14
Uc14 chap14
 
BAS 250 Lecture 1
BAS 250 Lecture 1BAS 250 Lecture 1
BAS 250 Lecture 1
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Basics of IR: Web Information Systems class

  • 1. Basics of Information Retrieval Adapted from Lectures by Christopher Manning, Prabhakar Raghavan, and Pandu Nayak, Stanford University Web Info. Systems Spring 2015 1 Web Information Systems - 2015
  • 2. Information Retrieval • Information Retrieval (IR) is finding material (usually documents) of an unstructured nature (usually text) that satisfies an information need from within large collections (usually stored on computers). – These days we frequently think first of web search, but there are many other cases: • E-mail search • Searching your laptop • Corporate knowledge bases • Legal information retrieval Pramod Web Info. Systems Spring 2015 2
  • 3. Unstructured (text) vs. structured (database) data in the mid-nineties Pramod Web Info. Systems Spring 2015 3
  • 4. Unstructured (text) vs. structured (database) data today Pramod Web Info. Systems Spring 2015 4
  • 5. IR vs. databases: Structured vs. unstructured data • Structured data tends to refer to information in “tables” Employee Manager Salary Smith Jones 50000 Chang Smith 60000 50000Ivy Smith Typically allows numerical range and exact match (for text) queries, e.g., Salary < 60000 AND Manager = Smith. Pramod Web Info. Systems Spring 2015 5
  • 6. Unstructured data • Typically refers to free text • Allows – Keyword queries including operators – More sophisticated “concept” queries e.g., • find all web pages dealing with drug abuse • Classic model for searching text documents Pramod Web Info. Systems Spring 2015 6
  • 7. Semi-structured data • In fact almost no data is “unstructured” • E.g., this slide has distinctly identified zones such as the Title and Bullets • … to say nothing of linguistic structure • Facilitates “semi-structured” search such as – Title contains data AND Bullets contain search • Or even – Title is about Object Oriented Programming AND Author something like stro*rup – where * is the wild-card operator Pramod Web Info. Systems Spring 2015 7
  • 8. Basic assumptions of Information Retrieval • Collection: A set of documents – Assume it is a static collection for the moment • Goal: Retrieve documents with information that is relevant to the user’s information need and helps the user complete a task Sec. 1.1 Pramod Web Info. Systems Spring 2015 8
  • 9. how trap mice alive The classic search model Collection User task Info need Query Results Search engine Query refinement Get rid of mice in a politically correct way Info about removing mice without killing them Misconception? Misformulation? Search Pramod Web Info. Systems Spring 2015 9
  • 10. How good are the retrieved docs?  Precision : Fraction of retrieved docs that are relevant to the user’s information need  Recall : Fraction of relevant docs in collection that are retrieved Sec. 1.1 Pramod Web Info. Systems Spring 2015 10
  • 11. Term-document incident matrices Adapted from Lectures by Christopher Manning, Prabhakar Raghavan, and Pandu Nayak, Stanford University Pramod Web Info. Systems Spring 2015 11
  • 12. Unstructured data in 1620 • Which plays of Shakespeare contain the words Brutus AND Caesar but NOT Calpurnia? • One could grep all of Shakespeare’s plays for Brutus and Caesar, then strip out lines containing Calpurnia? • Why is that not the answer? – Slow (for large corpora) – NOT Calpurnia is non-trivial – Other operations (e.g., find the word Romans near countrymen) not feasible – Ranked retrieval (best documents to return) • Later in the lecture Sec. 1.1 Pramod Web Info. Systems Spring 2015 12
  • 13. Term-document incidence matrices Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth Antony 1 1 0 0 0 1 Brutus 1 1 0 1 0 0 Caesar 1 1 0 1 1 1 Calpurnia 0 1 0 0 0 0 Cleopatra 1 0 0 0 0 0 mercy 1 0 1 1 1 1 worser 1 0 1 1 1 0 1 if play contains word, 0 otherwise Brutus AND Caesar BUT NOT Calpurnia Sec. 1.1 Pramod Web Info. Systems Spring 2015 13
  • 14. Incidence vectors • So we have a 0/1 vector for each term. • To answer query: take the vectors for Brutus, Caesar and Calpurnia (complemented)  bitwise AND. – 110100 AND 110111 AND 101111 = – 100100 Sec. 1.1 Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth Antony 1 1 0 0 0 1 Brutus 1 1 0 1 0 0 Caesar 1 1 0 1 1 1 Calpurnia 0 1 0 0 0 0 Cleopatra 1 0 0 0 0 0 mercy 1 0 1 1 1 1 worser 1 0 1 1 1 0 Pramod Web Info. Systems Spring 2015 14
  • 15. Answers to query • Antony and Cleopatra, Act III, Scene ii Agrippa [Aside to DOMITIUS ENOBARBUS]: Why, Enobarbus, When Antony found Julius Caesar dead, He cried almost to roaring; and he wept When at Philippi he found Brutus slain. • Hamlet, Act III, Scene ii Lord Polonius: I did enact Julius Caesar I was killed i’ the Capitol; Brutus killed me. Sec. 1.1 Pramod Web Info. Systems Spring 2015 15
  • 16. Bigger collections • Consider N = 1 million documents, each with about 1000 words. • Avg 6 bytes/word including spaces/punctuation – 6GB of data in the documents. • Say there are M = 500K distinct terms among these. Sec. 1.1 Pramod Web Info. Systems Spring 2015 16
  • 17. Can’t build the matrix • 500K x 1M matrix has half-a-trillion 0’s and 1’s. • But it has no more than one billion 1’s. – matrix is extremely sparse. • What’s a better representation? – We only record the 1 positions. Sec. 1.1 Pramod Web Info. Systems Spring 2015 17
  • 18. The Inverted Index: key data structure underlying modern IR Adapted from Lectures by Christopher Manning and Pandu Nayak, Stanford University Pramod Web Info. Systems Spring 2015 18
  • 19. Inverted index • For each term t, we must store a list of all documents that contain t. – Identify each doc by a docID, a document serial number • Can we used fixed-size arrays for this? What happens if the word Caesar is added to document 14? Sec. 1.2 Brutus Calpurnia Caesar 1 2 4 5 6 16 57 132 1 2 4 11 31 45 173 2 31 174 54 101 Pramod Web Info. Systems Spring 2015 19
  • 20. Inverted index • We need variable-size postings lists – On disk, a continuous run of postings is normal and best – In memory, can use linked lists or variable length arrays • Some tradeoffs in size/ease of insertion Dictionary Postings Posting Sec. 1.2 Brutus Calpurnia Caesar 1 2 4 5 6 16 57 132 1 2 4 11 31 45 173 2 31 174 54 101 Pramod Web Info. Systems Spring 2015 20
  • 21. Tokenizer Token stream Friends Romans Countrymen Inverted index construction Linguistic modules Modified tokens friend roman countryman Indexer Inverted index friend roman countryman 2 4 2 13 16 1 Documents to be indexed Friends, Romans, countrymen. Sec. 1.2 Pramod Web Info. Systems Spring 2015 21
  • 22. Initial stages of text processing • Tokenization – Cut character sequence into word tokens • Deal with “John’s”, a state-of-the-art solution • Normalization – Map text and query term to same form • You want U.S.A. and USA to match • Stemming – We may wish different forms of a root to match • authorize, authorization • Stop words – We may omit very common words (or not) • the, a, to, of Pramod Web Info. Systems Spring 2015 22
  • 23. Indexer steps: Token sequence • Sequence of (Modified token, Document ID) pairs. I did enact Julius Caesar I was killed i’ the Capitol; Brutus killed me. Doc 1 So let it be with Caesar. The noble Brutus hath told you Caesar was ambitious Doc 2 Sec. 1.2 Pramod Web Info. Systems Spring 2015 23
  • 24. Indexer steps: Sort • Sort by terms – And then docID Core indexing step Sec. 1.2 Pramod Web Info. Systems Spring 2015 24
  • 25. Indexer steps: Dictionary & Postings • Multiple term entries in a single document are merged. • Split into Dictionary and Postings • Doc. frequency information is added. Why frequency? Will discuss later. Sec. 1.2 Pramod Web Info. Systems Spring 2015 25
  • 26. Where do we pay in storage? Pointers Terms and counts IR system implementation • How do we index efficiently? • How much storage do we need? Sec. 1.2 Lists of docIDs Pramod Web Info. Systems Spring 2015 26
  • 27. Query processing with an inverted index Adapted from Lectures by Christopher Manning, Prabhakar Raghavan, and Pandu Nayak, Stanford University Pramod Web Info. Systems Spring 2015 27
  • 28. The index we just built • How do we process a query? – Later - what kinds of queries can we process? Our focus Sec. 1.3 Pramod Web Info. Systems Spring 2015 28
  • 29. Query processing: AND • Consider processing the query: Brutus AND Caesar – Locate Brutus in the Dictionary; • Retrieve its postings. – Locate Caesar in the Dictionary; • Retrieve its postings. – “Merge” the two postings (intersect the document sets): 128 34 2 4 8 16 32 64 1 2 3 5 8 13 21 Brutus Caesar Sec. 1.3 Pramod Web Info. Systems Spring 2015 29
  • 30. The merge • Walk through the two postings simultaneously, in time linear in the total number of postings entries 34 1282 4 8 16 32 64 1 2 3 5 8 13 21 Brutus Caesar If the list lengths are x and y, the merge takes O(x+y) operations. Crucial: postings sorted by docID. Sec. 1.3 Pramod Web Info. Systems Spring 2015 30
  • 31. Intersecting two postings lists (a “merge” algorithm) Pramod Web Info. Systems Spring 2015 31
  • 32. The Boolean Retrieval Model Adapted from Lectures by Christopher Manning, Prabhakar Raghavan, and Pandu Nayak, Stanford University Pramod Web Info. Systems Spring 2015 32
  • 33. Boolean queries: Exact match • The Boolean retrieval model is being able to ask a query that is a Boolean expression: – Boolean Queries are queries using AND, OR and NOT to join query terms • Views each document as a set of words • Is precise: document matches condition or not. – Perhaps the simplest model to build an IR system on • Primary commercial retrieval tool for 3 decades. • Many search systems you still use are Boolean: – Email, library catalog, Mac OS X Spotlight Sec. 1.3 Pramod Web Info. Systems Spring 2015 33
  • 34. Query optimization • What is the best order for query processing? • Consider a query that is an AND of n terms. • For each of the n terms, get its postings, then AND them together. Brutus Caesar Calpurnia 1 2 3 5 8 16 21 34 2 4 8 16 32 64 128 13 16 Query: Brutus AND Calpurnia AND Caesar 34 Sec. 1.3 Pramod Web Info. Systems Spring 2015 34
  • 35. Query optimization example • Process in order of increasing freq: – start with smallest set, then keep cutting further. This is why we kept document freq. in dictionary Execute the query as (Calpurnia AND Brutus) AND Caesar. Sec. 1.3 Brutus Caesar Calpurnia 1 2 3 5 8 16 21 34 2 4 8 16 32 64 128 13 16 Pramod Web Info. Systems Spring 2015 35
  • 36. More general optimization • e.g., (madding OR crowd) AND (ignoble OR strife) • Get doc. freq.’s for all terms. • Estimate the size of each OR by the sum of its doc. freq.’s (conservative). • Process in increasing order of OR sizes. Sec. 1.3 Pramod Web Info. Systems Spring 2015 36
  • 37. Exercise • Recommend a query processing order for • Which two terms should we process first? Term Freq eyes 213312 kaleidoscope 87009 marmalade 107913 skies 271658 tangerine 46653 trees 316812 (tangerine OR trees) AND (marmalade OR skies) AND (kaleidoscope OR eyes) Pramod Web Info. Systems Spring 2015 37
  • 38. Indexing and Search Continued… • In Class – Vector Space Model – Lucene Java API for indexing a set of documents and searching the index for a user query • Assignment (More details in class) – Set up Nutch to crawl web pages – Index the crawled web pages – Retrieve the results to a search query Pramod Web Info. Systems Spring 2015 38
  • 39. Scoring, Term Weighing, and the Vector Space Model Adapted from Lectures by Christopher Manning, Prabhakar Raghavan, and Pandu Nayak, Stanford University Pramod Web Info. Systems Spring 2015 39
  • 40. Ranked retrieval • Thus far, our queries have all been Boolean. – Documents either match or don’t. • Good for expert users with precise understanding of their needs and the collection. – Also good for applications: Applications can easily consume 1000s of results. • Not good for the majority of users. – Most users incapable of writing Boolean queries (or they are, but they think it’s too much work). – Most users don’t want to wade through 1000s of results. • This is particularly true of web search. Ch. 6 Pramod Web Info. Systems Spring 2015 40
  • 41. Problem with Boolean search: feast or famine • Boolean queries often result in either too few (=0) or too many (1000s) results. • Query 1: “standard user dlink 650” → 200,000 hits • Query 2: “standard user dlink 650 no card found”: 0 hits • It takes a lot of skill to come up with a query that produces a manageable number of hits. – AND gives too few; OR gives too many Ch. 6 Pramod Web Info. Systems Spring 2015 41
  • 42. Ranked retrieval models • Rather than a set of documents satisfying a query expression, in ranked retrieval, the system returns an ordering over the (top) documents in the collection for a query • Free text queries: Rather than a query language of operators and expressions, the user’s query is just one or more words in a human language • In principle, there are two separate choices here, but in practice, ranked retrieval has normally been associated with free text queries and vice versa Pramod Web Info. Systems Spring 2015 42
  • 43. Feast or famine: not a problem in ranked retrieval • When a system produces a ranked result set, large result sets are not an issue – Indeed, the size of the result set is not an issue – We just show the top k ( ≈ 10) results – We don’t overwhelm the user – Premise: the ranking algorithm works Ch. 6 Pramod Web Info. Systems Spring 2015 43
  • 44. Scoring as the basis of ranked retrieval • We wish to return in order the documents most likely to be useful to the searcher • How can we rank-order the documents in the collection with respect to a query? • Assign a score – say in [0, 1] – to each document • This score measures how well document and query “match”. Ch. 6 Pramod Web Info. Systems Spring 2015 44
  • 45. Query-document matching scores • We need a way of assigning a score to a query/document pair • Let’s start with a one-term query • If the query term does not occur in the document: score should be 0 • The more frequent the query term in the document, the higher the score (should be) • We will look at a number of alternatives for this. Ch. 6 Pramod Web Info. Systems Spring 2015 45
  • 46. Take 1: Jaccard coefficient • A commonly used measure of overlap of two sets A and B • jaccard(A,B) = |A ∩ B| / |A ∪ B| • jaccard(A,A) = 1 • jaccard(A,B) = 0 if A ∩ B = 0 • A and B don’t have to be the same size. • Always assigns a number between 0 and 1. Ch. 6 Pramod Web Info. Systems Spring 2015 46
  • 47. Jaccard coefficient: Scoring example • What is the query-document match score that the Jaccard coefficient computes for each of the two documents below? • Query: ides of march • Document 1: caesar died in march • Document 2: the long march Ch. 6 Pramod Web Info. Systems Spring 2015 47
  • 48. Issues with Jaccard for scoring • It doesn’t consider term frequency (how many times a term occurs in a document) • Rare terms in a collection are more informative than frequent terms. Jaccard doesn’t consider this information • We need a more sophisticated way of normalizing for length Ch. 6 Pramod Web Info. Systems Spring 2015 48
  • 49. Recall (Lecture 1): Binary term- document incidence matrix Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth Antony 1 1 0 0 0 1 Brutus 1 1 0 1 0 0 Caesar 1 1 0 1 1 1 Calpurnia 0 1 0 0 0 0 Cleopatra 1 0 0 0 0 0 mercy 1 0 1 1 1 1 worser 1 0 1 1 1 0 Each document is represented by a binary vector ∈ {0,1}|V| Sec. 6.2 Pramod Web Info. Systems Spring 2015 49
  • 50. Term-document count matrices • Consider the number of occurrences of a term in a document: – Each document is a count vector in ℕv: a column below Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth Antony 157 73 0 0 0 0 Brutus 4 157 0 1 0 0 Caesar 232 227 0 2 1 1 Calpurnia 0 10 0 0 0 0 Cleopatra 57 0 0 0 0 0 mercy 2 0 3 5 5 1 worser 2 0 1 1 1 0 Sec. 6.2 Pramod Web Info. Systems Spring 2015 50
  • 51. Bag of words model • Vector representation doesn’t consider the ordering of words in a document • John is quicker than Mary and Mary is quicker than John have the same vectors • This is called the bag of words model. • Positional index will be able to distinguish these two documents. • For now: bag of words model Pramod Web Info. Systems Spring 2015 51
  • 52. Term frequency tf • The term frequency tft,d of term t in document d is defined as the number of times that t occurs in d. • We want to use tf when computing query-document match scores. But how? • Raw term frequency is not what we want: – A document with 10 occurrences of the term is more relevant than a document with 1 occurrence of the term. – But not 10 times more relevant. • Relevance does not increase proportionally with term frequency. Pramod Web Info. Systems Spring 2015 52
  • 53. Log-frequency weighting • The log frequency weight of term t in d is • 0 → 0, 1 → 1, 2 → 1.3, 10 → 2, 1000 → 4, etc. • Score for a document-query pair: sum over terms t in both q and d: • score • The score is 0 if none of the query terms is present in the document.      otherwise0, 0tfif,tflog1 10 t,dt,d t,dw    dqt dt )tflog(1 , Sec. 6.2 Pramod Web Info. Systems Spring 2015 53
  • 54. Document frequency • Rare terms are more informative than frequent terms – Recall stop words • Consider a term in the query that is rare in the collection (e.g., arachnocentric) • A document containing this term is very likely to be relevant to the query arachnocentric → We want a high weight for rare terms like arachnocentric. Sec. 6.2.1 Pramod Web Info. Systems Spring 2015 54
  • 55. Document frequency, continued • Frequent terms are less informative than rare terms • Consider a query term that is frequent in the collection (e.g., high, increase, line) • A document containing such a term is more likely to be relevant than a document that doesn’t • But it’s not a sure indicator of relevance. • → For frequent terms, we want high positive weights for words like high, increase, and line • But lower weights than for rare terms. • We will use document frequency (df) to capture this. Sec. 6.2.1 Pramod Web Info. Systems Spring 2015 55
  • 56. idf weight • dft is the document frequency of t: the number of documents that contain t – dft is an inverse measure of the informativeness of t – dft  N • We define the idf (inverse document frequency) of t by – We use log (N/dft) instead of N/dft to “dampen” the effect of idf. )/df(logidf 10 tt N Will turn out the base of the log is immaterial. Sec. 6.2.1 Pramod Web Info. Systems Spring 2015 56
  • 57. idf example, suppose N = 1 million term dft idft calpurnia 1 animal 100 sunday 1,000 fly 10,000 under 100,000 the 1,000,000 There is one idf value for each term t in a collection. Sec. 6.2.1 )/df(logidf 10 tt N Pramod Web Info. Systems Spring 2015 57
  • 58. Effect of idf on ranking • Does idf have an effect on ranking for one- term queries, like – iPhone • idf has no effect on ranking one term queries – idf affects the ranking of documents for queries with at least two terms – For the query capricious person, idf weighting makes occurrences of capricious count for much more in the final document ranking than occurrences of person. Pramod Web Info. Systems Spring 2015 58
  • 59. Collection vs. Document frequency • The collection frequency of t is the number of occurrences of t in the collection, counting multiple occurrences. • Example: • Which word is a better search term (and should get a higher weight)? Word Collection frequency Document frequency insurance 10440 3997 try 10422 8760 Sec. 6.2.1 Pramod Web Info. Systems Spring 2015 59
  • 60. tf-idf weighting • The tf-idf weight of a term is the product of its tf weight and its idf weight. • Best known weighting scheme in information retrieval – Note: the “-” in tf-idf is a hyphen, not a minus sign! – Alternative names: tf.idf, tf x idf • Increases with the number of occurrences within a document • Increases with the rarity of the term in the collection )df/(log)tf1log(w 10,, tdt Ndt  Sec. 6.2.2 Pramod Web Info. Systems Spring 2015 60
  • 61. Score for a document given a query • There are many variants – How “tf” is computed (with/without logs) – Whether the terms in the query are also weighted – … Score(q,d) = tf.idft,dtÎqÇd å Sec. 6.2.2 Pramod Web Info. Systems Spring 2015 61
  • 62. Binary → count → weight matrix Antony and Cleopatra Julius Caesar The Tempest Hamlet Othello Macbeth Antony 5.25 3.18 0 0 0 0.35 Brutus 1.21 6.1 0 1 0 0 Caesar 8.59 2.54 0 1.51 0.25 0 Calpurnia 0 1.54 0 0 0 0 Cleopatra 2.85 0 0 0 0 0 mercy 1.51 0 1.9 0.12 5.25 0.88 worser 1.37 0 0.11 4.15 0.25 1.95 Each document is now represented by a real-valued vector of tf-idf weights ∈ R|V| Sec. 6.3 Pramod Web Info. Systems Spring 2015 62
  • 63. Documents as vectors • So we have a |V|-dimensional vector space • Terms are axes of the space • Documents are points or vectors in this space • Very high-dimensional: tens of millions of dimensions when you apply this to a web search engine • These are very sparse vectors - most entries are zero. Sec. 6.3 Pramod Web Info. Systems Spring 2015 63
  • 64. Queries as vectors • Key idea 1: Do the same for queries: represent them as vectors in the space • Key idea 2: Rank documents according to their proximity to the query in this space • proximity = similarity of vectors • proximity ≈ inverse of distance • Recall: We do this because we want to get away from the you’re-either-in-or-out Boolean model. • Instead: rank more relevant documents higher than less relevant documents Sec. 6.3 Pramod Web Info. Systems Spring 2015 64
  • 65. Formalizing vector space proximity • First cut: distance between two points – ( = distance between the end points of the two vectors) • Euclidean distance? • Euclidean distance is a bad idea . . . • . . . because Euclidean distance is large for vectors of different lengths. Sec. 6.3 Pramod Web Info. Systems Spring 2015 65
  • 66. Why distance is a bad idea The Euclidean distance between q and d2 is large even though the distribution of terms in the query q and the distribution of terms in the document d2 are very similar. Sec. 6.3 Pramod Web Info. Systems Spring 2015 66
  • 67. Use angle instead of distance • Thought experiment: take a document d and append it to itself. Call this document d′. • “Semantically” d and d′ have the same content • The Euclidean distance between the two documents can be quite large • The angle between the two documents is 0, corresponding to maximal similarity. • Key idea: Rank documents according to angle with query. Sec. 6.3 Pramod Web Info. Systems Spring 2015 67
  • 68. Length normalization • A vector can be (length-) normalized by dividing each of its components by its length – for this we use the L2 norm: • Dividing a vector by its L2 norm makes it a unit (length) vector (on surface of unit hypersphere) • Effect on the two documents d and d′ (d appended to itself) from earlier slide: they have identical vectors after length-normalization. – Long and short documents now have comparable weights  i ixx 2 2  Sec. 6.3 Pramod Web Info. Systems Spring 2015 68
  • 69. cosine(query,document)        V i i V i i V i ii dq dq d d q q dq dq dq 1 2 1 2 1 ),cos(       Dot product Unit vectors qi is the tf-idf weight of term i in the query di is the tf-idf weight of term i in the document cos(q,d) is the cosine similarity of q and d … or, equivalently, the cosine of the angle between q and d. Sec. 6.3 Pramod Web Info. Systems Spring 2015 69
  • 70. Cosine for length-normalized vectors • For length-normalized vectors, cosine similarity is simply the dot product (or scalar product): for q, d length-normalized. cos(q,d) = q·d = qidii=1 V å Pramod Web Info. Systems Spring 2015 70
  • 71. Cosine similarity illustrated Pramod Web Info. Systems Spring 2015 71
  • 72. Cosine similarity amongst 3 documents term SaS PaP WH affection 115 58 20 jealous 10 7 11 gossip 2 0 6 wuthering 0 0 38 How similar are the novels SaS: Sense and Sensibility PaP: Pride and Prejudice, and WH: Wuthering Heights? Term frequencies (counts) Sec. 6.3 Note: To simplify this example, we don’t do idf weighting. Pramod Web Info. Systems Spring 2015 72
  • 73. 3 documents example contd. Log frequency weighting term SaS PaP WH affection 3.06 2.76 2.30 jealous 2.00 1.85 2.04 gossip 1.30 0 1.78 wuthering 0 0 2.58 After length normalization term SaS PaP WH affection 0.789 0.832 0.524 jealous 0.515 0.555 0.465 gossip 0.335 0 0.405 wuthering 0 0 0.588 cos(SaS,PaP) ≈ 0.789 × 0.832 + 0.515 × 0.555 + 0.335 × 0.0 + 0.0 × 0.0 ≈ 0.94 cos(SaS,WH) ≈ 0.79 cos(PaP,WH) ≈ 0.69 Why do we have cos(SaS,PaP) > cos(SaS,WH)? Sec. 6.3 Pramod Web Info. Systems Spring 2015 73
  • 74. Summary – vector space ranking • Represent the query as a weighted tf-idf vector • Represent each document as a weighted tf-idf vector • Compute the cosine similarity score for the query vector and each document vector • Rank documents with respect to the query by score • Return the top K (e.g., K = 10) to the user Pramod Web Info. Systems Spring 2015 74