change DefaultDict, Dict to defaultdict, dict · TonyJava/aima-python@e734314 · GitHub
Skip to content

Commit e734314

Browse files
committed
change DefaultDict, Dict to defaultdict, dict
These functions, which I had to implement in Python 2.2, are now available in standard Python.
1 parent ecf9a2f commit e734314

8 files changed

Lines changed: 42 additions & 39 deletions

File tree

csp.py

Lines changed: 2 additions & 1 deletion

games.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Fig52Game(Game):
188188
B=dict(b1='B1', b2='B2', b3='B3'),
189189
C=dict(c1='C1', c2='C2', c3='C3'),
190190
D=dict(d1='D1', d2='D2', d3='D3'))
191-
utils = Dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)
191+
utils = dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)
192192
initial = 'A'
193193

194194
def actions(self, state):

logic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
diff, simp Symbolic differentiation and simplification
2525
"""
2626

27-
import itertools, re
27+
import itertools
28+
import re
2829
import agents
2930
from utils import *
31+
from collections import defaultdict
3032

3133
#______________________________________________________________________________
3234

@@ -608,7 +610,7 @@ def pl_fc_entails(KB, q):
608610
"""
609611
count = dict([(c, len(conjuncts(c.args[0]))) for c in KB.clauses
610612
if c.op == '>>'])
611-
inferred = DefaultDict(False)
613+
inferred = defaultdict(bool)
612614
agenda = [s for s in KB.clauses if is_prop_symbol(s.op)]
613615
while agenda:
614616
p = agenda.pop()

nlp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# from the third edition until this gets reviewed.)
55

66
from utils import *
7+
from collections import defaultdict
78

89
#______________________________________________________________________________
910
# Grammars and Lexicons
@@ -30,7 +31,7 @@ class Grammar:
3031
def __init__(self, name, rules, lexicon):
3132
"A grammar has a set of rules and a lexicon."
3233
update(self, name=name, rules=rules, lexicon=lexicon)
33-
self.categories = DefaultDict([])
34+
self.categories = defaultdict(list)
3435
for lhs in lexicon:
3536
for word in lexicon[lhs]:
3637
self.categories[word].append(lhs)

probability.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from utils import *
55
from logic import extend
6-
from random import choice, seed
6+
import random
7+
from collections import defaultdict
78

89
#______________________________________________________________________________
910

@@ -80,7 +81,7 @@ class JointProbDist(ProbDist):
8081
>>> P[dict(X=0, Y=1)]
8182
0.5"""
8283
def __init__(self, variables):
83-
update(self, prob={}, variables=variables, vals=DefaultDict([]))
84+
update(self, prob={}, variables=variables, vals=defaultdict(list))
8485

8586
def __getitem__(self, values):
8687
"Given a tuple or dict of values, return P(values)."
@@ -399,7 +400,7 @@ def rejection_sampling(X, e, bn, N):
399400
evidence e in BayesNet bn, using N samples. [Fig. 14.14]
400401
Raises a ZeroDivisionError if all the N samples are rejected,
401402
i.e., inconsistent with e.
402-
>>> seed(47)
403+
>>> random.seed(47)
403404
>>> rejection_sampling('Burglary', dict(JohnCalls=T, MaryCalls=T),
404405
... burglary, 10000).show_approx()
405406
'False: 0.7, True: 0.3'
@@ -421,7 +422,7 @@ def consistent_with(event, evidence):
421422
def likelihood_weighting(X, e, bn, N):
422423
"""Estimate the probability distribution of variable X given
423424
evidence e in BayesNet bn. [Fig. 14.15]
424-
>>> seed(1017)
425+
>>> random.seed(1017)
425426
>>> likelihood_weighting('Burglary', dict(JohnCalls=T, MaryCalls=T),
426427
... burglary, 10000).show_approx()
427428
'False: 0.702, True: 0.298'
@@ -450,7 +451,7 @@ def weighted_sample(bn, e):
450451

