__author__  = "Hugo Liu <hugo@media.mit.edu>"
__version__ = "1.3"
import sys,string,bisect
from types import *
class LexiconEfficient:


    path_prefix = ''
    lexicon_filename = path_prefix+'LEXICON'
    lex = []
    length_of_lex = 0
    lowercased_keys = []
    def __init__(self):

        self.populate_lexicon_from_file(self.lexicon_filename)
        keys1 = map(lambda x: x[0].lower(),self.lex)
        unique = {}
        for key in keys1:
            unique[key] = 1
        keys2 = unique.keys()
        keys2.sort()
        self.lowercased_keys = keys2
        print "Lexicon OK!"
        return
    def compare(self,element1,element2):

        if element1[0]<element2[0]: return -1
        elif element1[0]>element2[0]: return 1
        else: return 0
    def get(self,word,default):

        index = bisect.bisect(self.lex,(word,))
        if index >= self.length_of_lex:
            return default
        elif self.lex[index][0] == word:
            return self.lex[index][1]
        else:
            return default
    def primary_pos(self,word):

        pos_arr = self.get(word,[])
        if pos_arr == []:
            return ""
        else:
            return pos_arr[0]
    def all_pos(self,word):

        pos_arr = self.get(word,[])
        return pos_arr
    def has_pos(self,word,pos):

        return pos in self.get(word,[])
    def is_word(self,word,case_sensitivity=0):

        if case_sensitivity:
            return (self.get(word,[]) != [])
        else:
            if word.lower() in self.lowercased_keys:
                return 1
            else:
                return 0
    def populate_lexicon_from_file(self,filename):

        l = self.lex
        try:
            f = open(filename,'r')
            line = f.readline()
            while line:
                toks = string.split(line)
                word = toks[0]
                tags = tuple(toks[1:])
                l.append((word,tags))
                line = f.readline()
            f.close()
        except:
            print "Error parsing Lexicon!"
            sys.exit(-1)
        l.sort(self.compare)
        self.length_of_lex = len(self.lex)
        return
if __name__ == "__main__":
    l = LexiconEfficient()
    a = "This sentence is false ."
    for word in string.split(a):
        print l.primary_pos(word)
    raw_input("Press return to quit")