From b5bb68ebefff92abef2afb1df1e4a3eb77b57948 Mon Sep 17 00:00:00 2001 From: Kritika Date: Sat, 25 Mar 2017 15:44:54 +0530 Subject: [PATCH 1/4] removed flake8 errors --- csp.py | 5 +++-- logic.py | 4 ++-- text.py | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/csp.py b/csp.py index deb1efc12..3bd6d88ac 100644 --- a/csp.py +++ b/csp.py @@ -20,7 +20,7 @@ class CSP(search.Problem): the other variables that participate in constraints. constraints A function f(A, a, B, b) that returns true if neighbors A, B satisfy the constraint when they have values A=a, B=b - + In the textbook and in most mathematical definitions, the constraints are specified as explicit pairs of allowable values, but the formulation here is easier to express and more compact for @@ -347,6 +347,7 @@ def topological_sort(X, root): build_topological(root, None, neighbors, visited, stack, parents) return stack, parents + def build_topological(node, parent, neighbors, visited, stack, parents): """Builds the topological sort and the parents of each node in the graph""" visited[node] = True @@ -356,7 +357,7 @@ def build_topological(node, parent, neighbors, visited, stack, parents): build_topological(n, node, neighbors, visited, stack, parents) parents[node] = parent - stack.insert(0,node) + stack.insert(0, node) def make_arc_consistent(Xj, Xk, csp): diff --git a/logic.py b/logic.py index 68d996c14..2f41a42fc 100644 --- a/logic.py +++ b/logic.py @@ -855,9 +855,9 @@ def fol_fc_ask(KB, alpha): if p != p_: for theta in (subst(theta, p) == subst(theta, p_)): q_ = subst(theta, q) - if not unify(q_,KB.sentence in KB) or not unify(q_, new): + if not unify(q_, KB.sentence in KB) or not unify(q_, new): new.append(q_) - phi = unify(q_,alpha) + phi = unify(q_, alpha) if phi is not None: return phi KB.tell(new) diff --git a/text.py b/text.py index 991c764d9..1e1ca6906 100644 --- a/text.py +++ b/text.py @@ -26,7 +26,6 @@ def samples(self, n): return ' '.join(self.sample() for i in range(n)) - class NgramTextModel(CountingProbDist): """This is a discrete probability distribution over n-tuples of words. @@ -80,7 +79,7 @@ def samples(self, nwords): class NgramCharModel(NgramTextModel): def add_empty(self, words, n): - return ' ' * (n - 1) + words + return ' ' * (n - 1) + words def add_sequence(self, words): for word in words: From 884151551646b4f503eb5fa02c7b6891fb080f67 Mon Sep 17 00:00:00 2001 From: Kritika Date: Wed, 29 Mar 2017 10:03:38 +0530 Subject: [PATCH 2/4] fixed remaining flake8 errors --- csp.py | 1 - learning.py | 1 - logic.py | 21 +++++++++++---------- utils.py | 1 + 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/csp.py b/csp.py index 3bd6d88ac..7e5c86a2f 100644 --- a/csp.py +++ b/csp.py @@ -336,7 +336,6 @@ def topological_sort(X, root): visited shows the state (visited - not visited) of nodes """ - nodes = X.variables neighbors = X.neighbors visited = defaultdict(lambda: False) diff --git a/learning.py b/learning.py index ec685131d..74ecc16d8 100644 --- a/learning.py +++ b/learning.py @@ -668,7 +668,6 @@ def LinearLearner(dataset, learning_rate=0.01, epochs=100): for i in range(len(w)): w[i] = w[i] + learning_rate * (dotproduct(err, X_col[i]) / num_examples) - def predict(example): x = [1] + example return dotproduct(w, x) diff --git a/logic.py b/logic.py index 2f41a42fc..b3dc95add 100644 --- a/logic.py +++ b/logic.py @@ -847,19 +847,20 @@ def subst(s, x): def fol_fc_ask(KB, alpha): """A simple forward-chaining algorithm. [Figure 9.3]""" + new = [] while new is not None: - new = [] - for rule in KB: + for rule in KB.clauses: p, q = parse_definite_clause(standardize_variables(rule)) - for p_ in random.KB.clauses: + for p_ in KB.clauses: if p != p_: - for theta in (subst(theta, p) == subst(theta, p_)): - q_ = subst(theta, q) - if not unify(q_, KB.sentence in KB) or not unify(q_, new): - new.append(q_) - phi = unify(q_, alpha) - if phi is not None: - return phi + for theta in KB.clauses: + if subst(theta, p) == subst(theta, p_): + q_ = subst(theta, q) + if not unify(q_, KB.sentence in KB) or not unify(q_, new): + new.append(q_) + phi = unify(q_, alpha) + if phi is not None: + return phi KB.tell(new) return None diff --git a/utils.py b/utils.py index ed44f1e9e..5f31f2566 100644 --- a/utils.py +++ b/utils.py @@ -7,6 +7,7 @@ import os.path import random import math +import functools # ______________________________________________________________________________ # Functions on Sequences and Iterables From a28bb21eb038bf9dad0f2a90cdd989e39055700b Mon Sep 17 00:00:00 2001 From: Kritika Date: Fri, 31 Mar 2017 14:49:41 +0530 Subject: [PATCH 3/4] fixed loop --- text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text.py b/text.py index 1e1ca6906..58f5b6099 100644 --- a/text.py +++ b/text.py @@ -59,7 +59,7 @@ def add_sequence(self, words): n = self.n words = self.add_empty(words, n) - for i in range(len(words) - n): + for i in range(len(words) - n+1): self.add(tuple(words[i:i + n])) def samples(self, nwords): From 001bcd4a9c510711a78a5846394a26f58d33d331 Mon Sep 17 00:00:00 2001 From: Kritika Date: Fri, 31 Mar 2017 17:08:08 +0530 Subject: [PATCH 4/4] added space --- text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text.py b/text.py index 58f5b6099..e1acc6624 100644 --- a/text.py +++ b/text.py @@ -59,7 +59,7 @@ def add_sequence(self, words): n = self.n words = self.add_empty(words, n) - for i in range(len(words) - n+1): + for i in range(len(words) - n + 1): self.add(tuple(words[i:i + n])) def samples(self, nwords):