From a3a75534158004480840f4e2dd78dcbf7dc993b6 Mon Sep 17 00:00:00 2001 From: akashrajkn Date: Thu, 30 Nov 2017 15:22:34 +0100 Subject: [PATCH 1/3] Convert sentence to adjancency matrix --- src/utils.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/utils.py b/src/utils.py index 08d6992..a568b77 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,11 +1,28 @@ +import numpy as np + + def convert_sentence_to_adjancency_matrix(sentence): ''' - Input: sentence in json? + Input: sentence in json Output: adjancency matrix (gold standard) ''' + + sentence_len = len(sentence['words']) + + # Initialize a matrix of size N x N + adjancency_matrix = np.zeros((sentence_len, sentence_len)) + for word in sentence['words']: - pass + word_id = int(word['id']) + head = int(word['head']) + + # Ignore the root(0)-(-1) connection + if head == -1: + continue + + adjancency_matrix[head][word_id] = 1 + return adjancency_matrix def convert_adjancecy_matrix_to_sentece(matrix): pass From fd2a39e69d0a67d3a760b3e82bd10e8e66e113d5 Mon Sep 17 00:00:00 2001 From: Jack-Harding1 <31994187+Jack-Harding1@users.noreply.github.com> Date: Tue, 12 Dec 2017 18:51:05 +0100 Subject: [PATCH 2/3] Update utils.py --- src/utils.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/utils.py b/src/utils.py index a568b77..91b6000 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,16 +1,16 @@ import numpy as np -def convert_sentence_to_adjancency_matrix(sentence): +def convert_sentence_to_adjacency_matrix(sentence): ''' Input: sentence in json - Output: adjancency matrix (gold standard) + Output: adjacency matrix (gold standard) ''' sentence_len = len(sentence['words']) # Initialize a matrix of size N x N - adjancency_matrix = np.zeros((sentence_len, sentence_len)) + adjacency_matrix = np.zeros((sentence_len, sentence_len)) for word in sentence['words']: word_id = int(word['id']) @@ -20,9 +20,23 @@ def convert_sentence_to_adjancency_matrix(sentence): if head == -1: continue - adjancency_matrix[head][word_id] = 1 - - return adjancency_matrix - -def convert_adjancecy_matrix_to_sentece(matrix): - pass + adjacency_matrix[head][word_id] = 1 + + return adjacency_matrix + +def adjacency_matrix_to_tensor(matrix): + output = [0] * matrix.shape[0] + for i in range(matrix.shape[0]): + for j in range(matrix.shape[0]): + if matrix[i][j] == 1: + output[j] = i + output1 = torch.LongTensor(output) + return(output1) + +test_matrix = np.array(([1, 0, 1, 1, 0], + [0, 0, 0, 0, 0], + [0, 1, 0, 0, 0], + [0, 0, 0, 0, 1], + [0, 0, 0, 0, 0])) +print(test_matrix) +print(adjacency_matrix_to_tensor(test_matrix)) From d68b408f66359ff0f5308a9dd464559d7c6de335 Mon Sep 17 00:00:00 2001 From: Jack-Harding1 <31994187+Jack-Harding1@users.noreply.github.com> Date: Tue, 12 Dec 2017 18:57:25 +0100 Subject: [PATCH 3/3] Update utils.py --- src/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils.py b/src/utils.py index 91b6000..e1254f6 100644 --- a/src/utils.py +++ b/src/utils.py @@ -40,3 +40,12 @@ def adjacency_matrix_to_tensor(matrix): [0, 0, 0, 0, 0])) print(test_matrix) print(adjacency_matrix_to_tensor(test_matrix)) + +#something like this will convert directly +def convert_sentence_to_tensor(sentence): + sentence_len = len(sentence['words']) + output = [0] * sentence_len + for word in sentence['words']: + output[int(word['id'])] = int(word['head']) + output1 = torch.LongTensor(output) + return output1