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

Source Code for Module nltk_lite.contrib.classifier.autoclass

 1  # Natural Language Toolkit - AutoClass 
 2  #  automatic class value generator 
 3  # 
 4  # Author: Sumukh Ghodke <sumukh dot ghodke at gmail dot com> 
 5  # 
 6  # URL: <http://nltk.sf.net> 
 7  # This software is distributed under GPL, for license information see LICENSE.TXT 
 8   
9 -class AutoClass:
10 - def __init__(self, name):
11 self.name = name
12
13 - def next(self):
14 base26 = self.base26() 15 base26 += 1 16 return AutoClass(string(base26))
17
18 - def base26(self):
19 base26 = 0 20 length = len(self.name) 21 for index in range(length): 22 numeric = ord(self.name[index]) - 97 23 if (index == length - 1): base26 += numeric 24 else: base26 += numeric * 26 * (length - index - 1) 25 return base26
26
27 - def __eq__(self, other):
28 if other is None: return False 29 if self.__class__ != other.__class__: return False 30 if self.name == other.name: return True 31 return False
32
33 - def __hash__(self):
34 if self.name == None: return id(self) 35 return 3 * self.name + 7
36
37 - def __str__(self):
38 return self.name
39 40 FIRST = AutoClass('a') 41
42 -def string(base26):
43 strn = '' 44 while (base26 /26 > 0): 45 strn = chr((base26 % 26) + 97) + strn 46 base26 = base26 / 26 47 strn = chr((base26 % 26) + 97) + strn 48 return strn
49