Navigation Menu

Skip to content

Commit

Permalink
fix tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtux89 committed Jan 7, 2017
1 parent 082bc07 commit 044198c
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 108 deletions.
8 changes: 4 additions & 4 deletions buf.py
Expand Up @@ -15,21 +15,21 @@ class Buffer:
def __init__(self, embs, tokens, alignments):
self.embs = embs
self.tokens = tokens
if alignments != None:
if alignments is not None:
for token, al in zip(tokens, alignments):
token.nodes = al

def __repr__(self):
buf = [item.word for item in self.tokens]
return '<%s %s>' % (
self.__class__.__name__, buf)
self.__class__.__name__, buf)

def size(self):
return len(self.tokens)

def reorder(self, deps, N):
order = deps.postorder(N)
if order != None:
if order is not None:
self.tokens = order

def isEmpty(self):
Expand Down Expand Up @@ -73,5 +73,5 @@ def nes(self, K, start = 0):
return ret

def __eq__(self, other):
return other != None and self.tokens == other.tokens
return other is not None and self.tokens == other.tokens

4 changes: 2 additions & 2 deletions buftoken.py
Expand Up @@ -22,11 +22,11 @@ def __init__(self, word, lemma, ne, pos, index, nodes):
self.nodes = nodes

def __eq__(self, other):
return other != None and self.word == other.word and self.lemma == other.lemma and self.pos == other.pos and self.ne == other.ne and self.index == other.index
return other is not None and self.word == other.word and self.lemma == other.lemma and self.pos == other.pos and self.ne == other.ne and self.index == other.index

def __repr__(self):
return '<%s %s %s %s %s %s %s>' % (
self.__class__.__name__, str(self.word), str(self.lemma), self.ne, self.pos, self.index, self.nodes)
self.__class__.__name__, str(self.word), str(self.lemma), self.ne, self.pos, self.index, self.nodes)

def __hash__(self):
return hash((self.word, self.lemma, self.ne, self.pos, self.index))
18 changes: 9 additions & 9 deletions create_dataset.py
Expand Up @@ -49,7 +49,7 @@ def create(prefix, split, model_dir):
for f_cat in f_rel:
for v in f_cat:
dataset.write(str(v) + ",")
dataset.write(str(action.get_id()) + "\n")
dataset.write(str(action.get_id()) + "\n")

if action.name.endswith("arc"):
if action.argv in labels:
Expand All @@ -59,15 +59,15 @@ def create(prefix, split, model_dir):
labels_dataset.write(str(labels[action.argv]) + "\n")

