Skip to content

Instantly share code, notes, and snippets.

@stevenbedrick
Created December 10, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenbedrick/97c11ecbcd480afde1dd to your computer and use it in GitHub Desktop.
Save stevenbedrick/97c11ecbcd480afde1dd to your computer and use it in GitHub Desktop.
StateCounter
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]
@stevenbedrick
Copy link
Author

Useful for when you're building an OpenFST transducer and want to identify states by something more meaningful than simple integers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment