Loading probing.py +52 −56 Changes for probing.py: 52 added lines, 56 removed lines. Original line number Diff line number Diff line from STSB_Dataset import STSB_Dataset import pandas as pd import spacy import random df = STSB_Dataset('data/stsbenchmark/sts-test.csv').as_dataframe Loading @@ -14,60 +13,75 @@ sentences2 = df['sentence2'].tolist() sentences1_nlp = [nlp(sent) for sent in sentences1] sentences2_nlp = [nlp(sent) for sent in sentences2] sent1_roots = [] sent2_roots = [] sent1_root_children = [] sent2_root_children = [] dependency_hierarchy1 = [] dependency_hierarchy2 = [] for sent1 in sentences1_nlp: root1 = [(token.text, token, token.pos_) for token in sent1 if token.dep_ == "ROOT"] sent1_roots.append((root1[0][0], root1[0][2])) root_children1 = [(child.text, child.pos_) for child in root1[0][1].children] sent1_root_children.append(root_children1) sentence_tokens = [token for token in sent1] dep_hierarchy1 = [] dep_hierarchy1.append([token for token in sent1 if token.dep_ == "ROOT"]) sentence_tokens.remove(dep_hierarchy1[0][0]) while len(sentence_tokens) != 0: children = [] for i in range(len(dep_hierarchy1[-1])): index_children = [t for t in sent1 if t.head.text == dep_hierarchy1[-1][i].text and t in sentence_tokens] for elem in index_children: children.append(elem) sentence_tokens.remove(elem) dep_hierarchy1.append(children) dependency_hierarchy1.append(dep_hierarchy1) for sent2 in sentences2_nlp: root2 = [(token.text, token, token.pos_) for token in sent2 if token.dep_ == "ROOT"] sent2_roots.append((root2[0][0], root2[0][2])) root_children2 = [(child.text, child.pos_) for child in root2[0][1].children] sent2_root_children.append(root_children2) sentence_tokens = [token for token in sent2] dep_hierarchy2 = [] dep_hierarchy2.append([token for token in sent2 if token.dep_ == "ROOT"]) sentence_tokens.remove(dep_hierarchy2[0][0]) while len(sentence_tokens) != 0: children = [] for i in range(len(dep_hierarchy2[-1])): index_children = [t for t in sent2 if t.head.text == dep_hierarchy2[-1][i].text and t in sentence_tokens] for elem in index_children: children.append(elem) sentence_tokens.remove(elem) dep_hierarchy2.append(children) dependency_hierarchy2.append(dep_hierarchy2) assert len(sent1_roots) == len(sent2_roots) assert len(sent1_root_children) == len(sent2_root_children) def mask_token(pos_tag): del_rows = [] #rows to be deleted for i in range(len(sent1_roots)): if sent1_roots[i][1] == pos_tag: df.iloc[i, 0] = df.iloc[i, 0].replace(sent1_roots[i][0], "[MASK]") else: for e in range(len(sent1_root_children[i])): if pos_tag in sent1_root_children[i][e]: df.iloc[i, 0] = df.iloc[i, 0].replace(sent1_root_children[i][e][0], "[MASK]") for e in range(len(dependency_hierarchy1)): mask_token = None for layer in dependency_hierarchy1[e]: if mask_token != None: break elif e == len(sent1_root_children[i]) - 1: del_rows.append(i) for i in range(len(sent2_roots)): if sent1_roots[i][1] == pos_tag: df.iloc[i, 1] = df.iloc[i, 1].replace(sent2_roots[i][0], "[MASK]") else: for e in range(len(sent2_root_children[i])): if pos_tag in sent2_root_children[i][e]: df.iloc[i, 1] = df.iloc[i, 1].replace(sent2_root_children[i][e][0], "[MASK]") for i in range(len(layer)): if layer[i].pos_ == pos_tag: mask_token = layer[i].text df.iloc[e, 0] = df.iloc[e, 0].replace(mask_token, "[MASK]") break elif e == len(sent2_root_children[i]) - 1: if i not in del_rows: del_rows.append(i) df.drop(del_rows, inplace=True) for e in range(len(dependency_hierarchy2)): mask_token = None for layer in dependency_hierarchy2[e]: if mask_token != None: break for i in range(len(layer)): if layer[i].pos_ == pos_tag: mask_token = layer[i].text df.iloc[e, 1] = df.iloc[e, 1].replace(mask_token, "[MASK]") break return df def mask_first(pos_tag): i = -1 Loading Loading @@ -102,21 +116,3 @@ def mask_first(pos_tag): return df #print(sent1_roots) #print(sent2_roots) #print(sent1_root_children) #print(sent2_root_children) # mask_token("VERB") print(mask_first("NUM")) Loading
probing.py +52 −56 Changes for probing.py: 52 added lines, 56 removed lines. Original line number Diff line number Diff line from STSB_Dataset import STSB_Dataset import pandas as pd import spacy import random df = STSB_Dataset('data/stsbenchmark/sts-test.csv').as_dataframe Loading @@ -14,60 +13,75 @@ sentences2 = df['sentence2'].tolist() sentences1_nlp = [nlp(sent) for sent in sentences1] sentences2_nlp = [nlp(sent) for sent in sentences2] sent1_roots = [] sent2_roots = [] sent1_root_children = [] sent2_root_children = [] dependency_hierarchy1 = [] dependency_hierarchy2 = [] for sent1 in sentences1_nlp: root1 = [(token.text, token, token.pos_) for token in sent1 if token.dep_ == "ROOT"] sent1_roots.append((root1[0][0], root1[0][2])) root_children1 = [(child.text, child.pos_) for child in root1[0][1].children] sent1_root_children.append(root_children1) sentence_tokens = [token for token in sent1] dep_hierarchy1 = [] dep_hierarchy1.append([token for token in sent1 if token.dep_ == "ROOT"]) sentence_tokens.remove(dep_hierarchy1[0][0]) while len(sentence_tokens) != 0: children = [] for i in range(len(dep_hierarchy1[-1])): index_children = [t for t in sent1 if t.head.text == dep_hierarchy1[-1][i].text and t in sentence_tokens] for elem in index_children: children.append(elem) sentence_tokens.remove(elem) dep_hierarchy1.append(children) dependency_hierarchy1.append(dep_hierarchy1) for sent2 in sentences2_nlp: root2 = [(token.text, token, token.pos_) for token in sent2 if token.dep_ == "ROOT"] sent2_roots.append((root2[0][0], root2[0][2])) root_children2 = [(child.text, child.pos_) for child in root2[0][1].children] sent2_root_children.append(root_children2) sentence_tokens = [token for token in sent2] dep_hierarchy2 = [] dep_hierarchy2.append([token for token in sent2 if token.dep_ == "ROOT"]) sentence_tokens.remove(dep_hierarchy2[0][0]) while len(sentence_tokens) != 0: children = [] for i in range(len(dep_hierarchy2[-1])): index_children = [t for t in sent2 if t.head.text == dep_hierarchy2[-1][i].text and t in sentence_tokens] for elem in index_children: children.append(elem) sentence_tokens.remove(elem) dep_hierarchy2.append(children) dependency_hierarchy2.append(dep_hierarchy2) assert len(sent1_roots) == len(sent2_roots) assert len(sent1_root_children) == len(sent2_root_children) def mask_token(pos_tag): del_rows = [] #rows to be deleted for i in range(len(sent1_roots)): if sent1_roots[i][1] == pos_tag: df.iloc[i, 0] = df.iloc[i, 0].replace(sent1_roots[i][0], "[MASK]") else: for e in range(len(sent1_root_children[i])): if pos_tag in sent1_root_children[i][e]: df.iloc[i, 0] = df.iloc[i, 0].replace(sent1_root_children[i][e][0], "[MASK]") for e in range(len(dependency_hierarchy1)): mask_token = None for layer in dependency_hierarchy1[e]: if mask_token != None: break elif e == len(sent1_root_children[i]) - 1: del_rows.append(i) for i in range(len(sent2_roots)): if sent1_roots[i][1] == pos_tag: df.iloc[i, 1] = df.iloc[i, 1].replace(sent2_roots[i][0], "[MASK]") else: for e in range(len(sent2_root_children[i])): if pos_tag in sent2_root_children[i][e]: df.iloc[i, 1] = df.iloc[i, 1].replace(sent2_root_children[i][e][0], "[MASK]") for i in range(len(layer)): if layer[i].pos_ == pos_tag: mask_token = layer[i].text df.iloc[e, 0] = df.iloc[e, 0].replace(mask_token, "[MASK]") break elif e == len(sent2_root_children[i]) - 1: if i not in del_rows: del_rows.append(i) df.drop(del_rows, inplace=True) for e in range(len(dependency_hierarchy2)): mask_token = None for layer in dependency_hierarchy2[e]: if mask_token != None: break for i in range(len(layer)): if layer[i].pos_ == pos_tag: mask_token = layer[i].text df.iloc[e, 1] = df.iloc[e, 1].replace(mask_token, "[MASK]") break return df def mask_first(pos_tag): i = -1 Loading Loading @@ -102,21 +116,3 @@ def mask_first(pos_tag): return df #print(sent1_roots) #print(sent2_roots) #print(sent1_root_children) #print(sent2_root_children) # mask_token("VERB") print(mask_first("NUM"))