Text processing: tokenization
Text Processing: Basics
2 / 26
Week 1: Lecture 5
What is Tokenization?
Tokenization is the process of segmenting a string of characters into words.
Depending on the application in hand, you might have to perform sentence segmentation as well.
Courtsey (Most slides): Prof. Pawan Goyal
Sentence Segmentation
Text Processing: Basics
Week 1: Lecture 5
The problem of deciding where the sentences begin and end.
Challenges Involved
3 / 26
Sentence Segmentation
Text Processing: Basics
Week 1: Lecture 5
The problem of deciding where the sentences begin and end.
Challenges Involved
While ‘!’, ‘?’ are quite unambiguous
3 / 26
Sentence Segmentation
The problem of deciding where the sentences begin and end.
Challenges Involved
While ‘!’, ‘?’ are quite unambiguous
Period “.” is quite ambiguous and can be used additionally for
) Abbreviations (Dr., Mr., m.p.h.)
Text Processing: Basics
Week 1: Lecture 5
3 / 26
Sentence Segmentation
The problem of deciding where the sentences begin and end.
Challenges Involved
While ‘!’, ‘?’ are quite unambiguous
Period “.” is quite ambiguous and can be used additionally for
) Abbreviations (Dr., Mr., m.p.h.)
) Numbers (2.4%, 4.3)
Text Processing: Basics
Week 1: Lecture 5
3 / 26
Sentence Segmentation
Text Processing: Basics
Week 1: Lecture 5
The problem of deciding where the sentences begin and end.
Challenges Involved
While ‘!’, ‘?’ are quite unambiguous
Period “.” is quite ambiguous and can be used additionally for
) Abbreviations (Dr., Mr., m.p.h.)
) Numbers (2.4%, 4.3)
Approach: build a binary classifier
For each “.”
Decides EndOfSentence/NotEndOfSentence
3 / 26
Sentence Segmentation
Text Processing: Basics
Week 1: Lecture 5
The problem of deciding where the sentences begin and end.
Challenges Involved
While ‘!’, ‘?’ are quite unambiguous
Period “.” is quite ambiguous and can be used additionally for
) Abbreviations (Dr., Mr., m.p.h.)
) Numbers (2.4%, 4.3)
Approach: build a binary classifier
For each “.”
Decides EndOfSentence/NotEndOfSentence
Classifiers can be: hand-written rules, regular expressions, or machine learning
3 / 26
Sentence Segmentation: Decision Tree Example
Text Processing: Basics
Week 1: Lecture 5
Decision Tree: Is this word the end-of-sentence (E-O-S)?
4 / 26
Sentence Segmentation: Decision Tree Example
Text Processing: Basics
Week 1: Lecture 5
Decision Tree: Is this word the end-of-sentence (E-O-S)?
4 / 26
Text Processing: Basics
Week 1: Lecture 5
Other Important Features
Case of word with “.”: Upper, Lower, Cap, Number
5 / 26
Other Important Features
Text Processing: Basics
Week 1: Lecture 5
Case of word with “.”: Upper, Lower, Cap, Number Case of word after “.”: Upper, Lower, Cap, Number
5 / 26
Other Important Features
Text Processing: Basics
Week 1: Lecture 5
Case of word with “.”: Upper, Lower, Cap, Number Case of word after “.”: Upper, Lower, Cap, Number Numeric Features
5 / 26
Other Important Features
Text Processing: Basics
Week 1: Lecture 5
Case of word with “.”: Upper, Lower, Cap, Number Case of word after “.”: Upper, Lower, Cap, Number Numeric Features
) Length of word with “.”
) Probability (word with “.” occurs at end-of-sentence)
) Probability (word after “.” occurs at beginning-of-sentence)
5 / 26
Text Processing: Basics
Week 1: Lecture 5
Implementing Decision Trees
Just an if-then-else statement
Pawan Goyal (IIT Kharagpur)
6 / 26
Implementing Decision Trees
Text Processing: Basics
Week 1: Lecture 5
Just an if-then-else statement
Choosing the features is more important
Pawan Goyal (IIT Kharagpur)
6 / 26
Implementing Decision Trees
Text Processing: Basics
Week 1: Lecture 5
Just an if-then-else statement
Choosing the features is more important
For numeric features, thresholds are to be picked
Pawan Goyal (IIT Kharagpur)
6 / 26
Implementing Decision Trees
Just an if-then-else statement
Choosing the features is more important
For numeric features, thresholds are to be picked
With increasing features including numerical ones, difficult to set up the structure by hand
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
6 / 26
Implementing Decision Trees
Just an if-then-else statement
Choosing the features is more important
For numeric features, thresholds are to be picked
With increasing features including numerical ones, difficult to set up the structure by hand
Decision Tree structure can be learned using machine learning over a training corpus
Text Processing: Basics
Week 1: Lecture 5
6 / 26
Implementing Decision Trees
Text Processing: Basics
Week 1: Lecture 5
Just an if-then-else statement
Choosing the features is more important
For numeric features, thresholds are to be picked
With increasing features including numerical ones, difficult to set up the structure by hand
Decision Tree structure can be learned using machine learning over a training corpus
Basic Idea
Usually works top-down, by choosing a variable at each step that best splits the set of items. How: Using information Gain.
Popular algorithms: ID3, C4.5, CART
6 / 26
Other Classifiers
Text Processing: Basics
Week 1: Lecture 5
The questions in the decision tree can be thought of as features, that could be exploited by any other classifier:
Support Vector Machines Logistic regression Neural Networks
Pawan Goyal (IIT Kharagpur)
7 / 26
Word Tokenization
Text Processing: Basics
Week 1: Lecture 5
What is Tokenization?
Tokenization is the process of segmenting a string of characters into words.
Pawan Goyal (IIT Kharagpur)
8 / 26
Or meaningful unit
of processing
Words
morphemes
Characters
(sub-words)??
Word Tokenization
Text Processing: Basics
Week 1: Lecture 5
What is Tokenization?
Tokenization is the process of segmenting a string of characters into words.
I have a can opener; but I can’t open these cans.
Word Token
An occurrence of a word
For the above sentence, 11 word tokens.
Word Type
A different realization of a word
For the above sentence, 10 word types.
Pawan Goyal (IIT Kharagpur)
8 / 26
NLTK Toolkit (Python) Stanford CoreNLP (Java) Unix Commands
Text Processing: Basics
9 / 26
Week 1: Lecture 5
Tokenization in practice
Pawan Goyal (IIT Kharagpur)
Word Tokenization
Text Processing: Basics
Week 1: Lecture 5
Issues in Tokenization
Finland’s → Finland Finlands Finland’s ?
What’re, I’m, shouldn’t → What are, I am, should not ? San Francisco → one token or two?
m.p.h. → ??
Pawan Goyal (IIT Kharagpur)
10 / 26
Word Tokenization
Text Processing: Basics
Week 1: Lecture 5
Issues in Tokenization
Finland’s → Finland Finlands Finland’s ?
What’re, I’m, shouldn’t → What are, I am, should not ? San Francisco → one token or two?
m.p.h. → ??
For information retrieval, use the same convention for documents and queries
Pawan Goyal (IIT Kharagpur)
10 / 26
Text Processing: Basics
Week 1: Lecture 5
Handling Hyphenation
Hyphens can be
Pawan Goyal (IIT Kharagpur)
11 / 26
Handling Hyphenation
Text Processing: Basics
Week 1: Lecture 5
Hyphens can be
End-of-Line Hyphen
Used for splitting whole words into part for text justification.
This paper describes MIMIC, an adaptive mixed initia-tive spoken dialogue system that provides movie show-time information.
Pawan Goyal (IIT Kharagpur)
11 / 26
Handling Hyphenation
Text Processing: Basics
Week 1: Lecture 5
Hyphens can be
End-of-Line Hyphen
Used for splitting whole words into part for text justification.
This paper describes MIMIC, an adaptive mixed initia-tive spoken dialogue system that provides movie show-time information.
Lexical Hyphen
Certain prefixes are offen written hyphenated, e.g. co-, pre-, meta-, multi-, etc.
Pawan Goyal (IIT Kharagpur)
11 / 26
Handling Hyphenation
Text Processing: Basics
Week 1: Lecture 5
Hyphens can be
End-of-Line Hyphen
Used for splitting whole words into part for text justification.
This paper describes MIMIC, an adaptive mixed initia-tive spoken dialogue system that provides movie show-time information.
Lexical Hyphen
Certain prefixes are offen written hyphenated, e.g. co-, pre-, meta-, multi-, etc.
Sententially Determined Hyphenation
Mainly to prevent incorrect parsing of the phrase. Some possible usages: Noun modified by an ‘ed’-verb: case-based, hand-delivered
Entire expression as a modifier in a noun group: three-to-five-year direct marketing plan
Pawan Goyal (IIT Kharagpur)
11 / 26
Text Processing: Basics
Week 1: Lecture 5
Language Specific Issues: French and German
French
l’ensemble: want to match with un ensemble
Pawan Goyal (IIT Kharagpur)
12 / 26
Language Specific Issues: French and German
Text Processing: Basics
Week 1: Lecture 5
French
l’ensemble: want to match with un ensemble
German
Noun coumpounds are not segmented
Lebensversicherungsgesellschaftsangestellter ‘life insurance company employee’
Compound splitter required for German information retrieval
Pawan Goyal (IIT Kharagpur)
12 / 26
What’s a simple algo you can think of?
Text Processing: Basics
Week 1: Lecture 5
Language Specific Issues: Chinese and Japanese
No space between words
Pawan Goyal (IIT Kharagpur)
13 / 26
Language Specific Issues: Chinese and Japanese
Text Processing: Basics
Week 1: Lecture 5
No space between words
Japanese: further complications with multiple alphabets intermingled.
Pawan Goyal (IIT Kharagpur)
13 / 26
morpheme
Language Specific Issues: Sanskrit
Text Processing: Basics
Week 1: Lecture 5
“One should tell the truth, one should say kind words; one should neither tell harsh truths, nor flattering lies; this is a rule for all times.”
Pawan Goyal (IIT Kharagpur)
14 / 26
Language Specific Issues: Sanskrit
Text Processing: Basics
Week 1: Lecture 5
“One should tell the truth, one should say kind words; one should neither tell harsh truths, nor flattering lies; this is a rule for all times.”
Segmented Text:
Pawan Goyal (IIT Kharagpur)
14 / 26
Text Processing: Basics
15 / 26
Week 1: Lecture 5
Longest Words
Pawan Goyal (IIT Kharagpur)
Longest Words
Text Processing: Basics
16 / 26
Week 1: Lecture 5
Compound word composed of 431 letters, from the Varadāmbikā Pariṅaya Campū by Tirumalāmba
Pawan Goyal (IIT Kharagpur)
Diacritics (phonetics):
macron ( − ) , ā in fate
Text Processing: Basics
Week 1: Lecture 5
Word Tokenization in Chinese or Sanskrit
Also called ‘Word Segmentation’.
Pawan Goyal (IIT Kharagpur)
17 / 26
Word Tokenization in Chinese or Sanskrit
Text Processing: Basics
Week 1: Lecture 5
Also called ‘Word Segmentation’.
Greedy Algorithm for Chinese
Maximum Matching (Greedy Algorithm)
Start a pointer at the beginning of the string
Find the largest word in dictionary that matches the string starting at pointer
Move the pointer over the word in string
Think of the cases when word segmentation would be required for English Text.
Pawan Goyal (IIT Kharagpur)
17 / 26
Word Tokenization in Chinese or Sanskrit
Text Processing: Basics
Week 1: Lecture 5
Also called ‘Word Segmentation’.
Greedy Algorithm for Chinese
Maximum Matching (Greedy Algorithm)
Start a pointer at the beginning of the string
Find the largest word in dictionary that matches the string starting at pointer
Move the pointer over the word in string
Think of the cases when word segmentation would be required for English Text.
Finding constituent words in a compound hashtags: #ThankYouSachin, #musicmonday etc.
Pawan Goyal (IIT Kharagpur)
17 / 26
Text Segmentation for Sanskrit
Text Processing: Basics
Week 1: Lecture 5
1
General assumption behind the design
Sentences from Classical Sanskrit may be generated by a regular relation R of the Kleene closure W∗ of a regular set W of words over a finite alphabet Σ.
Pawan Goyal (IIT Kharagpur)
18 / 26
Text Segmentation for Sanskrit
Text Processing: Basics
Week 1: Lecture 5
1
General assumption behind the design
Sentences from Classical Sanskrit may be generated by a regular relation R of the Kleene closure W∗ of a regular set W of words over a finite alphabet Σ.
W: vocabulary of (inflected) words (padas) and
R: sandhi
Pawan Goyal (IIT Kharagpur)
18 / 26
Text Segmentation for Sanskrit
Text Processing: Basics
Week 1: Lecture 5
1
General assumption behind the design
Sentences from Classical Sanskrit may be generated by a regular relation R of the Kleene closure W∗ of a regular set W of words over a finite alphabet Σ.
W: vocabulary of (inflected) words (padas) and
R: sandhi
Analysis of a sentence
A candidate sentence w is analyzed by inverting relation R to produce a finite sequence w1, w2,...wn of word forms, together with a proof that
w ∈ R(w1 · w2... · wn).
Pawan Goyal (IIT Kharagpur)
18 / 26
Text Processing: Basics
19 / 26
Week 1: Lecture 5
Word Segmentation in Sanskrit
Pawan Goyal (IIT Kharagpur)
July 28 Summary
Sentence Segmentation
Word Segmentation
An application of tokenization is in Information retrieval
Normalization
Text Processing: Basics
Week 1: Lecture 5
Why to “normalize”?
Indexed text and query terms must have the same form.
U.S.A. and USA should be matched
Pawan Goyal (IIT Kharagpur)
20 / 26
Normalization
Why to “normalize”?
Indexed text and query terms must have the same form.
U.S.A. and USA should be matched
We implicitly define equivalence classes of terms
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
20 / 26
Case Folding
Text Processing: Basics
Week 1: Lecture 5
Reduce all letters to lower case Possible exceptions (Task dependent):
Pawan Goyal (IIT Kharagpur)
21 / 26
Case Folding
Text Processing: Basics
Week 1: Lecture 5
Reduce all letters to lower case Possible exceptions (Task dependent):
Pawan Goyal (IIT Kharagpur)
21 / 26
Lemmatization
Reduce inflections or variant forms to base form:
Have to find the correct dictionary headword form
Text Processing: Basics
22 / 26
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
Morphology
Text Processing: Basics
Week 1: Lecture 5
Morphology studies the internal structure of words, how words are built up from smaller meaningful units called morphemes
Pawan Goyal (IIT Kharagpur)
23 / 26
Morphology
Morphology studies the internal structure of words, how words are built up from smaller meaningful units called morphemes
Morphemes are divided into two categories
Stems: The core meaning bearing units
Affixes: Bits and pieces adhering to stems to change their meanings and grammatical functions
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
23 / 26
Morphology
Morphology studies the internal structure of words, how words are built up from smaller meaningful units called morphemes
Morphemes are divided into two categories
Stems: The core meaning bearing units
Affixes: Bits and pieces adhering to stems to change their meanings and grammatical functions
) Prefix: un-, anti-, etc (a-, ati-, pra- etc.)
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
23 / 26
Morphology
Morphology studies the internal structure of words, how words are built up from smaller meaningful units called morphemes
Morphemes are divided into two categories
Stems: The core meaning bearing units
Affixes: Bits and pieces adhering to stems to change their meanings and grammatical functions
) Prefix: un-, anti-, etc (a-, ati-, pra- etc.)
) Suffix: -ity, -ation, etc (-taa, -ke, -ka etc.)
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
23 / 26
Morphology
Morphology studies the internal structure of words, how words are built up from smaller meaningful units called morphemes
Morphemes are divided into two categories
Stems: The core meaning bearing units
Affixes: Bits and pieces adhering to stems to change their meanings and grammatical functions
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
23 / 26
(Extra) Morphemes and Typology
Translation quality often seems to depend on typological differences
Stemming
Text Processing: Basics
Week 1: Lecture 5
Reducing terms to their stems, used in information retrieval Crude chopping of affixes
Pawan Goyal (IIT Kharagpur)
24 / 26
Stemming
Text Processing: Basics
Week 1: Lecture 5
Reducing terms to their stems, used in information retrieval Crude chopping of affixes
Pawan Goyal (IIT Kharagpur)
24 / 26
Porter’s algorithm
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
25 / 26
Porter’s algorithm
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
25 / 26
Porter’s algorithm
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
25 / 26
Porter’s algorithm
Text Processing: Basics
Week 1: Lecture 5
Step 2
ational → ate (relational → relate) izer → ize (digitizer → digitize) ator → ate (operator → operate)
Pawan Goyal (IIT Kharagpur)
26 / 26
Porter’s algorithm
Text Processing: Basics
Week 1: Lecture 5
Pawan Goyal (IIT Kharagpur)
26 / 26
Great Book: Speech and Language Processing, Jurafsky Martin