Professional Documents
Culture Documents
Supervisor:
Author: P ROF. C HAN C HEE K EONG
T HAI B INH D UONG Examiner:
P ROF. C HONG YONG K IM
Even though many natural language systems have been developed successfully and commercialized, none
of them yet proved to be versatile enough for a wide variation of tasks. One exception probably was IBM’s
Watson, which during the course of this project has won against 2 human champions in a Jeopardy contest
and showed for the first time that full scale interaction and reasoning in natural language were finally
within the reach of modern technology. In this project, user input query which is in natural language
form will be analyzed and presented in FOPC (First Order Predicate Calculus) which is suitable for using
as input for higher layer tasks such as logic. In the process, referents or implication from the question,
answer pair might also be deducted.
Acknowledgment
I owed my deepest gratitude to my supervisor professor Chan Chee Keong, who is very considerate and
cheerful at the same time, and who has given me the opportunity to work on this project which has been
very enjoyable.
I also want to express my gratitude to my counsellor Mr. Frank Boon Pink, Ms. Jasmine, Ms. Joanne
Quek, and professor Gwee Bah Hwee for behind me all the time.
My big thanks to professor Francis Bond for his teachings that I became interested in natural language
processing.
And last but not least, my no-word-can-describe-this gratitude and love to my family and my friends
Long and John for their supports.
Thanks all of you for everything. All the mistakes in this project are my own.
Contents
Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Acknowledgment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 Introduction 1
1.1 Literature Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Goals and Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Report Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 System Design 4
2.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Designs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Text Clean Up 9
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Tokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 Spell Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.3.2 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3.4 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.3.5 Future Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 Meaning Representation 25
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6.2 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6.2.1 First Order Predicate Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6.2.2 Formal Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6.3 Semantic Analysis A Study Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6.4 Operation explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
6.4.1 Implication Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
6.4.2 Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.5 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.5.1 Semantics Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.5.2 Meaning Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.6 Evaluations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
7 Conclusion 36
List of Figures
5.1 Accuracies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 Accuracies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 1
Introduction
Seach engine is vital for fast and accurate information retrieval. Most search engine up to date based
on key words, meta data and ranking algorithm to return results that are most likely to match the input
queries. This can be very irrelevant as in early search engine back in the 90s, or can be very effective as
in Google case, but still it can be tricked to give a higher rank than it really should, which is also known
as search engine optimization (SEO). Nevertheless, this suggests that in Google method, the real content
of a web page plays less significant role than it should do. Natural language search engine on the other
hand in theory will be able to response to questions from users as opposed to keywords only, and be able
to analyze the actual contents of the web page to determine the level of relevant.
To be fair, Google’s power lies in their gigantic knowledge base. To process such a huge knowledge
base and half a billion queries per day, traditional search engine probably is the most suitable choice by
letting the user do the final and the usually the most difficult task: read through the contents and choose
whatever suit their needs. Other interesting search engines that might be more useful than Google when
it comes to more specific tasks are GazoPa, TinyEyes, Stock photography all of which are similar image
search engine, each with their distinct features; Bing which is great for lifestyle; and Wolfram Alpha, the
world’s most academic search engine, which is also a natural language search engine.
Natural language search engine can be broken down to basic natural language tasks that we perform
daily: analysis, sense disambiguation, language generation. . . In fact, any natural language tasks can be
grouped into these below:
• Pragmatics: The study of how language can be used to accomplish goal or in different situation.
• Discourse: The study of linguistic units larger than one single utterance.
1
Human attempts to build automation that mimic humanlike behavior dated back some thousands years
ago and still going strong, from ancient programmable machines by pegs and ropes, to a mechanical mar-
vel robotic lion by Leonardo da Vinci in1515, and what kind of science fiction that is without humanlike
machine, either in a form of lovable and talkative android or an intelligent super computer with its own
evil will and desire. Despite a long history of envisioning, striving and many brilliant minds, it was
not until about 80 years ago in 1936, when the first freely programmable computer ‘Z1 Computer’ was
invented, that humankind had the facility to realized thist long standing dream.
As the technology evolves, human also invent more methods to effectively communicate with the
systems, from keyboard, mouse, to touch screen, eye and motion tracking, and even brain signal. But the
holy grail of communication will be what we have been developed through generations and what we are
most naturally familiar with: our mother tongue, or more generally natural languages. Even though many
systems has been successfully developed and commercialized [7], yet none prove to be versatile enough
for a wide variation of tasks. The linguistic tasks that we human perform almost effortlessly daily turn
out to be challenging indeed.
In this project, the main goal is to derive the implication given a pair of question and answer, which
I believe is how the machine should, and can learn, just as how we used to learn when we were kids and
plain. Along the way, minor tasks such as spelling and syntax analysis will also be explored.
2
to figure out which question should have been asked given a statement. Despite the fact that Watson had
the entire data of Wikipedia loaded in its RAM, or exhibited weird behavior at some points, chance are
not so far into the future, things such as interact freely with an intelligent system in natural language form
will start to penetrate and change the way we are using the machines today.
1.3 Scope
Currently, the program is able to perform word stemming, spelling correction, part-of-speech tagging,
chunking and partial meaning representation from an natural language input.
3
Chapter 2
System Design
2.1 Requirements
At the end of the project, the program should be able to analyse a natural language query, which is
typically a question or a pair of question and answer and return its meaning representation in the format
suitable for logic operation.
Some necessary intermediate processes are:
• Part-of-speech (POS) tagging using second hidden Markov model (HMM): Accuracy should ap-
proach 90%.
• Meaning deduction and representation using first order predicate calculus (FOPC).
2.2 Designs
At the core of the program are three separate modules and a central database.
Three modules are:
4
Figure 2.1: Simplified system DFD
5
Chapter 3
3.1 Tools
3.1.1 Python
Python, named after Monty Python a British band of comedians, is an easy-to-use, flexible, object-
oriented, mature, popular, and open source programming language designed to optimize development
speed. Python emphasizes concepts such as quality, productivity, portability, and integration. In short,
these terms mean (but not limited to) readability, fast development speed, text processing power and web
work suitability.
As a general-purpose language, Python can be used for almost anything computers are capable of. A
few organizations currently using Python are:
To be fair, it’s unlikely that Python will ever be as fast as C. However, since Python programs use
highly optimized structures and libraries, they tend to run near or even quicker than the speed of C
language somehow. Both C and Python have their distinct strengths and roles. In modern software
context, Python’s speed of development is just as important as C’s speed of execution.
Natural Language Processing Toolkit (NLTK) is an open source natural language processing libraries,
software and data for Python. In this project, NLTK was used mainly for its corpus and probability
module.
6
3.2 Resources
Below are collection of corpora used in the process. Some of them are shipped with NLTK.
3.2.1 Wordnet
Wordnet is a lexical database for English language. Nouns, verbs, adjectives and adverbs are grouped
into sets of synonyms (synsets), each expressing a distinct concept. Synsets are interlinked by means of
semantic and lexical relations. The result is a meaningful hierarchical network of related words, which is
useful for text analysis and artificial intelligence.
NLTK is shipped with Wordnet 3.0.
3.2.2 Brown
The Brown University Standard Corpus of Present-Day American English (or just Brown corpus) con-
sists of 1,014,312 wordsl of running text of edited English prose printed in the United States during the
calendar year 1961. The samples were divided into categories and subdivisions. A tagged version of the
corpus, in which every word was tagged with its part of speech is also available in NLTK.
3.2.3 Semcor
A subset of Brown corpus in which words are also tagged with their sense along with their part of speech.
Available at Rada Mihalcea’s page
Collections of ‘which’ and ‘what’ questions tagged with part of speech and intention of the question.
Available at Rada Mihalcea’s page
• Python 2.7
• PyYAML
• NLTK
After installing all packages, run the Python IDLE (see Getting Started), and type the commands:
7
A new window should open, showing the NLTK Downloader. Click on the File menu and select
Change Download Directory. For central installation, set this to C:\nltk data. Next, select
the packages or collections you want to download.
If you did not install the data to one of the above central locations, you will need to set the NLTK DATA
environment variable to specify the location of the data. Right click on My Computer, select Properties>Advanced>En
Variables>User Variables>New...
Test that the data has been installed as follows. (This assumes you downloaded the Brown Corpus):
8
Chapter 4
Text Clean Up
4.1 Introduction
Usually the very first step of every text processing tasks. A clever cleaning up can benefit the project in
many ways. Even if it’s just a simple procedure which filters out non-desired characters, the amount of
memory saving can be substantial considering a very large and noisy corpus such as html documents.
As in this project context, user input query will be tokenized (including punctuation), filtered out odd
characters and spell checked before being used for further processing.
4.2 Tokenizer
Here is the code for the tokenizer:
import re
TOK=r’(?:\b([\w][\w\-\’]*[\w]))|([ˆ\s\w])’
def tokenizer(sent,pattern=TOK):
return [item[0] and item[0] or item[1] \
for item in re.findall(pattern,sent)]
The function uses a regular expression define in pattern (default value is TOK) to search for words
and punctuations in the input string sent.
The expression A and B or C is an equivalent to:
if A is True: return B
else: return C
9
4.3 Spell Checker
4.3.1 Introduction
[17] reports a rate of 25 errors per thousand for handwritten essays by secondary school students, though
there was considerable variation between good spellers and poor spellers. On the other hand, a rate of
0.5% to 23% for bibliographic database was reported in [2]. Spelling error rate and it’s significance varies
depend on the application fields. For the project’s context where user types in the input query, the rate of
errors will be high, and they will have significant impact on the output results
There are many sources of spelling errors:
• Real word error: hole (hope), them (then). [17] found that real-word errors account for about a
quarter to a third of all spelling errors, perhaps more if you include word-division errors.
Depend on what kind of spelling errors, there are different methods for detection and correction [14].
Some methods are:
• N-gram technique
• Probability technique
• ...
As for this project, we will only focus on errors that result in nonexistent words. Section 4.3.2 will
explain in detail the method use in this project, while the rest of this chapter will evaluate and discuss on
the performance.
10
4.3.2 Method
• small dict.txt: Compiled using Wordnet and stop word lexicon in NLTK.
• wn dic.txt: The corpus of every lemma, its POS tag and associated definition. Compiled using
Wordnet.
Figure 4.1 shows a simplified flow chart for the spell checker
Stemmer
A handy and readily availbale stemmer called Porter Stemmer can be called using the following code:
import nltk
stemmer = nltk.stem.PorterStemmer()
However, the stemmer doesn’t validate the return solutions. This leads to many awkward results as
shown below.
>>> stemmer.stem(’goes’)
’goe’
>>> stemmer.stem(’propose’)
’propos’
>>> stemmer.stem(’grocery’)
’groceri’
11
>>> stemmer.stem(’groceries’)
’groceri’
For this reason, a more sophisticated stemmer was developed. It’s flow chart is shown in figure 4.2.
The stemmer utilises extra information such as exceptions and part of speech tags for validation,
hence a better accuracy. It can also return the definition associating with the part of speech of the token
during the process if required. Source code can be found in module morphy.py. Another version that
also return the meaning associated with the word can be found in module wn dict fast.py
Spelling Correction
The underlined approach was to figured out the similarity between 2 tokens. A few features were chosen
and combined either by linear or cascade combination. These features are:
• fitc(word, can): scans for match strings between word and cand, sums up their lengths then
weights.
• fitm(word, can): in some sense, it is the opposite of feature fitc. This weights the unique,
rather than common characters.
The experiment results indicated that feature transmuteI alone was sufficient. The other features
can be used for selecting potential candidates to speed up the process. Feature fitc was chosen because
of its high speed and usually resulted in a neither too broad or too restricted candidate set.
12
Detail Operation Explanation
Refer to figure 4.2 for the stemmer operation. The source code can be found in module morphy.py.
A flow chart for the spelling correction is shown in figure 4.3.
def interpolation(word,can,classifiers=properties):
’Linear interpolation of various classifiers, last element is used for candidate
sig = sum(wei for (prop,wei) in classifiers[:-1])
return 1.0*sum(prop(word,can)*wei for (prop,wei) in classifiers[:-1])/sig
The C(properties) is a vector of features together with their weights for linear interpolation as
can be seen in the function C(interpolation). The last element in the feature vector is used for
13
candidate filtering. As stated above, only C(transmuteI) will be used as feature and C(fitc) as
candidate filter.
Below are the implementation code for C(fitc):
Note that f itc is in range [0, 1]. The function implies that the bigger the value, the more similar the
words are. Furthermore, when C(word) or C(can) becomes longer, their common must approach their
lengths for f itc to be significant.
Calculating transmuteI is not very straightforward as in the case of f itc. The transformation from
C(word) to C(can) can be assumed to go through a series of basic transformation steps.
These steps are:
If 4 steps is required for the transformation, we’ll say the distance between the two is 4. Obviously,
there are infinite number of ways to transform one word to another, but there should be a limited number
of shortest paths. The function C(transmuteI) will calculate the shortest distance and will figure out
the intermediate transformation steps if required as well.
This can be achieved through a four steps process:
• Segment and align the two words based on the common string.
14
Firstly, search for the common string between the two words. This is a greedy process, which means it
tries to group as many adjacent characters as possible, for instance ’abc’ as opposed to ’a’,’b’,’c’
or ’ab’,’c’.
Sample code for searching function:
>>> common_strings_mk2(’abcdefghklm’,’21becdfhlkm’)
[’b’, ’cd’, ’f’, ’h’, ’k’, ’m’]
Secondly, segment and align the two words based on the common string. The desired output of this
process is shown in the example below.
def align(word,can,verbose=False):
...
>>> align(’abcdefghklm’,’21becdfhlkm’)
[[’a’, ’’, ’b’, ’’, ’cd’,’e’, ’f’,’g’, ’h’, ’’, ’k’,’l’, ’m’],
[’2’,’1’, ’b’,’e’, ’cd’, ’’, ’f’, ’’, ’h’,’l’, ’k’, ’’, ’m’],
[ 2, 4, 6, 8, 10, 12]]
Thirdly, assign transformation steps. The process bases on the fact that the transformation steps
will decide how the column vector look like. Table 4.3.2 showed the summary of the patterns and their
corresponding steps.
’c’
Delete ’c’
’’
’c’
Change ’c’ to ’d’
’d’
’’ ’d’ ’c’
Swap ’c’ and ’d’
’c’ ’d’ ’’
’’ ’’ ’d’ ’c’
but not Add ’c’
’c’ ’c’ ’d’ ’’
Finally, weight the transformation for calculating transmuteI. Some transformation are more likely
to happen than others, so these transformations should be weighed less, i.e. nearer distance. Table 4.3.2
shows the weighed transformations.
15
Swap Between vowels 0.5
Swap Between (g,h), (r,t), (c,h), (h,p), (h,s), (h,k), and (h,r) 0.5
Change a to e, o to u, e to a, u to o, i to e or y, y to i 0.5
All steps Not above cases 1.0
6
transmuteI = (4.2)
6 + sum(weighed transf ormations)
Function (4.2) implies that returned value of the function is equal to 1.0 only if the distance is zero,
converges to zero when distance approaches infinity, and reduces by one-third when distance is 3.0.
Some sample codes and outputs of a few keys functions were shown below:
def tagseq(word,can,verbose=False):
"Figure out the transmute steps given aligned sequences\n\
return value: [[weight,type,involved chars,index]]"
...
>>> tagseq(’abcdefghklm’,’21becdfhlkm’)
[[1.0, ’Change’, ’a to 2’, 0],
[1.0, ’Add’, ’1’, 1],
[1.0, ’Add’, ’e’, 3],
[1.0, ’Del’, ’e’, 5],
[1.0, ’Del’, ’g’, 7],
[1.0, ’Swap’, ’l and k’, 11]]
def transmuteI(word,can):
"Measure distance (˜steps require to tranform word to can)"
#tagseq(align(word,can)) originally was the steps require to tranform word to can
#Basic steps are add, delete, change, and swap (adjacent chars)
#Has been modified to weight the steps instead.
#For example: ’swap a and e:0.5’, ’change a to e’: 1.0
return 6.0/(6+sum([val[0] for val in tagseq(word,can)]))
>>> transmuteI(’abcdefghklm’,’21becdfhlkm’)
0.5
4.3.3 Results
16
>>> morphy(’goes’)
[[’go’, ’v’]]
>>> morphy(’propose’)
[[’propose’, ’v’]]
>>> morphy(’grocery’)
[[’grocery’, ’n’]]
>>> morphy(’groceries’)
[[’grocery’, ’n’]]
Some demonstration for the spelling correction. The error samples were taken in 4.3.1
>>> correct(’umrella’)
[’umbrella’]
>>> correct(’libary’)
[’library’]
>>> correct(’qhat’)
[’what’, ’that’, ’quat’, ’qat’, ’khat’, ’hat’, ’ghat’, ’chat’]
>>> correct(’no3’)
[’nox’, ’now’, ’nov’, ’not’, ’nos’, ’nor’, ’non’, ’noi’, ’nog’,
’noe’, ’nod’, ’noc’, ’nob’, ’no.’, ’no’]
>>> correct(’brid’)
[’rid’, ’grid’, ’brit’, ’bris’, ’brio’, ’brim’,
’brig’, ’brie’, ’bride’, ’braid’, ’brad’, ’bird’, ’bid’, ’arid’]
>>> correct(’gril’)
[’grit’, ’gris’, ’grip’, ’grin’, ’grim’, ’grill’, ’grid’,
’grail’, ’girl’, ’aril’]
>>> correct(’wuns’)
[’uns’, ’buns’]
>>> correct(’sed’)
[’sad’]
>>> correct(’stopt’)
[’stout’, ’stops’, ’stop’, ’stoat’]
>>> correct(’happend’)
[’happen’, ’append’]
>>> correct(’realy’)
[’reply’, ’rely’, ’relay’, ’redly’, ’realty’, ’realm’,
’really’, ’real’, ’ready’, ’mealy’]
4.3.4 Evaluation
17
On the other hand, it’s hard to define a baseline or perform an accuracy test for the spelling checker
because due to undetermined nature of spelling errors, there might be no unique correct answer. The best
solution therefore would be letting the user choose from a list of suggested words. So we would say a
spell checker fails only if it lefts out the intended solutions.
From the above testing experiment, the spell checked failed in 4 cases: ‘wuns’, ‘sed’, ‘stopt’, and
‘happend’. However, these four cases couldn’t be helped since they were errors due to spelling by sound
and the spell checker wasn’t designed for this type of errors.
Further experiments by quickly skimming through a dictionary, catching one glance at a long word
and rapidly typing it into the computer showed that the spell checker rarely failed. One of the few failed
cases was ‘lurve’, which was one of Woody Allen’s words for ‘love’ since he thought ‘love’ was too weak
of a word.
>>> correct(’lurve’)
[’lure’, ’curve’]
18
Chapter 5
5.1 Introduction
Most of the time, we can understand an utterance while paying little to no regard to its syntax. For
example, “I am driving a car”, or some random jungle talk “George good, George no hurt you!”.
However, at times when the syntax becomes complicated, or there is ambiguity, some syntax analysis
will be necessary to understand the utterance’s sense correctly. In fact most of natural language tasks can
be viewed ask resolving ambiguity at some points.
Let’s consider a few examples:
4. When Ulysses S. Grant and Robert E. Lee met in the parlor of a modest house at Appomattox Court
House, Virginia to work out the terms for the surrender of Lee’s Army of Northern Virginia, a great
chapter in American life came to a close and a greater chapter began.
Considering example 1, given the usual structure of the verb eat, this utterance can either mean that
the speaker wants to eat at some nearby location, or that the person wants to swallow the place. The latter
is much less likely to happen in real world.
Considering example 2, depending on which part of the sentence that the preposition phrase “until
4PM” modifies, the meaning can be “being out of town before 4PM and arriving in town only then”, or
“arriving in town at some earlier time but not staying as late as 4PM”.
The same goes for example 3, the clause “because she was rich” can modify either the state of being
married (the whole utterance) or just the cause (the verb only).
Example 4 might look complicated at first, but closer inspection reveals a quite simple structure: a
sentence pre modifier ( When Ulysses S. Grant and Robert E. Lee met in the parlor of a modest house
at Appomattox Court House, Virginia to work out the terms for the surrender of Lee’s Army of Northern
19
Virginia,) followed by a series of simple sentences ([a great chapter in American life came to a close][
and a greater chapter began.]
As those examples above illustrated, a good syntax analysis will make the task of meaning extraction
easier and more precise than intuition alone. In this chapter, we’ll explore the syntax analysis, i.e. POS
tagging. Section 5.2 will provide some information about method used. Necessary mathematical calcula-
tion is covered in section 5.3. The following sections will continue to elaborate on the program operation
and its performance.
5.2 Method
The language is assumed to be a second order hidden Markov model (HMM), which means that the
choice of a word is only depend on its previous two words. This of course is not true, but not totally false
either. Let’s consider an example:
“I painted my neighbour’s whole new . . . green except for the front door yesterday.”
Which word will be likely to fill in the empty space? It should be something that can be painted on,
such as face, house, board, pants, or cow. But only “house” are related to “door”, which appeared quite
far after the empty location. Our current HMM model will fail in this case unless the term “whole new
house” happens to appear more often than the others in usual context.
However, considering our context which is part of speech tagging, the HMM model works much more
efficiently since grammar rules restrict which classes can stand next to each other. Some examples are
“adjective usually precede noun”, or “to must be follow by noun phrase or bare infinitive verb”.
A tagger based on HMM model will estimate the probability of various sequences and return the most
probable sequence or sequences.
A HMM model has some major advantages compared to a decision tree model.
• It would required an adept knowledge in linguistics to capture all the grammar rules for using in
the decision tree, since a strict definition of a grammar rule is not easy to define.
• Most words in English are unambiguous, that is they only have one part of speech. But many of
the most common words in English are not. In fact [9]stated that over 40
• A single HMM tagger can be reused for many languages or applications if provided with proper
training data which in this case is a set of sentences tagged with each word’s part of speech. This
kind of database is usually simpler to prepare but time consumming.
• True model of the language is not known and can only be approximated.
• It is impossible to have a databse of every intance of a language, the HMM model should account
for these unseen events (also known as smoothing)
20
• A database of sufficient size for trainning must be availbe.
In this project, a trigram, bigram and unigram classifiers were linearly interpolated into a tagger call
lihmmtagger. In case this tagger fails to tag a words, an prefix tagger, a regular expression tagger and
a default tagger will be called in that order to prevent error propagation caused by failing to tag a word.
The source code can be found in module tagger.py.
The tagging problem can be defined for the trigram model as follow: giving sequence of[t1 , t2 ] (POS 1
and 2), find the probability of the sequence being follow by word w3 which has POS t3 i.e.
P (t3 , w3 |t1 , t2 ) = P (w3 |t1 , t2 , t3 ) × P (t3 |t1 , t2 ) = P (w3 |t3 ) × P (t3 |t1 , t2 ) (5.1)
The formula can be derived by making the assumption that w3 only depends on t3 .
Similarly, the bigram and unigram model can be defined:
Solution can be found using iinear interpolation of 5.1, 5.2, and 5.3:
Y
argmax P (wi |ti )(λ3 × P (ti |ti−2 , ti−1 ) + λ2 × P (ti |ti−1 ) + λ3 × P (ti )) (5.4)
1≤i≤n
In which λ1 , λ2 , λ3 are weights of the classifiers, λ1 + λ2 + λ3 = 1, and t−1 = t−2 =0 BOS 0 (Beginning
of Sentence). Logarithm is used when the numbers get smaller.
It should be pointed out that, the returned soultions is the most probable paths, i.e. the highest multiple
of probabilities. It’s not the same as the multiple of highest probabilities. One method for finding this
path is known as Viterbi algorithm [10]
For each trigram in training data, compare the following values: C(t1 ,t2 ,t3 )−1 C(t2 ,t3 )−1 C(t3 )−1
C(t1 ,t2 )−1 , C(t2 )−1 , N (t)−1
Depend on which is the maximum of them, increase the corresponding lambda by a certain amount. The
chosen amount were: 1, C(t1 , t2 , t3 ), C(t 1 ,t2 ,t3 )
C(t1 ,t2 )
The reason for minus 1 is because we treat the in using trigram as observed event, so the actual data must
be minus by 1. For this reason we skipped trigrams which have been seen only once.
21
5.4 Operation Explanation
The tagger flow chart is shown in figure 5.1
• A default tagger, which automatically assigns the most common tags which is ‘NN’ (147169 counts
in 1071233, that is 14%)
• train: takes a tagged corpus as trainning data and export training information for later use since
training might take a long time.
• accuracy: take a tagged corpus as test set and return the percentage of correctly tagged words.
22
• test: An improved accuracy test. Take a tagged coprus as test set, divide it into smaller sets and
perform accuracy test on each set. Return average accuracy and the standard deviations.
There are 170 possible tags, and since the tagger searches for the most probable path, the search space
can easily reach several hundred thousands in just 10 or 15 words, which are the most common sentence
length in Brown corpus. A search beam (1000 to 2000) must be applied to limit the search space, but
this will affect the accuracy as well. In order to improve speed of processing but limiting the affect on
accuracy at the same time, complex sentences which consist of more than one clauses will be broken and
tagged individually since words across clauses have little syntax relation.
5.5 Results
Below are some return scores in accuracy test.
Table 5.5 showed accuracy test for the linear interpolation HMM tagger.
Testset size Mean Mean w/o testset segmenting Variance Best Worst
120 63.25 64.72 132 78.04 45.45
1010 67.54 67.45 24 78.03 58.6
23
that the language model might go wrong at some points such as smoothing process or estimating lambda
values.
The speed is much slower than comparing to the other HMM tagger, especially for lengthy sentences.
By segmenting the sentences into clauses, the speed was practically double. The performance improve-
ments however was unknown. In some occasions during debugging process, segmenting the sentence into
smaller clauses proved to have better accuracy. The experiment logs are provided in the database.
Analyzing experiment logs suggested that punctuations can improve, but can also degrade the perfor-
mance dramatically. For instance, let’s consider a sample from the log:
The tagger assigned ‘what’ with a ‘‘‘’ tag instead of ‘WDT’. This might due to punctuations often
have very high frequency of appearance. More specifically, the POS tag ‘‘‘’ has a count of 6160 as
opposed to a count of 4834 for ‘WDT’.
On the other hand, since punctuations are more likely to be tagged correctly, they also have the effect
of limiting the error propagation. Therefore should the punctuations be considered during the training or
tagging process, whether their benefits outgrow their disadvantage is unresolved at the moment.
Even though it is possible to modify the program to carry out experiments for the sake of verifying the
above problem, considering the project’s context where the inputs are user search queries, punctuations
will not be a big issue. Furthermore, when applied to the semantics module (discussed in the next chapter)
the returned results were promising. So in conclusion, the program are good enough for practical usage,
even though the performance score was not as high as expected.
24
Chapter 6
Meaning Representation
6.1 Introduction
In the previous chapter, a few examples have illustrated that in many cases syntax analysis is useful but
not necessary for meaning comprehension. This implies that a different system other than grammar is
necessary for meaning representation. It makes sense since most of the grammar rules are about how
different words can be combined to form a sentence. The exception, for example is some verbs must
not stand alone on itself, like “give”. This class of verbs is known as transitive verb, and in someway
represents the sense of the verbs. However, consider some of everyday language tasks that require some
form of semantic processing:
• Answering an question.
• Following a recipe.
It is clear that none of morphological or syntactical representation thus far will get us very far on these
tasks. What is needed is a representation that can bridge the gap between linguistic inputs, their meaning
and the kind of real world knowledge that is needed to perform the involved tasks.
Over the years, a fair number of representational schemes have been invented to capture the meaning
of the natural language inputs for use in processing systems. Three notable schemes are First Order
Predicate Calculus (FOPC), Semantic Networks and Frames.
In this chapter, some background theory on FOPC and formal logic will be cover in 6.2. 6.3 will
explore how these theory can be apply to a specific case. The rest of this chapter will then explain how
the program achieved the desired result describe in 6.3.
25
6.2 Background Theory
Verifiability
The system’s ability to compare an affair described by a representation to affairs modeled in a knowledge
base.
Unambiguous Representations
Ambiguities exist in all aspects of all languages. Some means of determining that certain interpretations
are more or less preferable than others is needed.
Canonical Form
The phenomenon of distinct inputs that should be assigned the same meaning representations.
Inference
The system’s ability to derive valid conclusions based on meaning representations of inputs and its knowl-
edge base. The conclusions might be not explicitly represented in the knowledge base, but are logically
derivable from the available propositions.
Variables
Allow the system to match unknown entity to a known object in knowledge base so that the entire propo-
sitions is matched.
Expressiveness
Finally, to be useful a representation scheme must be expressive enough to cover a wide range of subject
matters such as time and tense.
Capturing meaning of a sentence involves identifying the terms and predicates corresponding to various
grammar elements of the sentence. Some basic building elements of FOPC are:
Constants
Refer to specific object in the world. Like in programming language, FOPC contants refer to exactly one
object, Objects can, however have more than one contants refer to them.
26
Functions
Functions in FOPC can be expressed as attributes of objects. FOPC functions have the same syntax as a
single argument predicate, however, they are in fact terms since they actually refer to unique objects.
Examples: LocationOf(NTU)
Variables
Give the system the ability to draw inferences or make assertions about objects without having to refer to
any particular ones.
Formular
An equivalent so sentence in grammar representation. FOPC formular is a representation of objects,
properties or relations between terms. Formulars therefore can be assigned with True or False values
depending on whether the information they encoded are in accord with the knowledge base or not. Note
that the arguments of formulars must be terms, i.e. constants, functions, and variables and not a formular.
Quantifiers
Variable can be used to making statements about either a particular unknown object, or all the objects
in some arbitrary classes of objects. Usage of quantifiers make these two uses possible. The two basic
operator in FOPC are ‘exist’ quantifier that denotes one particular unknown object, and ‘all’ quantifier
that refers to all objects in a class.
Lambda Notation
Enable formal functionality for replacing of a specifics variable by a specifics term.
Examples: lambda x P(x)(A) --> P(A)
The word ‘formal’ means by form, rather than by shape or meaning. Some necessary terms to read and
understand formal logic are:
• Proof and disproof: A formal proof is a logical argument which convinces by following formal
rules.
• Monotonicity principle: A proof cannot be invalidated by adding premises, since proof obeys rules,
not on meaning or facts.
27
• Formal rules: is an intermediate step in an logical arguments. Rules show how can larger proof be
made out of one or more smaller proofs.
• Connectives: the logic connectives are used to build larger claims out of smaller claim. There are
four connectives:
– And: If we accept ‘A and B’ then we are forced to accept both A and B simultaneously.
Each connective has two rules associated with them: introduction and elimination. The names
suggest that the connectives are introduced or eliminated in the final proof. For instance here is an
example of ‘If-then’ elimination rule:
(If A then B) also A, therefore B
28
lambda_x (witches(x)-->wood(x)) (3) implication-introduction (1) (2)
The claim at the end is nonsensical according to our intuition. Using logic as shown above, this is
true since there is one invalid proof at step (12). However, it is still quite possible for the claim to be true
if we rewrite the proof as below:
29
lambda_x (human(x) and not_wood(x) --> not_float(x)) (4.1) premise
A new premise (4.2), which is reasonable is introduced. Amazingly enough, the final claim which is
supposed to be humorous and nonsensical is true logically.
Design
The program derives the implication by figuring out what are the symbols, referents and their syntactical
roles in the sentences. Considering an example:
Q: Who is Albert?
A: He is a genius.
In the above example, ‘who’ and ‘he’ are symbols, and they refers to ‘genius’ and ‘Albert’ respec-
tively. Logically, the implication must satisfy the following two rules:
1. The left hand side of the implication must be either the argument or predicate of the question, and
the right hand side of the implication must be either the argument or predicate of the answer.
2. The left hand side must be meaningful, or the implication will be pretty much useless.
Following the above two rules, a sound implication would be Albert --> genius or Albert
--> he, and implication such as who --> he is not very informative.
The above two rules and referent identification process are embedded in deduct class, while predi-
cate and argument extraction are handled by sentence class.
phrase Class
As the name suggested, phrases such as noun phrase, preposition phrase or verb phrase can be modeled
using the class phrase
Some key attributes of the class phrase are:
30
• string - the actual string of the phrase.
• type - Type of phrases. Currently noun phrase, preposition phrase, verb phrase and adjective
phrase are supported
Each phrase has an associated function to search for that particular phrase in a sentence.
Sentence Class
This class is used to represent an instance of sentence. It provides convenient access to many components
of a sentence such as POS tags, subject, predicate, sentence type. . .
Some key attributes of the class Sentence are:
• type - Sentence type. Currently declarative, yes/no question and some of Wh-questions are sup-
ported.
deduct Class
To derive implications from a pair of question and answer. It is worh noting that the process relies solely
on syntactical information provided by the Sentence class and pays no regard to the actual meaning of
the tokens, hence the name syntax driven semantics analysis.
Some key attributes of the class deduct are:
• lhs - Instance of the sentence on the left hand side (the question).
• rhs - Instance of the sentence on the right hand side (the answer).
31
6.4.2 Presentation
Design
At the moment, the representation module is not integrated into the semantics analysis modules discussed
above since the extracted information is not detail enough for the module to generate an accurate rep-
resentation. More specifically, the module will need information such as what should the quantifier be,
what should the term be (variable, constant, or function).
However, if we are able to provide these information, the class atom can be used to generate a proper
representation.
atom Class
On top of generating basic atomic representations, lambda reduction and combining smaller expressions
are also supported.
6.5 Results
Below are the output implications for sample pairs of questions and answers. Some of them are from the
study case in section 6.3, and some statements are modified into questions for compatibility or manually
tagged if they were tagged wrongly.
32
q2=’What do you burn apart from witches?’
a2=’Wood.’
>>> deduct(q2,a2).presentation
[’burn/VB-->wood/NN’]
q8=’who is Albert’
a8=’He is a genius’
>>> deduct(q8,a8).presentation
[’albert/NP-->genius/NN’, ’albert/NP-->he/PPS’]
33
>>> deduct(q10,a10).presentation
[’the/AT most/QL stupid/JJ guy/NN
on/IN earth/NN-->dummies/NNS’]
Below are a few demonstrations including atomic terms, lambda reduction and combining smaller atoms
into a bigger representation.
>>> t1=atom(’witches’,None,’var’)
>>> t1.presentation
’lambda_x_witches Isa(x_witches,witches) connective ’
>>> t2=atom(’burn’,None,’var’)
>>> t2.presentation
’lambda_x_burn Isa(x_burn,burn) connective ’
>>> t3=atom(t2,t1)
>>> t3.presentation
’lambda_x_burn Isa(x_burn,burn) lambda_x_witches
Isa(x_witches,witches) connective Role_of_x_witches(x_burn,x_witches)’
>>> t4=atom(t3,Lambda=[[t1,’girl’]])
>>> t4.Apresentation
’Isa(x_burn,burn) Isa(girl,witches) connective Role_of_girl(x_burn,girl)’
6.6 Evaluations
Further expriments on the deduction program showed that even though the program analyses the sentence
in a rather simple minded way, it worked unexpectedly well for simple sentences, and for some a bit more
complex sentences.
34
Some current limitations are:
• Not all type of questions, as well as sentences with clauses and commas are supported. Currently
only yes no question, ‘what’, ‘which’, ‘who’, ‘whom’, ‘why’ and simple sentences are available.
• Unable to extract information such as tense, quantity or entity relationships, which are necessary
for FOPC representation scheme.
• The analysis bases only on syntactical roles and does not consider the actual meaning and the
relative locations of the tokens
Despite the limitations, the program does work in simple context. Hence it can either be used as a
layer in a multi level process in which each layer solves a particular problem, or be improved to be able
to dealt with complex sentences and extract more useful information.
As for the representation module, it worked as expected for simple formulas. As the formulas getting
more complex, connectives proved to be an ambiguity issue. Let’s consider an example:
- Every restaurant has a menu
The meaning representation of the sentence might take either one of the below two forms:
- all Restaurant(x) then exist e, y Having(e) and Haver(e, x) and Isa(y, Menu) and Had(e, y)
- exist y Isa(y, Menu) and all x Isa(x, Restaurant) then exist e Having(e) and Haver(e, x) and Had(e,
y)
In the worst case, a sentence with n quantifiers will have n! representations.
35
Chapter 7
Conclusion
This has been a very enjoyable project for me. I have had fun learning Python and study some very
interesting aspect of the language that I used to take for granted. Though I failed to achieve the initial
goals, I managed to retrieve other precious things in return: less arrogant, realized how magnificent this
world and its people are, and memorable moments such as the excitement when getting the program
running for first time, or had my heart sunk to my feet when hearing about IBM Watson’s triumph.
Though I felt that I could have done much more if I have had received more formal training on
linguistics, I was contended with the achievement. I really am glad to have the opportunity to work on
this project. My deepest gratitude’s owed to my supervisor professor Chan Chee Keong, thank you.
36
Bibliography
[1] Richard Bornat. Proof and Disproof in Formal Logic An introduction for programmers. Oxford
University Press,, 2005.
[2] Charles P. Bourne. Frequency and impact of spelling errors in bibliographic data bases. Information
Processing and Management, 13(1):1 – 12, 1977.
[5] Eric Brill and Robert C. Moore. An improved error model for noisy channel spelling correction. In
Proceedings of the 38th Annual Meeting on Association for Computational Linguistics, ACL ’00,
pages 286–293, Stroudsburg, PA, USA, 2000. Association for Computational Linguistics.
[6] Hinrich Schiitze Christopher D. Manning. Foundations of Statistical Natural Language Processing.
Massachusetts Institute of Technology, second edition, 1999.
[7] Kenneth W. Church and Lisa F. Rau. Commercial applications of natural language processing.
Commun. ACM, 38:71–79, November 1995.
[8] Fred J. Damerau. A technique for computer detection and correction of spelling errors. Commun.
ACM, 7:171–176, March 1964.
[9] James H. Martin Daniel Jurafsky. An Introduction to Natural Language Processing, Computational
Linguistics and Speech Recognition. Prentice Hall, 2000.
[10] JR. G. DAVID FORNEY. The viterbi algorithm. Computation and Neural Systems, 7, May 1991.
[11] Lisa F. Rau Kenneth W. Church. Commercial applications of natural language processing.
[12] Mark D. Kernighan, Kenneth W. Church, and William A. Gale. A spelling correction program based
on a noisy channel model. In Proceedings of the 13th conference on Computational linguistics - Vol-
ume 2, COLING ’90, pages 205–210, Stroudsburg, PA, USA, 1990. Association for Computational
Linguistics.
[13] Karen Kukich. Spelling correction for the telecommunications network for the deaf. Commun.
ACM, 35:80–90, May 1992.
37
[14] Karen Kukich. Techniques for automatically correcting words in text. ACM Comput. Surv., 24:377–
439, December 1992.
[15] Charles R. Litecky and Gordon B. Davis. A study of errors, error-proneness, and error diagnosis in
cobol. Commun. ACM, 19:33–38, January 1976.
[16] Mark D Ryan Michael RA Huth. Logic in Computer Science Modelling and reasoning about sys-
tems. Cambridge University Press,, 2000.
[17] Roger Mitton. Spelling checkers,spelling correctors and the misspellings of poor spellers. Inf.
Process. Manage., 23:495–505, September 1987.
[18] Jacob Perkins. Python Text Processing with NLTK 2.0 Cookbook. Packt Publishing, 2010.
[19] James L. Peterson. Computer programs for detecting and correcting spelling errors. Commun. ACM,
23:676–687, December 1980.
[20] Kristina Toutanova and Robert C. Moore. Pronunciation modeling for improved spelling correction.
In Proceedings of the 40th Annual Meeting on Association for Computational Linguistics, ACL ’02,
pages 144–151, Stroudsburg, PA, USA, 2002. Association for Computational Linguistics.
[21] Joseph Weizenbaum. Eliza—a computer program for the study of natural language commu-
nication between man and machine. Commun. ACM, 9:36–45, January 1966.
[22] Jean-Sbastien Sencal Frderic Morin Jean-Luc Gauvain Yoshua Bengio, Holger Schwenk. Neural
probabilistic language models.
38