if action.name == "reduce":
if action.argv is not None:
for sib, vec in zip(action.argv[2],f_reentr):
if action.argv is not None:
for sib, vec in zip(action.argv[2],f_reentr):
for f_cat in vec:
for v in f_cat:
reentr_dataset.write(str(v) + ",")
if sib == action.argv[0]:
reentr_dataset.write(str(1) + "\n")
else:
reentr_dataset.write(str(2) + "\n")
for v in f_cat:
reentr_dataset.write(str(v) + ",")
if sib == action.argv[0]:
reentr_dataset.write(str(1) + "\n")
else:
reentr_dataset.write(str(2) + "\n")
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("-t", "--train", help="Training set", required = True)
Expand Down
12 changes: 6 additions & 6 deletions embs.py
Expand Up @@ -32,9 +32,9 @@ def __init__(self, vocab):
def get(self, label):
assert(label is not None)
if label == "<TOP>":
return self.enc["<TOP>"]
return self.enc["<TOP>"]
if label.startswith("<NULL"):
return self.enc["<NULL>"]
return self.enc["<NULL>"]
if label in self.enc:
return self.enc[label]
return self.enc["<UNK>"]
Expand Down Expand Up @@ -89,7 +89,7 @@ def __init__(self, generate, initializationFileIn, initializationFileOut, dim, u
fw.write("\n")
self.counter += 1

if punct != None:
if punct is not None:
self.indexes["<PUNCT>"] = self.counter
if generate:
fw.write(str(punct[0]))
Expand All @@ -107,7 +107,7 @@ def get(self, word):

if self.prepr:
word = self._preprocess(word)
if self.punct != None and word not in self.indexes and word in list(string.punctuation):
if self.punct is not None and word not in self.indexes and word in list(string.punctuation):
return self.indexes["<PUNCT>"]
elif word in self.indexes:
return self.indexes[word]
Expand All @@ -119,9 +119,9 @@ def _preprocess(self, word):
word = word[1:-1]
reg = re.compile(".+-[0-9][0-9]")
word = word.strip().lower()
if reg.match(word) != None:
if reg.match(word) is not None:
word = word.split("-")[0]
if re.match("^[0-9]", word) != None:
if re.match("^[0-9]", word) is not None:
word = word[0]
word = word.replace("0","zero")
word = word.replace("1","one")
Expand Down
25 changes: 12 additions & 13 deletions hooks.py
Expand Up @@ -107,7 +107,7 @@ def isCountry(name):
return c
if name.startswith("the_") or name.startswith("The_"):
name = name[4:]
return isCountry(name)
return isCountry(name)
return None

def stripzeros(num):
Expand Down Expand Up @@ -150,24 +150,24 @@ def run(token, var, label, variables):

if label == "LOCATION":
state = isState(token.word)
if state:
if state is not None:
return names(label, "state", token, var, variables)
country = isCountry(token.word)
if country != None:
return names(label, "country", token, var, variables)
city = isCity(token.word)
if city != None:
return names(label, "city", token, var, variables)
country = isCountry(token.word)
if country is not None:
return names(label, "country", token, var, variables)
city = isCity(token.word)
if city is not None:
return names(label, "city", token, var, variables)
return False
if var.lower() in nationalities:
return names(label, "country", token, nationalities[var.lower()], variables)
if label == "PERSON":
return names(label, "person", token, var, variables)
return names(label, "person", token, var, variables)
if label == "ORGANIZATION":
org_type = isOrg(token.word)
if org_type != None:
return names(label, org_type, token, var, variables)
return False
if org_type is not None:
return names(label, org_type, token, var, variables)
return False

if label == "ORDINAL":
num = var.split(".")[0]
Expand Down Expand Up @@ -203,7 +203,6 @@ def run(token, var, label, variables):
n1 = Node(token, var, "NUMBER", True)
nodes.append(n1)
return (nodes, relations)
return percentage(token, var, variables)

if label =="MONEY":
num1 = var.split("_")[0]
Expand Down
2 changes: 1 addition & 1 deletion nnets/classify.lua
@@ -1,7 +1,7 @@
require 'dp'
require 'optim'
require 'nngraph'
--require 'cunn'
require 'cunn'

function argmax_mask(v, mask)
local maxvalue = (torch.min(v) - 1)
Expand Down
8 changes: 4 additions & 4 deletions node.py
Expand Up @@ -15,7 +15,7 @@

class Node:
def __init__(self, token, var = None, concept = None, isConst = None):
assert (type(token) == bool and token == True) or (var != None and isConst != None)
assert (type(token) == bool and token == True) or (var is not None and isConst is not None)
if type(token) == bool and token == True: # special case for top node, use token as boolean flag
self.isRoot = True
self.token = None
Expand Down Expand Up @@ -52,14 +52,14 @@ def __hash__(self):
def __repr__(self):
if self.isRoot:
return '<%s %s>' % (
self.__class__.__name__, "TOP")
self.__class__.__name__, "TOP")
else:
if self.isConst:
return '<%s %s %s %s>' % (
self.__class__.__name__, "const", self.constant, self.concept)
self.__class__.__name__, "const", self.constant, self.concept)
else:
return '<%s %s %s>' % (
self.__class__.__name__, self.var, self.concept)
self.__class__.__name__, self.var, self.concept)

