__author__  = "Hugo Liu <hugo@media.mit.edu>"
__version__ = "1.3.1"
import sys,string
import MontyUtils
from types import *
class LexicalRuleParser:


    theLexicon = None
    lexicalrules_filename = 'LEXICALRULEFILE'
    lex_rules = []
    rule_names = ['char','hassuf','deletesuf','addsuf','haspref','deletepref','addpref','goodleft','goodright']
    rule_names += map(lambda x: 'f'+x,rule_names)
    def __init__(self,LexiconHandle):

        self.theLexicon = LexiconHandle
        self.lexicalrules_filename = MontyUtils.MontyUtils().find_file(self.lexicalrules_filename)
        if self.lexicalrules_filename == '':
            print "ERROR: could not find LEXICALRULEFILE"
            print "in current dir, %MONTYLINGUA% or %PATH%"
        self.populate_from_file(self.lexicalrules_filename)
        print 'LexicalRuleParser OK!'
        return
    def apply_all_rules(self,text_arr,word_index):

        rules = self.lex_rules
        for i in range(len(rules)):
            rule = rules[i]
            self.apply_rule(rule,text_arr,word_index)
    def apply_rule(self,rule,text_arr,word_index):

        word = text_arr[word_index]['word']
        pos = text_arr[word_index]['pos']
        is_word = self.theLexicon.is_word
        pred = rule[0]
        f_p = 0
        if pred[0] == 'f':
            pred = pred[1:]
            f_p = 1
        args = rule[1]
        left_word = ''
        right_word = ''
        if word_index > 0:
            left_word = text_arr[word_index-1]['word']
        if word_index < len(text_arr)-1:
            right_word = text_arr[word_index+1]['word']
        from_tag = ''
        if f_p:
            from_tag = args[0]
            args = args[1:]
        pattern = args[0]
        to_tag = args[-2]
        pattern = pattern.lower()
        word = word.lower()
        left_word = left_word.lower()
        right_word = right_word.lower()
        if f_p and (pos != from_tag):
            return
        if pred == 'char':
            if pattern in word:
               text_arr[word_index]['pos'] = to_tag
        elif pred == 'hassuf':
            if pattern is word[len(pattern):]:
               text_arr[word_index]['pos'] = to_tag
        elif pred == 'deletesuf':
            if pattern is word[len(pattern):] and is_word(word[:len(pattern)]):
                text_arr[word_index]['pos'] = to_tag
        elif pred == 'addsuf':
            if is_word(word + pattern):
                text_arr[word_index]['pos'] = to_tag
        elif pred == 'haspref':
            if pattern is word[:len(pattern)]:
               text_arr[word_index]['pos'] = to_tag
        elif pred == 'deletepref':
            if pattern is word[:len(pattern)] and is_word(word[len(pattern)-1:]):
                text_arr[word_index]['pos'] = to_tag
        elif pred == 'addsuf':
            if is_word(pattern + word):
                text_arr[word_index]['pos'] = to_tag
        elif pred == 'goodleft':
            if left_word == pattern:
                text_arr[word_index]['pos'] = to_tag
        elif pred == 'goodright':
            if right_word == pattern:
                text_arr[word_index]['pos'] = to_tag
        return
    def populate_from_file(self,filename):

        try:
            f = open(filename,'r')
            line = f.readline()
            while line:
                toks = string.split(line)
                pred = ''
                for rule_name in self.rule_names:
                    if rule_name in toks:
                        pred = rule_name
                        break
                args = toks
                rule = [pred,args]
                self.lex_rules.append(rule)
                line = f.readline()
            f.close()
        except:
            print "Error parsing Lexical rule file!"
            sys.exit(-1)
        return