__author__  = "Hugo Liu <hugo@media.mit.edu>"
__version__ = "1.3.1"
import sys,string
from types import *
import MontyUtils
class Lexicon:


    lexicon_filename = 'LEXICON'
    lex_dict = {}
    lowercased_keys = []
    def __init__(self):

        self.lexicon_filename = MontyUtils.MontyUtils().find_file(self.lexicon_filename)
        if self.lexicon_filename == '':
            print "ERROR: could not find LEXICON"
            print "in current dir, %MONTYLINGUA% or %PATH%"
        self.populate_lexicon_from_file(self.lexicon_filename)
        keys1 = map(lambda x: x.lower(),self.lex_dict.keys())
        unique = {}
        for key in keys1:
            unique[key] = 1
        keys2 = unique.keys()
        keys2.sort()
        self.lowercased_keys = keys2
        print "Lexicon OK!"
        return
    def primary_pos(self,word):

        pos_arr = self.lex_dict.get(word,[])
        if pos_arr == []:
            return ""
        else:
            return pos_arr[0]
    def all_pos(self,word):

        pos_arr = self.lex_dict.get(word,[])
        return pos_arr
    def has_pos(self,word,pos):

        return pos in self.lex_dict.get(word,[])
    def is_word(self,word,case_sensitivity=0):

        if case_sensitivity:
            return self.lex_dict.has_key(word)
        else:
            if word.lower() in self.lowercased_keys:
                return 1
            else:
                return 0
    def populate_lexicon_from_file(self,filename):

        l = self.lex_dict
        try:
            f = open(filename,'r')
            line = f.readline()
            while line:
                toks = string.split(line)
                word = toks[0]
                tags = toks[1:]
                l[word] = tags
                line = f.readline()
            f.close()
        except:
            print "Error parsing Lexicon!"
            sys.exit(-1)
        return
if __name__ == "__main__":
    l = Lexicon()
    a = "This sentence is false ."
    for word in string.split(a):
        print l.primary_pos(word)
    raw_input("Press return to quit")