from nltk_contrib.classifier.exceptions import systemerror as se
from nltk_contrib.classifier import autoclass as ac, cfile, decisionstump as ds
from nltk import probability as prob
import UserList

CONTINUOUS = 'continuous'
DISCRETE = 'discrete'

class Attribute:
    """
    Immutable object which represents an attribute/feature
    """
    def __init__(self, name, values, index):
        self.name = name
        self.values = values
        self.type = self.__get_type()
        self.index = index
    
    def __get_type(self):
        if len(self.values) == 1 and self.values[0] == CONTINUOUS:
            return CONTINUOUS
        return DISCRETE
        
    def has_value(self, to_test):
        return self.values.__contains__(to_test)
    
    def is_continuous(self):
        return self.type == CONTINUOUS
    
    def __eq__(self, other):
        if other is None: return False
        if self.__class__ != other.__class__: return False
        if self.name == other.name and \
           self.values == other.values and \
           self.index == other.index: 
            return True
        return False
    
    def __str__(self):
        return self.name +':[' + self.values_as_str() + '] index:' + str(self.index)
    
    def values_as_str(self):
        
        return ','.join([str(value) for value in self.values])
    
    def empty_freq_dists(self):
        return dict([(value, prob.FreqDist()) for value in self.values])
    
    def __hash__(self):
        return hash(self.name) + hash(self.index)