Created
December 10, 2014 23:03
-
-
Save stevenbedrick/97c11ecbcd480afde1dd to your computer and use it in GitHub Desktop.
StateCounter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StateCounter(object): | |
def __init__(self): | |
self.set = {} | |
self.count = -1 | |
def __contains__(self, obj): | |
return obj in self.set | |
def __getitem__(self, obj): | |
if not obj in self.set: | |
self.count += 1 | |
self.set[obj] = self.count | |
return self.set[obj] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for when you're building an OpenFST transducer and want to identify states by something more meaningful than simple integers.