def variable(self):
if self.isRoot:
Expand Down
12 changes: 6 additions & 6 deletions oracle.py
Expand Up @@ -19,10 +19,10 @@ class Oracle:
def reentrancy(self, node, found):
siblings = [item[0] for p in found.parents[node] for item in found.children[p[0]] if item[0] != node]
for s in siblings:
label = self.gold.isRel(node, s)
if label is not None:
self.gold.parents[s].remove((node,label))
self.gold.children[node].remove((s,label))
label = self.gold.isRel(node, s)
if label is not None:
self.gold.parents[s].remove((node,label))
self.gold.children[node].remove((s,label))
parents = [i[0] for i in found.parents[node]]
parents = [i[0] for i in found.parents[s] if i[0] in parents]
return [s, label, siblings]
Expand Down Expand Up @@ -77,7 +77,7 @@ def valid_actions(self, state):
self.gold.children[n2].remove((child,label))
self.gold.parents[child].remove((n2,label))

subgraph = Subgraph(nodes, relations)
return Action("shift", subgraph)
subgraph = Subgraph(nodes, relations)
return Action("shift", subgraph)

return None
2 changes: 1 addition & 1 deletion parser.py
Expand Up @@ -66,7 +66,7 @@ def to_string(triples, root):
children = [t for t in triples if str(t[0]) == root]
assert(len(children)==1)
if children[0][4] == "":
return "(e / emptygraph)", defaultdict(list), []
return "(e / emptygraph)", defaultdict(list), []
return _to_string(triples, children[0][3] + " / " + children[0][4], 1, False, [], "0", defaultdict(list), [])

def main(args):
Expand Down
2 changes: 1 addition & 1 deletion preprocessing.py
Expand Up @@ -24,7 +24,7 @@
negation_words = [n.split()[0].replace('"',"") for n in negation_words]

def normalize(token):
if re.match("[0-9]+,[0-9]+", token) != None:
if re.match("[0-9]+,[0-9]+", token) is not None:
token = token.replace(",",".")
return token

Expand Down
12 changes: 6 additions & 6 deletions relations.py
Expand Up @@ -112,7 +112,7 @@ def triples(self):
return lst

def _leftmost(self, node, direction, other = None):
if node != None:
if node is not None:
if direction == "child":
lst = self.children[node]
else:
Expand All @@ -131,7 +131,7 @@ def _leftmost(self, node, direction, other = None):
return None

def _rightmost(self, node, direction, other = None):
if node != None:
if node is not None:
if direction == "child":
lst = self.children[node]
else:
Expand Down Expand Up @@ -162,9 +162,9 @@ def leftmost_child(self, node, other = None):

def leftmost_grandchild(self, node, other = None):
child = self._leftmost(node, "child", other)
if child != None:
if child is not None:
grandchild = self._leftmost(child[0], "child", other)
if grandchild != None:
if grandchild is not None:
if grandchild[0].isConst:
return grandchild[0].constant
elif grandchild[0].isRoot:
Expand All @@ -186,9 +186,9 @@ def rightmost_child(self, node, other = None):

def rightmost_grandchild(self, node, other = None):
child = self._rightmost(node, "child", other)
if child != None:
if child is not None:
grandchild = self._rightmost(child[0], "child", other)
if grandchild != None:
if grandchild is not None:
if grandchild[0].isConst:
return grandchild[0].constant
elif grandchild[0].isRoot:
Expand Down
72 changes: 36 additions & 36 deletions resources.py
Expand Up @@ -53,42 +53,42 @@ def init_table(model_dir, empty = True):

