from
nltk.probability
import
ConditionalFreqDist
from
rediscollections
import
encode_key
class
RedisConditionalHashFreqDist(ConditionalFreqDist):
def
__init__(
self
, r, name, cond_samples
=
None
):
self
._r
=
r
self
._name
=
name
ConditionalFreqDist.__init__(
self
, cond_samples)
for
key
in
self
._r.keys(encode_key(
'% s:*'
%
name)):
condition
=
key.split(
':'
)[
1
]
self
[condition]
def
__getitem__(
self
, condition):
if
condition
not
in
self
._fdists:
key
=
'% s:% s'
%
(
self
._name, condition)
val
=
RedisHashFreqDist(
self
._r, key)
super
(RedisConditionalHashFreqDist,
self
).__setitem__(
condition, val)
return
super
(
RedisConditionalHashFreqDist,
self
).__getitem__(condition)
def
clear(
self
):
for
fdist
in
self
.values():
fdist.clear()