451452
def gibbs_ask(X, e, bn, N):
452453
"""[Fig. 14.16]
453-
>>> seed(1017)
454+
>>> random.seed(1017)
454455
>>> gibbs_ask('Burglary', dict(JohnCalls=T, MaryCalls=T), burglary, 1000
455456
... ).show_approx()
456457
'False: 0.738, True: 0.262'
@@ -460,7 +461,7 @@ def gibbs_ask(X, e, bn, N):
460461
Z = [var for var in bn.vars if var not in e]
461462
state = dict(e) # boldface x in Fig. 14.16
462463
for Zi in Z:
463-
state[Zi] = choice(bn.variable_values(Zi))
464+
state[Zi] = random.choice(bn.variable_values(Zi))
464465
for j in xrange(N):
465466
for Zi in Z:
466467
state[Zi] = markov_blanket_sample(Zi, state, bn)

search.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -473,33 +473,33 @@ def distance_to_node(n):
473473
g.connect(node, neighbor, int(d))
474474
return g
475475

476-
romania = UndirectedGraph(Dict(
477-
A=Dict(Z=75, S=140, T=118),
478-
B=Dict(U=85, P=101, G=90, F=211),
479-
C=Dict(D=120, R=146, P=138),
480-
D=Dict(M=75),
481-
E=Dict(H=86),
482-
F=Dict(S=99),
483-
H=Dict(U=98),
484-
I=Dict(V=92, N=87),
485-
L=Dict(T=111, M=70),
486-
O=Dict(Z=71, S=151),
487-
P=Dict(R=97),
488-
R=Dict(S=80),
489-
U=Dict(V=142)))
490-
romania.locations = Dict(
476+
romania = UndirectedGraph(dict(
477+
A=dict(Z=75, S=140, T=118),
478+
B=dict(U=85, P=101, G=90, F=211),
479+
C=dict(D=120, R=146, P=138),
480+
D=dict(M=75),
481+
E=dict(H=86),
482+
F=dict(S=99),
483+
H=dict(U=98),
484+
I=dict(V=92, N=87),
485+
L=dict(T=111, M=70),
486+
O=dict(Z=71, S=151),
487+
P=dict(R=97),
488+
R=dict(S=80),
489+
U=dict(V=142)))
490+
romania.locations = dict(
491491
A=( 91, 492), B=(400, 327), C=(253, 288), D=(165, 299),
492492
E=(562, 293), F=(305, 449), G=(375, 270), H=(534, 350),
493493
I=(473, 506), L=(165, 379), M=(168, 339), N=(406, 537),
494494
O=(131, 571), P=(320, 368), R=(233, 410), S=(207, 457),
495495
T=( 94, 410), U=(456, 350), V=(509, 444), Z=(108, 531))
496496

497-
australia = UndirectedGraph(Dict(
498-
T=Dict(),
499-
SA=Dict(WA=1, NT=1, Q=1, NSW=1, V=1),
500-
NT=Dict(WA=1, Q=1),
501-
NSW=Dict(Q=1, V=1)))
502-
australia.locations = Dict(WA=(120, 24), NT=(135, 20), SA=(135, 30),
497+
australia = UndirectedGraph(dict(
498+
T=dict(),
499+
SA=dict(WA=1, NT=1, Q=1, NSW=1, V=1),
500+
NT=dict(WA=1, Q=1),
501+
NSW=dict(Q=1, V=1)))
502+
australia.locations = dict(WA=(120, 24), NT=(135, 20), SA=(135, 30),
503503
Q=(145, 20), NSW=(145, 32), T=(145, 42), V=(145, 37))
504504

505505
class GraphProblem(Problem):

text.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from utils import *
88
from learning import CountingProbDist
99
from math import log, exp
10-
import re, search
10+
from collections import defaultdict
11+
import re
12+
import search
1113

1214
class UnigramTextModel(CountingProbDist):
1315
"""This is a discrete probability distribution over words, so you
@@ -28,7 +30,7 @@ def __init__(self, n, observation_sequence=[]):
2830
## mapping from (w1, ..., wn-1) to P(wn | w1, ... wn-1)
2931
CountingProbDist.__init__(self)
3032
self.n = n
31-
self.cond_prob = DefaultDict(CountingProbDist())
33+
self.cond_prob = defaultdict(CountingProbDist())
3234
self.add_sequence(observation_sequence)
3335

3436
## __getitem__, top, sample inherited from CountingProbDist
@@ -104,7 +106,7 @@ def __init__(self, stopwords='the a of'):
104106
"""Create an IR System. Optionally specify stopwords."""
105107
## index is a map of {word: {docid: count}}, where docid is an int,
106108
## indicating the index into the documents list.
107-
update(self, index=DefaultDict(DefaultDict(0)),
109+
update(self, index=defaultdict(lambda: defaultdict(int)),
108110
stopwords=set(words(stopwords)), documents=[])
109111

110112
def index_collection(self, filenames):

utils.py

Lines changed: 0 additions & 4 deletions

0 commit comments

Comments
 (0)