Storing an ordered dictionary in Redis
An ordered dictionary is like a normal dict
, but the keys are ordered by an ordering function. In the case of Redis, it supports ordered dictionaries whose keys are strings and whose values are floating point scores. This structure can come in handy in cases where we need to calculate the information gain (covered in the Calculating high information words recipe in Chapter 7, Text Classification), and when you want to store all the words and scores for later use.
Getting ready
Again, you'll need Redis
and redis-py
installed with an instance of redis-server
running, as explained in the earlier recipe, Storing a frequency distribution in Redis.
How to do it...
The RedisOrderedDict
class in rediscollections.py
extends collections.MutableMapping
to get a number of dict
compatible methods for free. Then, it implements all the key methods that require Redis
ordered set (also known as Zset) commands:
class RedisOrderedDict(collections.MutableMapping): def __init__...