Basic Text Processing
IP notices: slides from D. Jurafsy, C. Manning and S. Batzoglou
Università di Pisa
Outline
Regular expressions
Regular Expressions: Disjunctions
Pattern | Matches |
[wW]oodchuck | Woodchuck, woodchuck |
[1234567890] | Any digit |
Pattern | Matches | Text |
[A-Z] | An upper case letter | Drenched Blossoms |
[a-z] | A lower case letter | my beans were impatient |
[0-9] | A single digit | Chapter 1: Down the Rabbit Hole |
Regular Expressions: Negation in Disjunction
Pattern | Matches | Text |
[^A-Z] | Not an upper case letter | Oyfn pripetchik |
[^Ss] | Neither ‘S’ nor ‘s’ | I have no exquisite reason” |
[^e^] | Neither e nor ^ | Look here |
a^b | The pattern a carat b | Look up a^b now |
Regular Expressions: More Disjunction
Pattern | Matches |
groundhog|woodchuck | groundhog woodchuck |
yours|mine | yours mine |
a|b|c | = [abc] |
[gG]roundhog|[Ww]oodchuck | Groundhog woodchuck |
Regular Expressions: ? * + .
Stephen C Kleene
Pattern | Matches | Text |
colou?r | Optional previous char | color colour |
oo*h! | 0 or more of previous char | oh! ooh! oooh! ooooh! |
o+h! | 1 or more of previous char | oh! ooh! oooh! ooooh! |
baa+ | | baa baaa baaaa baaaaa |
beg.n | | begin begun begun beg3n |
Kleene *, Kleene +
Regular Expressions: anchors ^ $
Pattern | Matches |
^[A-Z] | Palo Alto |
^[^A-Za-z] | 1 “Hello” |
\.$ | The end. |
.$ | The end? The end! |
Example
Find me all instances of the word “the” in a text.
the
Misses capitalized examples
[tT]he
Incorrectly returns other or theology
[^a-zA-Z][tT]he[^a-zA-Z]
Errors
Errors (cont.)
Exercise
(Experiment on Jupyter notebook: HLT/RegExpr.ipynb)
Summary
13
Tokenization
Text Normalization
Tokenization
What’s a word?
How many words?
they lay back on the San Francisco grass and looked at the stars and their
How many words?
N = number of tokens
V = vocabulary = set of types
|V| is the size of the vocabulary
| Tokens = N | Types = |V| |
Switchboard phone conversations | 2.4 million | 20 thousand |
Shakespeare | 884,000 | 31 thousand |
Google N-grams | 1 trillion | 13 million |
Church and Gale (1990): |V| > O(N½)
Simple Tokenization in Unix
tr -sc 'A-Za-z' '\n' < shakes.txt
| sort
| uniq –c
1945 A
72 AARON
19 ABBESS
5 ABBOT
... ...
Change non-alpha to newlines
Sort in alphabetical order
Merge and count each type
The first step: tokenizing
tr -sc 'A-Za-z' '\n' < shakes.txt | head
THE
SONNETS
by
William
Shakespeare
From
fairest
creatures
We
...
The second step: sorting
tr -sc 'A-Za-z' '\n' < shakes.txt | sort | head
A
A
A
A
A
A
A
A
A
...
More counting
tr 'A-Z' 'a-z' < shakes.txt | tr –sc 'A-Za-z' '\n' | sort | uniq –c
tr 'A-Z' 'a-z' < shakes.txt | tr –sc 'A-Za-z' '\n' | sort | uniq –c | sort –n –r
23243 the
22225 i
18618 and
16339 to
15687 of
12780 a
12163 you
10839 my
10005 in
8954 d
What happened here?
Issues in Tokenization
Finland? Finlands? Finland’s
Slide from Chris Manning
Tokenization: language issues
Lebensversicherungsgesellschaftsangestellter Leben’s+versicherung’s+gesellschaft’s+angestellter
‘life insurance company employee’
German text processing benefits greatly from a compound splitter module
Slide from Chris Manning
Tokenization: language issues
フォーチュン500社は情報不足のため時間あた$500K(約6,000万円)
Katakana
Hiragana
Kanji
Romaji
End-user can express query entirely in hiragana!
Slide from Chris Manning
Word Tokenization in Chinese
Maximum Matching Word Segmentation Algorithm
Given a wordlist of Chinese, and a string.
English failure example (Palmer 2000)
Word Normalization and Stemming
Normalization
Slide from Chris Manning
Case folding
Slide from Chris Manning
Lemmatization
Slide from Chris Manning
Morphology
Stemming
for example compressed
and compression are both
accepted as equivalent to
compress.
for exampl compress and
compress ar both accept
as equival to compress
Slide from Chris Manning
Porter’s algorithm
Slide from Chris Manning
Porter’s algorithm
Step 1a
sses → ss caresses → caress
ies → I ponies → poni
ss → ss caress → caress
s → ø cats → cat
Step 1b
(*v*)ing → ø walking → walk
sing → sing
(*v*)ed → ø plastered → plaster
…
Step 2 (for long stems)
ational → ate relational → relate
izer → ize digitizer → digitize
ator → ate operator → operate
…
Step 3 (for longer stems)
al → ø revival → reviv
able → ø adjustable → adjust
ate → ø activate → activ
…
Viewing morphology in a corpus
(*v*)ing → ø walking → walk
sing → sing
Vowel present
Viewing morphology in a corpus
(*v*)ing → ø walking → walk
sing → sing
tr -sc 'A-Za-z' '\n' < shakes.txt | grep ’ing$' | sort | uniq -c | sort –nr
tr -sc 'A-Za-z' '\n' < shakes.txt | grep '[aeiou].*ing$' | sort | uniq -c | sort –nr
548 being
541 nothing
152 something
145 coming
130 morning
122 having
120 living
117 loving
116 Being
102 going
1312 King
548 being
541 nothing
388 king
375 bring
358 thing
307 ring
152 something
145 coming
130 morning
Vowel present
Dealing with complex morphology
Turkish:
Uygarlastiramadiklarimizdanmissinizcasina
‘(behaving) as if you are among those whom we could not civilize’
Uygar `civilized’ + las `become’
+ tir `cause’ + ama `not able’
+ dik `past’ + lar ‘plural’
+ imiz ‘p1pl’ + dan ‘abl’
+ mis ‘past’ + siniz ‘2pl’ + casina ‘as if’
Sentence Segmentation
Sentence Segmentation
Decision Tree Classifier for EOS
More sophisticated decision tree features
Slide from Richard Sproat
Learning Decision Trees
Alternative: using a ML classifier
Train a binary classifier to determine whether a punctuation character is an end of sentence
Error on Corpus | SVM | Naive Bayes |
WSJ | 0.25% | 0.35% |
Brown | 0.36% | 0.45% |
Complete Works of Edgar Allen Poe | 0.52% | 0.44 |
Punkt Sentence Splitter
import nltk.data
splitter = nltk.data.load(
'tokenizers/punkt/english.pickle')
for line in file:
for sent in splitter.tokenize(line.strip()):
print sent
tokenizer = splitter._lang_vars.word_tokenize
print ' '.join(tokenizer(sent))
Even Better
Dealing with clitics
lasciami, lasciatemelo
lascia-mi, lasciate-me-lo
Summary