Package nltk_lite :: Package contrib :: Package classifier :: Module instance
[hide private]
[frames] | no frames]

Source Code for Module nltk_lite.contrib.classifier.instance

  1  # Natural Language Toolkit - Instance 
  2  #  Understands the various operations that can be preformed on an instance 
  3  #     Each Instance inheriting from the main Instance is capable of operations 
  4  #     it can logically perform on that instance value eg: Test instance can  
  5  #     set the Class where as the Training instance cannot 
  6  # 
  7  # Author: Sumukh Ghodke <sumukh dot ghodke at gmail dot com> 
  8  # 
  9  # URL: <http://nltk.sf.net> 
 10  # This software is distributed under GPL, for license information see LICENSE.TXT 
 11   
 12  from nltk_lite.contrib.classifier.exceptions import systemerror as system, invaliddataerror as inv 
 13  import item 
 14   
15 -class Instance:
16 - def __init__(self):
17 self.klass_value, self.attrs, self.classified_klass = None, None, None
18
19 - def is_valid(self, klass, attributes):
20 return AssertionError()
21
22 - def value(self, attribute):
23 if attribute.is_continuous(): 24 return float(self.attrs[attribute.index]) 25 return self.attrs[attribute.index]
26
27 - def values(self, attributes):
28 _values = [] 29 for attribute in attributes: 30 _values.append(self.attrs[attribute.index]) 31 return _values
32
33 - def discretise(self, discretised_attributes):
34 for discretised_attribute in discretised_attributes: 35 index = discretised_attribute.index 36 self.attrs[index] = discretised_attribute.mapping(float(self.attrs[index]))
37
38 - def remove_attributes(self, attributes):
39 to_be_removed = [] 40 for attribute in attributes: 41 to_be_removed.append(self.attrs[attribute.index]) 42 for r in to_be_removed: 43 self.attrs.remove(r)
44
45 - def __eq__(self, other):
46 if other is None: return False 47 if self.__class__ != other.__class__: return False 48 if self.klass_value == other.klass_value and self.attrs == other.attrs and self.classified_klass == other.classified_klass: return True 49 return False
50
51 - def __str__(self):
52 return self.str_attrs() + self.str_class() + self.str_klassified_klass()
53
54 - def str_klassified_klass(self):
55 return ' Classified as: ' + self.check_none(self.classified_klass)
56
57 - def check_none(self, var):
58 if var is None: 59 return ' ' 60 return var.__str__()
61
62 - def str_class(self):
63 return ' Class: ' + self.check_none(self.klass_value)
64
65 - def str_attrs(self):
66 return 'Attributes: ' + self.check_none(self.attrs)
67
68 - def attr_values_as_str(self):
69 strn = '' 70 for attr in self.attrs: 71 strn += attr 72 strn += ',' 73 return strn[:-1]
74
75 -class TrainingInstance(Instance):
76 - def __init__(self, attr_values, klass_value):
77 Instance.__init__(self) 78 self.klass_value, self.attrs = klass_value, attr_values
79
80 - def is_valid(self, klass, attributes):
81 return klass.__contains__(self.klass_value) and attributes.has_values(self.attrs)
82
83 - def __str__(self):
84 return self.str_attrs() + self.str_class()
85
86 -class TestInstance(Instance):
87 - def __init__(self, attr_values):
88 Instance.__init__(self) 89 self.attrs = attr_values
90
91 - def set_klass(self, klass):
92 self.classified_klass = klass
93
94 - def is_valid(self, klass, attributes):
95 return attributes.has_values(self.attrs)
96
97 - def __str__(self):
98 return self.str_attrs() + self.str_klassified_klass()
99
100 -class GoldInstance(TrainingInstance, TestInstance):
101 - def __init__(self, attr_values, klass_value):
102 TrainingInstance.__init__(self, attr_values, klass_value)
103
104 - def is_valid(self, klass, attributes):
105 return TrainingInstance.is_valid(self, klass, attributes)
106
107 - def classificationType(self):
108 if self.classified_klass == None: raise system.SystemError('Cannot find classification type for instance that has not been classified')
109
110 - def __str__(self):
111 return Instance.__str__(self)
112