import numpy as np import math import json from mermaid import preprocessing as pp from mermaid import sessions from mermaid import models import json import os from tqdm import tqdm_notebook import gensim from mermaid.embeddings import Embedding from mermaid import preprocessing as pp from mermaid.preprocessing import WordIndex from mermaid import plotting from mermaid import models import numpy as np import mermaid from mermaid.factory import Factory from mermaid import sessions import keras sentence_set = [] with open('/home/NewPartion/e2e-coref/dev.english.jsonlines','r') as file: for f in file.readlines(): f = json.loads(f) sentence_set.append(f['sentences']) print(len(sentence_set)) antecedent_scores = np.load('/home/NewPartion/e2e-coref/antecedents.npz', encoding='bytes')['arr_0'] mention_starts = np.load('/home/NewPartion/e2e-coref/mention_starts.npz', encoding='bytes')['arr_0'] mention_ends = np.load('/home/NewPartion/e2e-coref/mention_ends.npz', encoding='bytes')['arr_0'] new_sentence_set = [] for item in sentence_set: new_item = [] for a in item: for b in a: new_item.append(b) new_sentence_set.append(new_item) print(len(new_sentence_set[0])) i=0 count=0 for index in range(len(mention_starts[i])): first_mention = new_sentence_set[i][mention_starts[i][index]:mention_ends[i][index]] for index2 in range(index + 1, len(mention_ends[i])): if mention_starts[i][index2] < mention_ends[i][index]: continue second_mention = new_sentence_set[i][mention_starts[i][index2]:mention_ends[i][index2]] split_index = math.ceil((mention_starts[i][index2] - mention_ends[i][index]) / 2) splitted_sentence1 = ' '.join(new_sentence_set[i][:split_index]) splitted_sentence2 = ' '.join(new_sentence_set[i][split_index:]) count+=1 if count == 10000: break print(antecedent_scores[i].shape) print(len(mention_ends[i])) print(len(mention_starts[i])) print(count) class LoadedWordVecEmbedding(Embedding): def __init__(self, word_vector_file_path, dim): super().__init__() self._w2v = gensim.models.KeyedVectors.load_word2vec_format(word_vector_file_path) self._word_index = WordIndex({ token: self._w2v.vocab[token].index + 2 for token in self._w2v.vocab }) self._dim = dim def make_layer(self): embedding_matrix = np.zeros((self.vocab_size, self._dim)) for key, index in self._word_index.items(): embedding_matrix[index] = self._w2v[key] return keras.layers.Embedding( self.vocab_size, self._dim, weights=[embedding_matrix], trainable=self._trainable ) @property def vocab_size(self): return len(self._word_index) + 2 @property def word_index(self): return self._word_index @property def index_word(self): return self._word_index.rev() def __getitem__(self, item): return self._w2v[item] sess = sessions.DQTrainingSession() sess.setup.embedding('pre-trained') sess.setup.embedding('loadedwordvecembedding-legruwxj6pwz') sess.setup.model('convknrm-z3kmin68al3z') sess.setup.preprocessor('word.30','word.30') sess.compile() to_predict_sentence = {'d':splitted_sentence1,'q':splitted_sentence2} predict_stream = pp.Stream([pp.Entry(**to_predict_sentence)]) sess.build_stream_tokens(predict_stream) sess.build_stream_sequences(predict_stream) sess.model.predict(sess.X(predict_stream))