# Resources.verbalization_list = {}
# for line in open("resources/verbalization-list-v1.06.txt"):
# line = line.strip().split()
# if line[0] == "VERBALIZE":
# var = Variables()
# nodes = []
# ntop = Node(None, var.nextVar(), line[3], False)
# nodes.append(ntop)
# relations = []
# fields = line[4:]
# for i in range(0,len(fields),2):
# if fields[i + 1] == "-":
# n = Node(None, '-', "", True)
# else:
# n = Node(None, var.nextVar(), fields[i + 1], False)
# nodes.append(n)
# relations.append((ntop,n,fields[i]))
# Resources.verbalization_list[line[1]] = Subgraph(nodes, relations)
# line = line.strip().split()
# if line[0] == "VERBALIZE":
# var = Variables()
# nodes = []
# ntop = Node(None, var.nextVar(), line[3], False)
# nodes.append(ntop)
# relations = []
# fields = line[4:]
# for i in range(0,len(fields),2):
# if fields[i + 1] == "-":
# n = Node(None, '-', "", True)
# else:
# n = Node(None, var.nextVar(), fields[i + 1], False)
# nodes.append(n)
# relations.append((ntop,n,fields[i]))
# Resources.verbalization_list[line[1]] = Subgraph(nodes, relations)
# for line in open("resources/have-org-role-91-roles-v1.06.txt"):
# line = line.strip().split()
# if line[0] == "USE-HAVE-ORG-ROLE-91-ARG2":
# var = Variables()
# ntop = Node(None, var.nextVar(), "have-org-role-91", False)
# node = Node(None, var.nextVar(), line[1], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# line = line.strip().split()
# if line[0] == "USE-HAVE-ORG-ROLE-91-ARG2":
# var = Variables()
# ntop = Node(None, var.nextVar(), "have-org-role-91", False)
# node = Node(None, var.nextVar(), line[1], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])

# for line in open("resources/have-rel-role-91-roles-v1.06.txt"):
# if "#" in line:
# line = line.split("#")[0]
# line = line.strip().split()
# if len(line) > 0 and line[0] == "USE-HAVE-REL-ROLE-91-ARG2":
# var = Variables()
# if len(line) >= 3 and line[2] == ":standard":
# ntop = Node(None, var.nextVar(), "have-rel-role-91", False)
# node = Node(None, var.nextVar(), line[3], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# Resources.verbalization_list[line[3]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# else:
# ntop = Node(None, var.nextVar(), "have-rel-role-91", False)
# node = Node(None, var.nextVar(), line[1], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# if "#" in line:
# line = line.split("#")[0]
# line = line.strip().split()
# if len(line) > 0 and line[0] == "USE-HAVE-REL-ROLE-91-ARG2":
# var = Variables()
# if len(line) >= 3 and line[2] == ":standard":
# ntop = Node(None, var.nextVar(), "have-rel-role-91", False)
# node = Node(None, var.nextVar(), line[3], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# Resources.verbalization_list[line[3]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
# else:
# ntop = Node(None, var.nextVar(), "have-rel-role-91", False)
# node = Node(None, var.nextVar(), line[1], False)
# Resources.verbalization_list[line[1]] = Subgraph([ntop, node], [(ntop, node, ":ARG2")])
12 changes: 6 additions & 6 deletions rules.py
Expand Up @@ -49,10 +49,10 @@ def _add(self, line, type):

def check(self, node1, node2):
assert(isinstance(node1, Node) and isinstance(node2, Node))
if node1.isConst:
return [0]*len(self.labels)
if node2.isRoot:
return [0]*len(self.labels)
if node1.isConst:
return [0]*len(self.labels)
if node2.isRoot:
return [0]*len(self.labels)

legals = [-1]*len(self.labels)
for i, rel in enumerate(self.labels):
Expand All @@ -67,8 +67,8 @@ def check(self, node1, node2):
else:
legals[i] = 1
else:
if node1.concept is not None and re.match(".*-[0-9][0-9]*", node1.concept) is None:
legals[i] = 0
if node1.concept is not None and re.match(".*-[0-9][0-9]*", node1.concept) is None:
legals[i] = 0
else:
ind = int(rel[-1])
if len(self.args_rules) > ind and node1.concept in self.args_rules[ind]:
Expand Down

0 comments on commit 044198c

Please sign in to comment.