[Tutor] Newbie

Nicole Seitz Nicole.Seitz@urz.uni-hd.de
Mon, 18 Feb 2002 22:48:04 +0100

Hi there,

months ago I gave up learning to program with C.Now I hope  I'll be more 
successful with Python.
I decided to learn Python by programming a table-controlled shift-reduce 
parser for the English language.I'm still struggling with the prephase, 
where I assign to each  word the correct category( visit = verb, etc.)
Well, here's one problem I don't know how to deal with:

The user should be able to change the lexicon. Currently this lexicon looks 
like that

ocean n
pleasure n
fish n
men n
Egypt n
friends n
Seattle n
London n
students n
tourists n
France n

in prep
near prep
with prep

visit vt
study vt

sleep vi
fish vi

their det
the det
an det
a det

big adj
good adj


These two functions in the module lexphase.py enable the user to add a word 
which was not found :

def WordNotFound(unknown_word) :

    print "Couldn't find %s in lexicon!" %unknown_word
    AddOrNot = raw_input("Would you like to add it to lexicon?(yes or no)")
    if (AddOrNot == "yes") :
         return new_category
         print "Bad choice!!"

def AddWord( ):
         global new_category

         new_word = raw_input("New word: ")
         new_category = raw_input("Category: ")
         new_entry ="\n"+ new_word + " "+ new_category
         file = open('lexicon.txt','a')
So far, so good. This works somehow.But unfortunately, after adding a new 
word, the lexicon isn't sorted anymore.
For example, I could have
sleep vi
fish vi

their det
the det
an det
a det

big adj
good adj
Paris n   #DOn't want the new entry HERE!!!!!!!!

So, how can I keep my lexicon sorted?

Many greetings and thanx in advance,